32#ifndef _QORE_STREAMREADER_H
33#define _QORE_STREAMREADER_H
37#include "qore/qore_bitopts.h"
38#include "qore/InputStream.h"
39#include "qore/intern/StringReaderHelper.h"
42DLLLOCAL
extern QoreClass* QC_STREAMREADER;
48 in(is, xsink),
enc(encoding) {
62 DLLLOCAL
const InputStream* getInputStream()
const {
81 char buffer[STREAMREADER_BUFFER_SIZE];
84 int rc = readData(xsink, buffer, STREAMREADER_BUFFER_SIZE,
false);
89 b->append(buffer, rc);
93 int rc = readData(xsink, buffer,
QORE_MIN(limit, STREAMREADER_BUFFER_SIZE),
false);
98 b->append(buffer, rc);
103 return b->empty() ? 0 : b.release();
114 return q_read_string(xsink, size,
enc, std::bind(&StreamReader::readData,
this, _3, _1, _2,
false));
129 return readLineEol(&nl, trim, xsink);
132 return eol ? readLineEol(eol, trim, xsink) :
readLine(trim, xsink);
148 int64 rc = readData(xsink, &c, 1,
false);
154 return str->empty() ? 0 : q_remove_bom_utf16(str.release(),
enc);
159 if ((**eolstr)[eolpos] == c) {
161 if (eolpos == eolstr->size()) {
163 str->terminate(str->size() - eolpos);
164 return q_remove_bom_utf16(str.release(),
enc);
169 for (
size_t i = eolpos; i; --i) {
172 if (!memcmp(eolstr->c_str(), str->c_str() + str->size() - i, i)) {
190 int64 rc = readData(xsink, &c, 1,
false);
195 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"%d byte%s read of null-terminated string; end of "
196 "stream encountered without a null", (
int)str->size(), str->size() == 1 ?
"" :
"s");
205 return str.release();
213 if (str->size() < size) {
214 xsink->
raiseException(
"END-OF-STREAM-ERROR", QLLD
" byte%s read of " QLLD
"-byte string; end of stream "
215 "encountered", str->size(), str->size() == 1 ?
"" :
"s", size);
218 assert(str->size() == size);
219 return str.release();
227 int64 rc = readData(xsink, &c, 1,
false);
232 return str->empty() ? nullptr : str.release();
239 return str.release();
240 }
else if (c ==
'\r') {
244 int64 p = peek(xsink);
249 readData(xsink, &c, 1);
251 str->concat((
char)p);
254 return str.release();
262 if (readData(xsink, &i, 1) < 0) {
270 if (readData(xsink, &i, 2) < 0)
278 if (readData(xsink, &i, 4) < 0)
286 if (readData(xsink, &i, 8) < 0)
294 if (readData(xsink, &i, 2) < 0)
302 if (readData(xsink, &i, 4) < 0)
310 if (readData(xsink, &i, 8) < 0)
318 if (readData(xsink, &i, 1) < 0) {
325 unsigned short i = 0;
326 if (readData(xsink, &i, 2) < 0)
334 if (readData(xsink, &i, 4) < 0)
341 unsigned short i = 0;
342 if (readData(xsink, &i, 2) < 0)
350 if (readData(xsink, &i, 4) < 0)
362 int64 rc = peek(xsink);
366 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"there is not enough data available in the stream; "
367 "1 byte was requested, and 0 were read");
389 return readData(xsink, dest, limit, require_all);
392 DLLLOCAL
virtual const char* getName()
const {
return "StreamReader"; }
396 static const int STREAMREADER_BUFFER_SIZE = 4096;
416 char* destPtr =
static_cast<char*
>(dest);
426 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"there is not enough data available in the stream; "
427 QSD
" byte%s requested, but only " QSD
" could be read", limit,
428 limit == 1 ?
" was" :
"s were",
read);
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
#define QORE_MIN(a, b)
macro to return the minimum of 2 numbers
Definition QoreLib.h:616
the base class for all data to be used as private data of Qore objects
Definition AbstractPrivateData.h:44
holds arbitrary binary data
Definition BinaryNode.h:41
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition ExceptionSink.h:50
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
defines a Qore-language class
Definition QoreClass.h:310
defines string encoding functions in Qore
Definition QoreEncoding.h:83
DLLEXPORT bool isAsciiCompat() const
returns true if the character encoding is backwards-compatible with ASCII
Qore's string type supported by the QoreEncoding class.
Definition QoreString.h:93
Qore's string value type, reference counted, dynamically-allocated only.
Definition QoreStringNode.h:50
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition ReferenceHolder.h:52
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition ReferenceHolder.h:127
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:388
const QoreEncoding * enc
Encoding of the source input stream.
Definition StreamReader.h:402
DLLLOCAL BinaryNode * readBinary(int64 limit, ExceptionSink *xsink)
Read binary data from the stream.
Definition StreamReader.h:77
DLLLOCAL QoreStringNode * readString(int64 size, ExceptionSink *xsink)
Read string data from the stream.
Definition StreamReader.h:113
DLLLOCAL QoreStringNode * readLine(const QoreStringNode *eol, bool trim, ExceptionSink *xsink)
Read one line.
Definition StreamReader.h:126
ReferenceHolder< InputStream > in
Source input stream.
Definition StreamReader.h:399
int64 peekCheck(ExceptionSink *xsink)
Peeks the next byte from the input stream.
Definition StreamReader.h:361
use this class to manage strings where the character encoding must be specified and may be different ...
Definition QoreString.h:1191
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition common.h:85
intptr_t qore_offset_t
used for offsets that could be negative
Definition common.h:82
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:266