Qore Programming Language  1.12.0
DataLineIterator.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  DataLineIterator.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_DATALINEITERATOR_H
33 #define _QORE_DATALINEITERATOR_H
34 
35 #include <cerrno>
36 #include <cstring>
37 
38 #include "qore/intern/StringInputStream.h"
39 #include "qore/intern/InputStreamLineIterator.h"
40 
45 public:
46  DLLLOCAL DataLineIterator(ExceptionSink* xsink, const QoreStringNode* n_str, const QoreStringNode* n_eol = 0, bool n_trim = true) :
47  src(0),
48  str(n_str->stringRefSelf()),
49  eol(n_eol ? n_eol->stringRefSelf() : 0),
50  trim(n_trim) {
51  doReset(xsink);
52  }
53 
54  DLLLOCAL DataLineIterator(ExceptionSink* xsink, const DataLineIterator& old) :
55  src(0),
56  str(old.str->stringRefSelf()),
57  eol(old.eol ? old.eol->stringRefSelf() : 0),
58  trim(old.trim) {
59  doReset(xsink);
60  }
61 
62  DLLLOCAL ~DataLineIterator() {
63  }
64 
65  DLLLOCAL bool next(ExceptionSink* xsink) {
66  bool validp = src->next(xsink);
67  if (!validp) {
68  doReset(xsink);
69  }
70  return validp;
71  }
72 
73  DLLLOCAL int64 index() {
74  return src->index();
75  }
76 
77  DLLLOCAL QoreStringNode* getValue() {
78  return src->getValue();
79  }
80 
81  DLLLOCAL bool valid() {
82  return src->valid();
83  }
84 
85  DLLLOCAL int checkValid(ExceptionSink* xsink) {
86  return src->checkValid(xsink);
87  }
88 
89  DLLLOCAL void reset(ExceptionSink* xsink) {
90  if (src->valid()) {
91  doReset(xsink);
92  }
93  }
94 
95  DLLLOCAL const QoreEncoding* getEncoding() {
96  return src->getEncoding();
97  }
98 
99  DLLLOCAL virtual void deref() {
100  if (ROdereference())
101  delete this;
102  }
103 
104  DLLLOCAL virtual const char* getName() const { return "DataLineIterator"; }
105 
106  DLLLOCAL virtual const QoreTypeInfo* getElementType() const {
107  return stringTypeInfo;
108  }
109 
110 private:
111  DLLLOCAL void doReset(ExceptionSink* xsink) {
112  if (!str->getEncoding()->isAsciiCompat())
113  src = new InputStreamLineIterator(xsink, new EncodingConversionInputStream(new StringInputStream(*str), str->getEncoding(), QCS_UTF8, xsink), QCS_UTF8, *eol, trim);
114  else
115  src = new InputStreamLineIterator(xsink, new StringInputStream(*str), str->getEncoding(), *eol, trim);
116  }
117 
118 private:
122  bool trim;
123 };
124 
125 #endif // _QORE_DATALINEITERATOR_H
DLLEXPORT const QoreEncoding * QCS_UTF8
UTF-8 multi-byte encoding (only UTF-8 and UTF-16 are multi-byte encodings)
Definition: QoreEncoding.h:247
Private data for the Qore::DataLineIterator class.
Definition: DataLineIterator.h:44
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: DataLineIterator.h:99
Private data for the Qore::EncodingConversionInputStream class.
Definition: EncodingConversionInputStream.h:41
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
Private data for the Qore::InputStreamLineIterator class.
Definition: InputStreamLineIterator.h:46
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
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
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLEXPORT QoreStringNode * stringRefSelf() const
references the object and returns a non-const pointer to "this"
Private data for the Qore::StringInputStream class.
Definition: StringInputStream.h:41
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