Qore CsvUtil Module Reference  1.0
 All Classes Namespaces Functions Variables Groups Pages
CsvUtil.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file CsvUtil.qm Qore user module for working with CSV files
3 
4 /* CsvUtil.qm Copyright 2012 Qore Technologies, sro
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // this module requires Qore 0.8.6 or better
26 
27 // turn on perl-style boolean evaluation
28 
29 
30 /* Version History
31  * 2012-10-13 v1.0: David Nichols <david@qore.org>: initial version of module
32 */
33 
99 // private class used to iterate a list and skip elements without any value
100 class ListValueIterator : public ListIterator {
101 
102 public:
103  constructor(*list l);
104 
105 
106  bool next();
107 
108 };
109 
111 namespace CsvUtil {
113 
247 
248 public:
249  public :
251  const Options = (
252  "encoding": True,
253  "separator": True,
254  "quote": True,
255  "eol": True,
256  "ignore-empty": True,
257  "ignore-whitespace": True,
258  "header-lines": True,
259  "header-names": True,
260  "headers": True,
261  "verify-columns": True,
262  "fields": True,
263  "timezone": True,
264  );
265 
267  const Types = (
268  "int": True,
269  "float": True,
270  "number": True,
271  "string": True,
272  "date": True,
273  );
274 
276  const FieldAttrs = ("type", "format", "timezone", "code");
277 
278 public:
279 
280  private :
281  // field separator
282  string separator = ",";
283 
284  // field content delimiter
285  string quote = "\"";
286 
287  // number of header lines
288  softint headerLines = 0;
289 
290  // flag to use string names from the first header row if possible
291  bool headerNames = False;
292 
293  // True if empty lines should be ignored
294  bool ignoreEmptyLines = True;
295 
296  // Flag to trim the field content (trim leading and trailing whitespace) from unquoted fields
297  bool ignoreWhitespace = True;
298 
299  // headers / column names for lines iterated
300  *softlist headers;
301 
302  // hash of field information (types, formats, and possible code), hash key = column name or number (starting with 0)
303  *hash fields;
304 
305  // list of field descriptions (from fields, ordered when headers are set)
306  *list fdesc;
307 
308  // the @ref Qore::TimeZone to use when parsing dates (default: current time zone)
309  *TimeZone tz;
310 
311  // verify the column count for every row; if a row does not match, then throw a \c CSVFILEITERATOR-DATA-ERROR exception
312  bool checkElementCounts = False;
313 
314 public:
315 
317 
322  constructor(string path, *hash opts);
323 
324 
326 
339  bool next();
340 
341 
343 
350  any memberGate(string name);
351 
352 
354 
365  any getValue();
366 
367 
369 
378  hash getRecord();
379 
380 
382 
392 
393 
395 
402  string getSeparator();
403 
404 
406 
413  string getQuote();
414 
415 
417 
423  *list getHeaders();
424 
425 
427 
428 private:
429  list parseLine();
430 public:
431 
432 
434 
435 private:
436  setFields();
437 public:
438 
439 
440  static checkType(string key, string value);
441 
442  }; // CsvFileIterator class
443 }; // CsvUtil namespace