Argo  1.0
A C++ library for handling JSON.
fd_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 
23 #include <unistd.h>
24 #include <errno.h>
25 
26 #include "common.hpp"
27 #include "fd_reader.hpp"
28 #include "json_io_exception.hpp"
29 
30 using namespace std;
31 using namespace NAMESPACE;
32 
34 
35 fd_reader::fd_reader(int fd, int max_message_length, bool block_read) :
36  reader(max_message_length, block_read),
37  m_fd(fd)
38 {
39 }
40 
42 {
43  char buf[1];
44  ssize_t n = read(m_fd, buf, 1);
45  if (n == 0)
46  {
47  return EOF;
48  }
49  else if (n == 1)
50  {
51  return buf[0];
52  }
53  else if (n == -1)
54  {
56  }
57  else
58  {
60  }
61 }
62 
64 {
65  ssize_t n = read(m_fd, m_block, block_size);
66 
67  if (n == 0)
68  {
69  return false;
70  }
71  else if (n == -1)
72  {
74  }
75  else
76  {
77  m_block_num_bytes = static_cast<int>(n);
78  m_block_index = 0;
79  return true;
80  }
81 }
The POSIX read() system call returned an unexpected value.
int m_fd
Underlying POSIX file descriptor.
Definition: fd_reader.hpp:63
A class to read JSON messages from various types of input stream.
Definition: reader.hpp:42
The fd_reader class.
#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
Common defs needed everywhere and, as far as is possible, platform specific changes.
The POSIX read() system call failed.
virtual bool read_next_block()
Definition: fd_reader.cpp:63
virtual int read_next_char()
Definition: fd_reader.cpp:41
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