-
Notifications
You must be signed in to change notification settings - Fork 9
/
FixedLengthReadDataProvider.qc
172 lines (153 loc) · 8.22 KB
/
FixedLengthReadDataProvider.qc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- mode: qore; indent-tabs-mode: nil -*-
#! @file FixedLengthReadDataProvider.qc Qore user module for working with files with fixed lines length
/* FixedLengthReadDataProvider Copyright 2015 - 2022 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#! The FixedLengthUtil namespace contains all the definitions in the FixedLengthUtil module
public namespace FixedLengthUtil {
#! Provides a data provider for reading fixed-length files
/** Supports generic record search operators and options
*/
public class FixedLengthReadDataProvider inherits DataProvider::AbstractDataProvider {
public {
#! Provider info
const ProviderInfo = <DataProviderInfo>{
"type": "FixedLengthReadDataProvider",
"supports_read": True,
"has_record": True,
"constructor_options": ConstructorOptions,
"search_options": GenericRecordSearchOptions,
"search_operators": AbstractDataProvider::GenericRecordSearchOperators,
};
#! Constructor options
const ConstructorOptions = {
"path": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the path to the fixed-length data; mutually exclusive with \"stream\"; resolved with a call "
"to FileLocationHandler::getBinaryStreamFromLocation()",
},
"stream": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(new Type("InputStream")),
"desc": "the input stream for fixed-length data; mutually exclusive with \"path\"",
},
"spec": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(AutoHashType),
"desc": "the fixed-length record specification hash",
"required": True,
},
"date_format": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the default global date format for `date` fields",
},
"encoding": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the encoding for strings",
},
"eol": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the end of line characters",
},
"ignore_empty": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(SoftBoolType),
"desc": "if `true` then empty lines are ignored",
},
"number_format": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the default global number format for `float` or `number` fields",
},
"timezone": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "a string giving a time zone region name or an integer offset in seconds east of UTC",
},
};
#! iteration option list
const FixedLengthReadOptionList = keys (ConstructorOptions - ("path", "stream", "spec"));
}
private {
#! The iterator object
FixedLengthAbstractIterator i;
}
#! Creates the FixedLengthReadDataProvider with the input data path, the record specifications, and optionally an option hash
/** @param path the input file to iterate
@param spec Fixed-length line specification; see @ref fixedlengthspec for more information
@param opts a hash of options; see @ref fixedlengthglobals for more information
@throw FIXED-LENGTH-UTIL-INVALID-SPEC invalid record specification; invalid type or missing field length
*/
constructor(string path, hash<auto> spec, *hash<auto> opts) {
i = new FixedLengthFileIterator(path, spec, opts);
}
#! Creates the FixedLengthReadDataProvider with the string input data stream, the record specifications, and optionally an option hash
/** @param stream the string input stream to iterate
@param spec Fixed-length line specification; see @ref fixedlengthspec for more information
@param opts a hash of options; see @ref fixedlengthglobals for more information
@throw FIXED-LENGTH-UTIL-INVALID-SPEC invalid record specification; invalid type or missing field length
*/
constructor(StringInputStream stream, hash<auto> spec, *hash<auto> opts) {
i = new FixedLengthIterator(stream, spec, opts);
}
#! Creates the FixedLengthReadDataProvider from a binary input data stream, the record specifications, and optionally an option hash
/** @param stream the input stream to iterate
@param encoding the encoding of the string data in the binary input stream
@param spec Fixed-length line specification; see @ref fixedlengthspec for more information
@param opts a hash of options; see @ref fixedlengthglobals for more information
@throw FIXED-LENGTH-UTIL-INVALID-SPEC invalid record specification; invalid type or missing field length
*/
constructor(InputStream stream, *string encoding, hash<auto> spec, *hash<auto> opts) {
i = new FixedLengthIterator(stream, encoding, spec, opts);
}
#! Creates the object from constructor options
constructor(*hash<auto> options) {
*hash<auto> copts = checkOptions("CONSTRUCTOR-ERROR", ConstructorOptions, options);
if (copts.path) {
if (copts.stream) {
error("CONSTRUCTOR-ERROR", "cannot provide both \"stream\" and \"path\" options; use either one or the other");
}
i = new FixedLengthIterator(FileLocationHandler::getBinaryStreamFromLocation(copts.path), copts.encoding,
copts.spec, copts{FixedLengthReadOptionList} - "encoding");
return;
}
if (copts.stream) {
i = new FixedLengthIterator(copts.stream, copts.encoding, copts.spec,
copts{FixedLengthReadOptionList} - "encoding");
return;
}
# create a dummy interator with no input
i = new FixedLengthIterator(new StringInputStream(""), copts.spec, copts{FixedLengthReadOptionList});
}
#! Returns the object name
string getName() {
return i instanceof FixedLengthFileIterator
? cast<FixedLengthFileIterator>(i).getFileName()
: sprintf("stream %s", self.uniqueHash());
}
#! Returns an iterator for zero or more records matching the search options
/** @param where_cond the search criteria
@param search_options the search options after processing by validateSearchOptions()
*/
private AbstractDataProviderRecordIterator searchRecordsImpl(*hash<auto> where_cond, *hash<auto> search_options) {
return new DefaultRecordIterator(i, where_cond, search_options, i.getRecordType(), "record");
}
#! Returns the description of the record type, if any
private *hash<string, AbstractDataField> getRecordTypeImpl(*hash<auto> search_options) {
return i.getRecordType();
}
#! Returns data provider static info
private hash<DataProviderInfo> getStaticInfoImpl() {
return ProviderInfo;
}
}
}