Argo  1.0
A C++ library for handling JSON.
stream_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 "stream_reader.hpp"
27 #include "json_io_exception.hpp"
28 
29 using namespace std;
30 using namespace NAMESPACE;
31 
32 
33 stream_reader::stream_reader(istream *s, int max_message_length, bool block_read) :
34  reader(max_message_length, block_read),
35  m_stream(s)
36 {
37 }
38 
40 {
41  return m_stream->get();
42 }
43 
45 {
46  m_stream->read(reinterpret_cast<char *>(m_block), block_size);
47 
48  streamsize n = m_stream->gcount();
49 
50  if (n > 0)
51  {
52  m_block_num_bytes = static_cast<int>(n);
53  m_block_index = 0;
54  return true;
55  }
56  else
57  {
58  if (m_stream->eof())
59  {
60  return false;
61  }
62  else
63  {
65  }
66  }
67 }
virtual int read_next_char()
A class to read JSON messages from various types of input stream.
Definition: reader.hpp:42
virtual bool read_next_block()
#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.
STL namespace.
unsigned char m_block[block_size]
If we&#39;re reading blocks, the block data.
Definition: reader.hpp:106
std::istream * m_stream
The C++ stream to read from.
Common defs needed everywhere and, as far as is possible, platform specific changes.
The POSIX read() system call failed.
The stream_reader class.
int m_block_index
If we&#39;re reading blocks, the index into the current block.
Definition: reader.hpp:112
static const int block_size
Amount of data to read at once when in block read mode.
Definition: reader.hpp:47
The json_io_exception class.
int m_block_num_bytes
If we&#39;re reading blocks, the number of bytes in the current block.
Definition: reader.hpp:109