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);
147 int64 rc = readData(xsink, &c, 1,
false);
153 return str->empty() ? 0 : q_remove_bom_utf16(str.release(),
enc);
158 if ((**eolstr)[eolpos] == c) {
160 if (eolpos == eolstr->size()) {
162 str->terminate(str->size() - eolpos);
163 return q_remove_bom_utf16(str.release(),
enc);
168 for (
size_t i = eolpos; i; --i) {
171 if (!memcmp(eolstr->c_str(), str->c_str() + str->size() - i, i)) {
189 int64 rc = readData(xsink, &c, 1,
false);
194 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"%d byte%s read of null-terminated string; end of "
195 "stream encountered without a null", (
int)str->size(), str->size() == 1 ?
"" :
"s");
204 return str.release();
212 if (str->size() < size) {
213 xsink->
raiseException(
"END-OF-STREAM-ERROR", QLLD
" byte%s read of " QLLD
"-byte string; end of stream "
214 "encountered", str->size(), str->size() == 1 ?
"" :
"s", size);
217 assert(str->size() == size);
218 return str.release();
226 int64 rc = readData(xsink, &c, 1,
false);
230 return str->empty() ? 0 : str.release();
236 return str.release();
237 }
else if (c ==
'\r') {
240 int64 p = peek(xsink);
244 readData(xsink, &c, 1);
246 str->concat((
char)p);
248 return str.release();
256 if (readData(xsink, &i, 1) < 0) {
264 if (readData(xsink, &i, 2) < 0)
272 if (readData(xsink, &i, 4) < 0)
280 if (readData(xsink, &i, 8) < 0)
288 if (readData(xsink, &i, 2) < 0)
296 if (readData(xsink, &i, 4) < 0)
304 if (readData(xsink, &i, 8) < 0)
312 if (readData(xsink, &i, 1) < 0) {
319 unsigned short i = 0;
320 if (readData(xsink, &i, 2) < 0)
328 if (readData(xsink, &i, 4) < 0)
335 unsigned short i = 0;
336 if (readData(xsink, &i, 2) < 0)
344 if (readData(xsink, &i, 4) < 0)
356 int64 rc = peek(xsink);
360 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"there is not enough data available in the stream; "
361 "1 byte was requested, and 0 were read");
383 return readData(xsink, dest, limit, require_all);
386 DLLLOCAL
virtual const char* getName()
const {
return "StreamReader"; }
390 static const int STREAMREADER_BUFFER_SIZE = 4096;
410 char* destPtr =
static_cast<char*
>(dest);
420 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"there is not enough data available in the stream; "
421 QSD
" byte%s requested, but only " QSD
" could be read", limit,
422 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:550
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:249
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:382
const QoreEncoding * enc
Encoding of the source input stream.
Definition: StreamReader.h:396
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:393
int64 peekCheck(ExceptionSink *xsink)
Peeks the next byte from the input stream.
Definition: StreamReader.h:355
use this class to manage strings where the character encoding must be specified and may be different ...
Definition: QoreString.h:1104
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:79
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