Qore Programming Language  1.12.0
BufferedStreamReader.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  BufferedStreamReader.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2022 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_BUFFEREDSTREAMREADER_H
33 #define _QORE_BUFFEREDSTREAMREADER_H
34 
35 #include <cstdint>
36 
37 #include "qore/qore_bitopts.h"
38 #include "qore/InputStream.h"
39 #include "qore/intern/StreamReader.h"
40 
41 // this corresponds to the Qore constant size in QC_BufferedStreamReader; these values must be identical
42 #define DefaultStreamBufferSize 4096
43 
46 public:
47  DLLLOCAL BufferedStreamReader(ExceptionSink* xsink, InputStream* is, const QoreEncoding* encoding, int64 bufsize = DefaultStreamBufferSize) :
48  StreamReader(xsink, is, encoding),
49  bufCapacity((size_t)bufsize),
50  bufCount(0),
51  buf(0) {
52  if (bufsize <= 0) {
53  xsink->raiseException("STREAM-BUFFER-ERROR", "the buffer size must be > 0 (value provided: " QLLD ")", bufsize);
54  return;
55  }
56 
57  // +1 is added to the real capacity for terminating null-character.
58  buf = new char[bufCapacity + 1];
59  }
60 
61  DLLLOCAL virtual ~BufferedStreamReader() {
62  if (buf)
63  delete [] buf;
64  }
65 
66  DLLLOCAL virtual const char* getName() const override { return "BufferedStreamReader"; }
67 
68 private:
70 
77  DLLLOCAL virtual qore_offset_t readData(ExceptionSink* xsink, void* dest, size_t limit, bool require_all = true) override {
78  assert(dest);
79  assert(limit);
80 
81  char* destPtr = static_cast<char*>(dest);
82  size_t read = 0;
83 
84  if (bufCount) {
85  read = QORE_MIN(limit, bufCount);
86  memmove(destPtr, buf, read);
87  if (limit < bufCount) {
88  assert(limit == read);
89  shiftBuffer(limit);
90  return limit;
91  }
92  bufCount = 0;
93  if (!(limit - read))
94  return read;
95  }
96 
97  // read in data directly into the target buffer until the amount left to read >= 1/2 the buffer capacity
98  while (true) {
99  size_t to_read = limit - read;
100  if (to_read <= (bufCapacity / 2))
101  break;
102  int64 rc = in->read(destPtr + read, to_read, xsink);
103  if (*xsink)
104  return -1;
105  if (!rc) {
106  if (require_all) {
107  xsink->raiseException("END-OF-STREAM-ERROR", "there is not enough data available in the stream; "
108  QSD " bytes were requested, and " QSD " were read", limit, read);
109  return -1;
110  }
111  break;
112  }
113  to_read -= rc;
114  read += rc;
115  }
116 
117  // here we try to populate the buffer first and the target second
118  while (true) {
119  size_t to_read = limit - read;
120  if (!to_read)
121  break;
122 
123  assert(!bufCount);
124  int64 rc = fillBuffer(bufCapacity, xsink);
125  if (*xsink)
126  return -1;
127  if (!rc) {
128  if (require_all) {
129  xsink->raiseException("END-OF-STREAM-ERROR", "there is not enough data available in the stream; " QSD " bytes were requested, and " QSD " were read", limit, read);
130  return -1;
131  }
132  break;
133  }
134  assert(rc > 0);
135  size_t len = QORE_MIN((size_t)rc, to_read);
136  memcpy(destPtr + read, buf, len);
137  shiftBuffer(len);
138  read += len;
139  assert(((limit - read) && !bufCount) || !(limit - read));
140  }
141 
142  return read;
143  }
144 
150  virtual int64 peek(ExceptionSink* xsink) override {
151  if (!bufCount) {
152  int rc = fillBuffer(bufCapacity, xsink);
153  if (!rc)
154  return -1;
155  if (rc < 0)
156  return -2;
157  }
158  return buf[0];
159  }
160 
162  DLLLOCAL int64 fillBuffer(size_t bytes, ExceptionSink* xsink) {
163  assert(bytes);
164  assert(bufCount + bytes <= bufCapacity);
165  int64 rc = in->read(buf + bufCount, bytes, xsink);
166  if (*xsink)
167  return 0;
168  bufCount += rc;
169  return rc;
170  }
171 
172  DLLLOCAL bool prepareEnoughData(size_t bytes, ExceptionSink* xsink) {
173  if (bytes > bufCapacity) {
174  xsink->raiseException("STREAM-BUFFER-ERROR", "a read of " QSD " bytes was attempted on a BufferedStreamReader with a capacity of " QSD " bytes", bytes, bufCapacity);
175  return false;
176  }
177  if (bufCount < bytes) {
178  while (true) {
179  int64 rc = fillBuffer(bytes - bufCount, xsink);
180  if (*xsink) {
181  return false;
182  }
183  if (rc == 0) {
184  xsink->raiseException("END-OF-STREAM-ERROR", "a read of " QSD " bytes cannot be performed because there is not enough data available in the stream to satisfy the request", bytes);
185  return false;
186  }
187  if (bufCount >= bytes)
188  break;
189  }
190  }
191  return true;
192  }
193 
194  DLLLOCAL void shiftBuffer(size_t bytes) {
195  assert(bytes <= bufCount && bytes > 0);
196  bufCount -= bytes;
197  memmove(buf, buf+bytes, bufCount);
198  }
199 
201 
205  DLLLOCAL const char* findEolInBuffer(const QoreStringNode* eol, size_t& eolLen, bool endOfStream, char& pmatch) const {
206  pmatch = '\0';
207  if (eol) {
208  const char* p = strstr(buf, eol->getBuffer());
209  eolLen = eol->strlen();
210  return p;
211  }
212  else {
213  const char* p = strpbrk(buf, "\n\r"); // Find first occurence of '\n' or '\r'.
214  if (p) { // Found end of line.
215  if (*p == '\n') {
216  eolLen = 1;
217  }
218  else { // *p == '\r'
219  if (*(p+1) == '\n') {
220  eolLen = 2;
221  }
222  // '\r' is the last character in the buffer, '\n' could be next in the stream.
223  // Unless this is the end of the stream.
224  else if (static_cast<size_t>(p - buf + 1) == bufCount) {
225  eolLen = 1;
226  if (!endOfStream) {
227  pmatch = *p;
228  p = 0;
229  }
230  }
231  else {
232  eolLen = 1;
233  }
234  }
235  }
236  else {
237  eolLen = 0;
238  }
239  return p;
240  }
241  }
242 
243 private:
244  size_t bufCapacity;
245  size_t bufCount;
246  char* buf;
247 };
248 
249 #endif // _QORE_BUFFEREDSTREAMREADER_H
#define QORE_MIN(a, b)
macro to return the minimum of 2 numbers
Definition: QoreLib.h:550
Private data for the Qore::BufferedStreamReader class.
Definition: BufferedStreamReader.h:45
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
Interface for private data of input streams.
Definition: InputStream.h:44
virtual int64 read(void *ptr, int64 limit, ExceptionSink *xsink)=0
Reads up to `limit` bytes from the input stream.
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
DLLEXPORT size_t strlen() const
returns number of bytes in the string (not including the null pointer)
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
Private data for the Qore::StreamReader class.
Definition: StreamReader.h:45
virtual DLLLOCAL qore_offset_t read(ExceptionSink *xsink, void *dest, size_t limit, bool require_all=true)
Read data until a limit.
Definition: StreamReader.h:334
ReferenceHolder< InputStream > in
Source input stream.
Definition: StreamReader.h:345
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260