Argo  1.0
A C++ library for handling JSON.
file_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 <errno.h>
26 
27 #include "common.hpp"
28 #include "file_reader.hpp"
29 #include "json_io_exception.hpp"
30 
31 using namespace std;
32 using namespace NAMESPACE;
33 
34 
35 file_reader::file_reader(FILE *f, int max_message_length, bool block_read) :
36  reader(max_message_length, block_read),
37  m_file(f)
38 {
39 }
40 
42 {
43  int c = fgetc(m_file);
44 
45  if (c == EOF && ferror(m_file))
46  {
48  }
49  else
50  {
51  return c;
52  }
53 }
54 
56 {
57  size_t n = fread(m_block, 1, block_size, m_file);
58 
59  if (n > 0)
60  {
61  m_block_num_bytes = static_cast<int>(n);
62  m_block_index = 0;
63  return true;
64  }
65  else
66  {
67  if (ferror(m_file))
68  {
70  }
71  else
72  {
73  return false;
74  }
75  }
76 }
A class to read JSON messages from various types of input stream.
Definition: reader.hpp:42
#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
virtual bool read_next_block()
Definition: file_reader.cpp:55
The file_reader class.
Common defs needed everywhere and, as far as is possible, platform specific changes.
virtual int read_next_char()
Definition: file_reader.cpp:41
The POSIX read() system call failed.
FILE * m_file
File to read from.
Definition: file_reader.hpp:65
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.
The stdio fgetc call failed in an unexpected way.
int m_block_num_bytes
If we&#39;re reading blocks, the number of bytes in the current block.
Definition: reader.hpp:109