Qore Programming Language 1.16.0
Loading...
Searching...
No Matches
FileLineIterator.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 FileLineIterator.h
4
5 Qore Programming Language
6
7 Copyright (C) 2016 - 2023 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_FILELINEITERATOR_H
33#define _QORE_FILELINEITERATOR_H
34
35#include <cerrno>
36#include <cstring>
37
38#include "qore/intern/FileInputStream.h"
39#include "qore/intern/InputStreamLineIterator.h"
40
45public:
46 DLLLOCAL FileLineIterator(ExceptionSink* xsink, const QoreStringNode* name, const QoreEncoding* enc = QCS_DEFAULT,
47 const QoreStringNode* n_eol = 0, bool n_trim = true, int flags = 0) :
48 eol(n_eol ? n_eol->stringRefSelf() : nullptr),
49 encoding(enc),
50 filename(name->stringRefSelf()),
51 trim(n_trim),
52 flags(flags) {
53 doReset(xsink);
54 }
55
56 DLLLOCAL FileLineIterator(ExceptionSink* xsink, const FileLineIterator& old) :
57 eol(old.eol ? old.eol->stringRefSelf() : nullptr),
58 encoding(old.encoding),
59 filename(old.filename->stringRefSelf()),
60 trim(old.trim),
61 flags(old.flags) {
62 doReset(xsink);
63 }
64
65 DLLLOCAL ~FileLineIterator() {
66 }
67
68 DLLLOCAL bool next(ExceptionSink* xsink) {
69 bool validp = src->next(xsink);
70 if (!validp) {
71 doReset(xsink);
72 }
73 return validp;
74 }
75
76 DLLLOCAL int64 index() {
77 return src->index();
78 }
79
80 DLLLOCAL QoreStringNode* getValue() {
81 return src->getValue();
82 }
83
84 DLLLOCAL bool valid() {
85 return src->valid();
86 }
87
88 DLLLOCAL int checkValid(ExceptionSink* xsink) {
89 return src->checkValid(xsink);
90 }
91
92 DLLLOCAL void reset(ExceptionSink* xsink) {
93 if (src->valid()) {
94 doReset(xsink);
95 }
96 }
97
98 DLLLOCAL const QoreEncoding* getEncoding() {
99 return src->getEncoding();
100 }
101
102 DLLLOCAL const QoreStringNode* getFileName() {
103 return *filename;
104 }
105
106 DLLLOCAL QoreListNode* stat(ExceptionSink* xsink) {
107 return fis->getFile().stat(xsink);
108 }
109
110 DLLLOCAL QoreHashNode* hstat(ExceptionSink* xsink) {
111 return fis->getFile().hstat(xsink);
112 }
113
114 DLLLOCAL bool isTty() {
115 return fis->getFile().isTty();
116 }
117
118 DLLLOCAL virtual void deref() {
119 if (ROdereference())
120 delete this;
121 }
122
123 DLLLOCAL virtual const char* getName() const { return "FileLineIterator"; }
124
125 DLLLOCAL virtual const QoreTypeInfo* getElementType() const {
126 return stringTypeInfo;
127 }
128
129private:
130 DLLLOCAL void doReset(ExceptionSink* xsink) {
131 fis = new FileInputStream(*filename, -1, flags, xsink);
132 if (*xsink)
133 return;
134 fis->ref();
135 if (!encoding->isAsciiCompat())
136 src = new InputStreamLineIterator(xsink, new EncodingConversionInputStream(*fis, encoding, QCS_UTF8, xsink), QCS_UTF8, *eol, trim);
137 else
138 src = new InputStreamLineIterator(xsink, *fis, encoding, *eol, trim);
139 }
140
144 const QoreEncoding* encoding;
146 bool trim;
147 int flags;
148};
149
150#endif // _QORE_FILELINEITERATOR_H
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
DLLEXPORT const QoreEncoding * QCS_UTF8
UTF-8 multi-byte encoding (only UTF-8 and UTF-16 are multi-byte encodings)
Definition: QoreEncoding.h:247
DLLLOCAL void ref() const
increments the reference count of the object
Definition: AbstractPrivateData.h:51
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::FileInputStream class.
Definition: FileInputStream.h:41
Private data for the Qore::FileLineIterator class.
Definition: FileLineIterator.h:44
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: FileLineIterator.h:118
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
DLLEXPORT QoreHashNode * hstat(ExceptionSink *xsink) const
returns a QoreHashNode with file status information
DLLEXPORT QoreListNode * stat(ExceptionSink *xsink) const
returns a QoreListNode with file status information
DLLEXPORT bool isTty() const
returns true if the file is a tty
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
abstract base class for iterator private data
Definition: QoreIteratorBase.h:68
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT bool ROdereference() const
atomically decrements the reference count
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
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