All Classes Namespaces Functions Variables Groups Pages
CsvUtil.qm.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
// @file CsvUtil.qm Qore user module for working with CSV files
/* CsvUtil.qm Copyright 2012 Qore Technologies, sro
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.
*/
// this module requires Qore 0.8.6 or better
// turn on perl-style boolean evaluation
/* Version History
* 2012-10-13 v1.0: David Nichols <david@qore.org>: initial version of module
*/
// private class used to iterate a list and skip elements without any value
class ListValueIterator : public ListIterator {
public:
constructor(*list l);
bool next();
};
namespace CsvUtil {
class CsvFileIterator : public Qore::FileLineIterator {
public:
public :
const Options = (
"encoding": True,
"separator": True,
"quote": True,
"eol": True,
"ignore-empty": True,
"ignore-whitespace": True,
"header-lines": True,
"header-names": True,
"headers": True,
"verify-columns": True,
"fields": True,
"timezone": True,
);
const Types = (
"int": True,
"float": True,
"number": True,
"string": True,
"date": True,
);
const FieldAttrs = ("type", "format", "timezone", "code");
public:
private :
// 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;
// headers / column names for lines iterated
*softlist headers;
// hash of field information (types, formats, and possible code), hash key = column name or number (starting with 0)
*hash fields;
// list of field descriptions (from fields, ordered when headers are set)
*list fdesc;
// the @ref Qore::TimeZone to use when parsing dates (default: current time zone)
*TimeZone tz;
// verify the column count for every row; if a row does not match, then throw a \c CSVFILEITERATOR-DATA-ERROR exception
bool checkElementCounts = False;
public:
constructor(string path, *hash opts);
bool next();
any memberGate(string name);
any getValue();
hash getRecord();
list getRecordList();
string getSeparator();
string getQuote();
*list getHeaders();
private:
list parseLine();
public:
private:
setFields();
public:
static checkType(string key, string value);
}; // CsvFileIterator class
}; // CsvUtil namespace