Qore Programming Language 2.1.1
Loading...
Searching...
No Matches
InputStreamLineIterator.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 InputStreamLineIterator.h
4
5 Qore Programming Language
6
7 Copyright (C) 2016 - 2024 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_INPUTSTREAMLINEITERATOR_H
33#define _QORE_INPUTSTREAMLINEITERATOR_H
34
35#include <cerrno>
36#include <cstring>
37
38#include "qore/InputStream.h"
39#include "qore/intern/BufferedStreamReader.h"
40#include "qore/intern/StreamReader.h"
41#include "qore/intern/EncodingConversionInputStream.h"
42
47public:
48 DLLLOCAL InputStreamLineIterator(ExceptionSink* xsink, InputStream* is, const QoreEncoding* encoding,
49 const QoreStringNode* n_eol, bool n_trim, size_t bufsize = DefaultStreamBufferSize) :
50 src(is, xsink),
51 reader(xsink),
52 enc(encoding),
53 trim(n_trim) {
54 if (assignEol(n_eol, xsink))
55 return;
56
57 // reference src for assignment to BufferedStreamReader
58 src->ref();
59 reader = new BufferedStreamReader(xsink, *src, enc, bufsize);
60 }
61
62 DLLLOCAL InputStreamLineIterator(ExceptionSink* xsink, StreamReader* sr, const QoreStringNode* n_eol = nullptr,
63 bool n_trim = true) :
64 src(xsink),
65 reader(sr, xsink),
66 enc(sr->getEncoding()),
67 trim(n_trim) {
68 if (assignEol(n_eol, xsink))
69 return;
70
71 // update the stream reader's encoding if necessary
72 if (enc != sr->getEncoding()) {
73 sr->setEncoding(enc);
74 }
75 }
76
77 DLLLOCAL ~InputStreamLineIterator() {
78 }
79
80 DLLLOCAL bool next(ExceptionSink* xsink) {
81 // Make sure to use a new string if the iterator was already valid.
82 if (validp && line && !line->empty()) {
83 line.discard();
84 }
85 validp = getLine(xsink);
86 if (validp) {
87 ++num; // Increment line number.
88 } else {
89 num = 0; // Reset iterator.
90 }
91 //printd(5, "InputStreamLineIterator::next() this: %p line: %d offset: %lld validp: %d '%s'\n", this, num,
92 // offset, validp, line->getBuffer());
93 return validp;
94 }
95
96 DLLLOCAL int64 index() const {
97 return num;
98 }
99
100 DLLLOCAL QoreStringNode* getValue() {
101 assert(validp);
102 return line ? line->stringRefSelf() : nullptr;
103 }
104
105 DLLLOCAL bool valid() const {
106 return validp;
107 }
108
109 DLLLOCAL int checkValid(ExceptionSink* xsink) const {
110 if (!validp) {
111 xsink->raiseException("ITERATOR-ERROR", "the %s is not pointing at a valid element; make sure %s::next() "
112 "returns True before calling this method", getName(), getName());
113 return -1;
114 }
115 return 0;
116 }
117
118 DLLLOCAL StreamReader* getStreamReader() {
119 return *reader;
120 }
121
122 DLLLOCAL const QoreEncoding* getEncoding() const {
123 return enc;
124 }
125
126 DLLLOCAL virtual void deref() {
127 if (ROdereference()) {
128 delete this;
129 }
130 }
131
132 DLLLOCAL virtual const char* getName() const {
133 return "InputStreamLineIterator";
134 }
135
136 DLLLOCAL virtual const QoreTypeInfo* getElementType() const {
137 return stringTypeInfo;
138 }
139
140private:
141 DLLLOCAL int assignEol(const QoreStringNode* n_eol, ExceptionSink* xsink) {
142 if (!n_eol || n_eol->empty()) {
143 return 0;
144 }
145 if (enc != n_eol->getEncoding()) {
146 SimpleRefHolder<QoreStringNode> neol(n_eol->convertEncoding(enc, xsink));
147 if (*xsink) {
148 return -1;
149 }
150 eol = q_remove_bom_utf16(neol.release(), enc);
151 } else {
152 eol = n_eol->stringRefSelf();
153 }
154 return 0;
155 }
156
157 DLLLOCAL bool getLine(ExceptionSink* xsink) {
158 line = reader->readLine(*eol, trim, xsink);
159 return (bool)line;
160 }
161
162private:
165 const QoreEncoding* enc;
168 int64 num = 0;
169 bool validp = false;
170 bool trim;
171};
172
173#endif // _QORE_INPUTSTREAMLINEITERATOR_H
DLLLOCAL void ref() const
increments the reference count of the object
Definition AbstractPrivateData.h:51
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:50
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
Private data for the Qore::InputStreamLineIterator class.
Definition InputStreamLineIterator.h:46
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition InputStreamLineIterator.h:126
defines string encoding functions in Qore
Definition QoreEncoding.h:83
abstract base class for iterator private data
Definition QoreIteratorBase.h:68
DLLEXPORT bool ROdereference() const
Atomically decrements the reference count.
DLLEXPORT const QoreEncoding * getEncoding() const
returns the encoding for the string
DLLEXPORT bool empty() const
returns true if the string is empty, false if not
Qore's string value type, reference counted, dynamically-allocated only.
Definition QoreStringNode.h:50
DLLEXPORT QoreStringNode * convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const
converts the encoding of the string to the specified encoding, returns 0 if an error occurs,...
DLLEXPORT QoreStringNode * stringRefSelf() const
references the object and returns a non-const pointer to "this"
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
DLLLOCAL QoreStringNode * readLine(const QoreStringNode *eol, bool trim, ExceptionSink *xsink)
Read one line.
Definition StreamReader.h:126
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