Argo  1.0
A C++ library for handling JSON.
reader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Andrew Haisley
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
24 
25 #include "common.hpp"
26 #include "reader.hpp"
27 #include "json_io_exception.hpp"
28 
29 using namespace NAMESPACE;
30 
31 reader::reader(size_t max_message_length, bool block_read) :
32  m_put_back(reader::no_put_back),
33  m_byte_index(0),
34  m_max_message_length(max_message_length),
35  m_block_read(block_read),
36  m_block_num_bytes(0),
37  m_block_index(-1)
38 {
39 }
40 
42 {
44  {
45  if (!read_next_block())
46  {
47  return EOF;
48  }
49  }
50 
51  return m_block[m_block_index++];
52 }
53 
55 {
56  if (m_put_back == no_put_back)
57  {
59 
61  {
63  }
64 
65  return res;
66  }
67  else
68  {
69  int c = m_put_back;
71  return c;
72  }
73 }
74 
75 void reader::put_back(int c)
76 {
77  if (m_put_back == no_put_back)
78  {
79  m_put_back = c;
80  }
81  else
82  {
84  }
85 }
86 
87 size_t reader::get_byte_index() const
88 {
89  return m_byte_index;
90 }
91 
93 {
94  m_byte_index = 0;
95 }
size_t m_byte_index
Total number of characters read.
Definition: reader.hpp:97
A class to read JSON messages from various types of input stream.
Definition: reader.hpp:42
virtual int next()
Get the next character from the reader.
Definition: reader.cpp:54
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
Specific class of exceptions for IO errors of various types.
Maximum message length was exceeded.
unsigned char m_block[block_size]
If we're reading blocks, the block data.
Definition: reader.hpp:106
Attempt to put back more than one character onto a reader.
int m_put_back
Character last put back. If equal to no_put_back then none was.
Definition: reader.hpp:94
Common defs needed everywhere and, as far as is possible, platform specific changes.
virtual int read_next_char()=0
Implemented by derived classes to do the actual reading.
static const int no_put_back
Special character value indicating that no character has been put back.
Definition: reader.hpp:80
virtual bool read_next_block()=0
size_t get_byte_index() const
Get the current byte index in the input.
Definition: reader.cpp:87
size_t m_max_message_length
Maximum length of a message.
Definition: reader.hpp:100
The reader class.
int m_block_index
If we're reading blocks, the index into the current block.
Definition: reader.hpp:112
int read_next_char_from_block()
When in block read mode, get the next available character.
Definition: reader.cpp:41
virtual void put_back(int c)
Put back a character so that it is returned by the next call to next().
Definition: reader.cpp:75
The json_io_exception class.
void reset_byte_index()
Reset the byte index at the start of parsing a messege.
Definition: reader.cpp:92
int m_block_num_bytes
If we're reading blocks, the number of bytes in the current block.
Definition: reader.hpp:109
bool m_block_read
Whether to read in blocks or one byte at a time.
Definition: reader.hpp:103