AbstractCsvIterator.qc.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
// Qore AbstractCsvIterator class definition
/* AbstractCsvIterator.qc Copyright 2012 - 2020 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.
*/
// assume local var scope, do not use "$" for vars, members, and method calls
namespace CsvUtil {
class AbstractCsvIterator : public Qore::AbstractIterator, protected CsvHelper {
public:
protected:
const Options = {
"compat_force_empty_string": C_OPT1|C_OPT2,
"date_format": C_OPT1|C_OPT2,
"date-format": C_OPT1|C_OPT2,
"encoding": C_OPT1|C_OPT2,
"eol": C_OPT1|C_OPT2,
"extended_record": C_OPT2,
"fields": C_OPT1,
"header-lines": C_OPT1|C_OPT2,
"header_lines": C_OPT1|C_OPT2,
"header-names": C_OPT1|C_OPT2,
"header_names": C_OPT1|C_OPT2,
"header_reorder": C_OPT1|C_OPT2,
"headers": C_OPT1,
"ignore-empty": C_OPT1|C_OPT2,
"ignore_empty": C_OPT1|C_OPT2,
"ignore-whitespace": C_OPT1|C_OPT2,
"ignore_whitespace": C_OPT1|C_OPT2,
"number_format": C_OPT1|C_OPT2,
"quote": C_OPT1|C_OPT2,
"separator": C_OPT1|C_OPT2,
"timezone": C_OPT1|C_OPT2,
"tolwr": C_OPT1|C_OPT2,
"verify-columns": C_OPT1|C_OPT2,
"verify_columns": C_OPT1|C_OPT2,
};
// field separator
string separator = ",";
// field content delimiter
string quote = "\"";
// number of header lines
softint headerLines = 0;
// flag to use string names from the first header row if possible
bool headerNames = False;
// True if empty lines should be ignored
bool ignoreEmptyLines = True;
// Flag to trim the field content (trim leading and trailing whitespace) from unquoted fields
bool ignoreWhitespace = True;
// the @ref Qore::TimeZone to use when parsing dates (default: current time zone)
*TimeZone timezone;
// verify the column count for every row; if a row does not match, then throw a \c CSVFILEITERATOR-DATA-ERROR exception
bool checkElementCounts = False;
// getRecord/getValue returns extended hash
bool extendedRecord = False;
// force "*string" fields with no value to return an empty string rather than @ref nothing for backwards compatibility with very early versions of CsvUtil
bool compat_force_empty_string = False;
// read ahead flag
bool read_ahead;
// column count for verifying column counts
int cc;
// current record count for the index() method
int rc = 0;
// to resolve record type by rules
hash m_resolve_by_rule;
// to resolve record type by number of fields
hash m_resolve_by_count;
// list of idx to field transformarions, in order of spec
hash m_resolve_by_idx;
// fake specs based on the first non-header row
bool fakeHeaderNames;
// data source iterator
AbstractLineIterator lineIterator;
public:
constructor(AbstractLineIterator li, *hash opts);
// NOTE: when declared as *hash then always calls this constructor
constructor(AbstractLineIterator li, hash spec, hash opts);
protected:
processCommonOptions(*hash opts, int C_OPTx);
public:
protected:
processSpec(hash spec);
public:
protected:
prepareFieldsFromHeaders(*list headers);
public:
bool valid();
bool next();
peek();
auto memberGate(string name);
hash<auto> getValue();
hash<auto> getRecord(bool extended);
hash<auto> getRecord();
auto getRecordList();
string getSeparator();
string getQuote();
*hash<string, AbstractDataField> getRecordType();
*list<string> getHeaders();
*list<string> getHeaders(string type);
int index();
int lineNumber();
string getRawLine();
list<*string> getRawLineValues();
protected:
auto handleType(hash<auto> fh, *string val);
public:
protected:
list<*string> getLineAndSplit();
public:
string identifyType(list<auto> rec);
protected:
*string identifyTypeImpl(list<auto> rec);
public:
protected:
hash<auto> parseLine();
public:
}; // AbstractCsvIterator class
}; // CsvUtil namespace
CsvUtil::AbstractCsvIterator::processSpec
processSpec(hash spec)
process specification and assing internal data for resolving
CsvUtil::AbstractCsvIterator::getRecordList
auto getRecordList()
Returns the current record as a list.
CsvUtil::AbstractCsvIterator::lineNumber
int lineNumber()
Returns the current iterator line number in the file (the first line is line 1) or 0 if not pointing ...
CsvUtil::AbstractCsvIterator::next
bool next()
Moves the current line / record position to the next line / record; returns False if there are no mor...
CsvUtil::AbstractCsvIterator::Options
const Options
valid options for the object (a hash for quick lookups of valid keys)
Definition: AbstractCsvIterator.qc.dox.h:278
type
string type(auto arg)
CsvUtil::AbstractCsvIterator::getQuote
string getQuote()
Returns the current quote string.
CsvUtil::AbstractCsvIterator
the AbstractCsvIterator class is an abstract base class that allows abstract CSV data to be iterated
Definition: AbstractCsvIterator.qc.dox.h:273
CsvUtil::AbstractCsvIterator::index
int index()
Returns the row index being iterated, which does not necessarily correspond to the line number when t...
CsvUtil::AbstractCsvIterator::getHeaders
*list< string > getHeaders()
Returns the current record headers or NOTHING if no headers have been detected or saved yet.
CsvUtil::AbstractCsvIterator::getRawLineValues
list< *string > getRawLineValues()
Returns the list of raw string values of the current line.
CsvUtil::AbstractCsvIterator::getRecord
hash< auto > getRecord()
Returns the current record as a hash.
True
const True
CsvUtil
the CsvUtil namespace. All classes used in the CsvUtil module should be inside this namespace
Definition: AbstractCsvIterator.qc.dox.h:28
CsvUtil::AbstractCsvIterator::identifyTypeImpl
*string identifyTypeImpl(list< auto > rec)
Identify a input record, given the raw line string. This method performs a lookup to a precalculated ...
CsvUtil::AbstractCsvIterator::getValue
hash< auto > getValue()
Returns the current record as a hash.
CsvUtil::AbstractCsvIterator::getHeaders
*list< string > getHeaders(string type)
Returns a list of headers for the given record or NOTHING if the record is not recognized.
CsvUtil::AbstractCsvIterator::getSeparator
string getSeparator()
Returns the current separator string.
CsvUtil::AbstractCsvIterator::identifyType
string identifyType(list< auto > rec)
Identify a fixed-length line type using identifyTypeImpl(); may be overridden if necessary.
list
list< auto > list(...)
hash
hash< auto > hash(object obj)
CsvUtil::AbstractCsvIterator::getRecord
hash< auto > getRecord(bool extended)
Returns the current record as a hash.
CsvUtil::AbstractCsvIterator::getRawLine
string getRawLine()
Returns the current line 'as it is', i.e. the original string.
CsvUtil::AbstractCsvIterator::getRecordType
*hash< string, AbstractDataField > getRecordType()
Returns the description of the record type, if any.
False
const False
CsvUtil::AbstractCsvIterator::prepareFieldsFromHeaders
prepareFieldsFromHeaders(*list headers)
match headers provided at csv header or in options, never called for multi-type because header_names ...
CsvUtil::AbstractCsvIterator::peek
peek()
Reads a single row without moving the index position.
CsvUtil::AbstractCsvIterator::constructor
constructor(AbstractLineIterator li, hash spec, hash opts)
creates the AbstractCsvIterator with an option hash in multi-type mode
CsvUtil::AbstractCsvIterator::constructor
constructor(AbstractLineIterator li, *hash opts)
creates the AbstractCsvIterator with an option hash in single-type mode
CsvUtil::AbstractCsvIterator::parseLine
hash< auto > parseLine()
Parses a line in the file and returns a processed list of the fields.
Qore::AbstractIterator
CsvUtil::AbstractCsvIterator::memberGate
auto memberGate(string name)
Returns the given column value for the current row.
CsvUtil::AbstractCsvIterator::getLineAndSplit
list< *string > getLineAndSplit()
Read line split by separator/quote into list.
CsvUtil::AbstractCsvIterator::processCommonOptions
processCommonOptions(*hash opts, int C_OPTx)
process common options and and assing internal fields