Qore Programming Language 2.1.1
Loading...
Searching...
No Matches
DataLineIterator.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 DataLineIterator.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_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
45public:
46 DLLLOCAL DataLineIterator(ExceptionSink* xsink, const QoreStringNode* n_str,
47 const QoreStringNode* n_eol = nullptr, bool n_trim = true) :
48 str(n_str->stringRefSelf()),
49 eol(n_eol ? n_eol->stringRefSelf() : nullptr),
50 trim(n_trim) {
51 doReset(xsink);
52 }
53
54 DLLLOCAL DataLineIterator(ExceptionSink* xsink, const DataLineIterator& old) :
55 str(old.str->stringRefSelf()),
56 eol(old.eol ? old.eol->stringRefSelf() : nullptr),
57 trim(old.trim) {
58 doReset(xsink);
59 }
60
61 DLLLOCAL ~DataLineIterator() {
62 }
63
64 DLLLOCAL bool next(ExceptionSink* xsink) {
65 bool validp = src->next(xsink);
66 if (!validp) {
67 doReset(xsink);
68 }
69 return validp;
70 }
71
72 DLLLOCAL int64 index() {
73 return src->index();
74 }
75
76 DLLLOCAL QoreStringNode* getValue() {
77 return src->getValue();
78 }
79
80 DLLLOCAL bool valid() {
81 return src->valid();
82 }
83
84 DLLLOCAL int checkValid(ExceptionSink* xsink) {
85 return src->checkValid(xsink);
86 }
87
88 DLLLOCAL void reset(ExceptionSink* xsink) {
89 if (src->valid()) {
90 doReset(xsink);
91 }
92 }
93
94 DLLLOCAL const QoreEncoding* getEncoding() {
95 return src->getEncoding();
96 }
97
98 DLLLOCAL virtual void deref() {
99 if (ROdereference())
100 delete this;
101 }
102
103 DLLLOCAL virtual const char* getName() const { return "DataLineIterator"; }
104
105 DLLLOCAL virtual const QoreTypeInfo* getElementType() const {
106 return stringTypeInfo;
107 }
108
109private:
110 DLLLOCAL void doReset(ExceptionSink* xsink) {
111 if (!str->getEncoding()->isAsciiCompat()) {
113 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
119private:
123 bool trim;
124};
125
126#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:98
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:50
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"
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::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:266