Rudiments
|
#include <csvdom.h>
Protected Member Functions | |
virtual bool | headerStart () |
virtual bool | column (const char *name, bool quoted) |
virtual bool | headerEnd () |
virtual bool | bodyStart () |
virtual bool | recordStart () |
virtual bool | field (const char *value, bool quoted) |
virtual bool | recordEnd () |
virtual bool | bodyEnd () |
The csvdom class implements a minimal CSV DOM parser.
It parses a file or string of CSV-formatted data and produces a dom tree representing the data. It creates an "h" element for the header, "c" element for each column, "r" element for each record, and "f" element for each field. To each "c" and "f" element, it adds a "v" attribute, containing the value, and a "q" element which can contain "y" or "n" indicating whether or not the value is quoted.
For example, the following CSV:
"col1","col2",col3,col4 field11,field12,"field13","field14" "field21",field22,field23,"field24" field31,"field32",field33,field34
would produce the following dom tree:
<h>
</h> <r> <f v="field11" q="n"> <f v="field12" q="n"> <f v="field13" q="y"> <f v="field14" q="y"> </r> <r> <f v="field21" q="y"> <f v="field22" q="n"> <f v="field23" q="n"> <f v="field24" q="y"> </r> <r> <f v="field31" q="n"> <f v="field32" q="y"> <f v="field33" q="n"> <f v="field34" q="n"> </r>
csvdom::csvdom | ( | ) |
Creates a new instance of the csvdom class.
csvdom::csvdom | ( | bool | stringcacheenabled | ) |
Creates a new instance of the csvdom class, allowing the user to enable or disable the "string cache" for this instance.
By default, as each csvdomnode is added to the csvdom tree, tag and attribute names and values are stored in a string cache and pointers into the cache are assigned to each node. Reference counters are kept and the string is removed from the cache when no more nodes are using it. If the data is highly repetitive this conserves memory at the cost of speed.
If the string cache is disabled then memory is allocated in each csvdomnode for names and values and freed when the csvdomnode is freed. This is faster but uses much more memory.
|
virtual |
Deletes this instance of the csvdom class.
Gets called when the end of the body is encountered.
Reimplemented from csvsax.
Gets called when the start of the body is encountered.
Reimplemented from csvsax.
Right-trims and left-trims the name of the column "name". Returns true on success and false if "name" doesn't exist.
bool csvdom::bothTrimColumn | ( | uint64_t | position | ) |
Right-trims and left-trims the name of the column at "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::bothTrimColumns | ( | ) |
Right-trims and left-trims all column names.
Right-trims and left-trims the value at "record","column". Returns true on success and false if no such record/column exists.
bool csvdom::bothTrimField | ( | uint64_t | record, |
uint64_t | column ) |
Right-trims and left-trims the value at "record","column". Returns true on success and false if no such record/column exists.
void csvdom::bothTrimFields | ( | ) |
Right-trims and left-trims all values.
void csvdom::carryAllValuesDown | ( | ) |
For each record, for all fields, if the value for the field in that record is empty, then replace it with the value of the field at the same position from the previous record.
For each record, at the field in column "name", if the value for the field in that record is empty, then replace it with the value of the field at the same position from the previous record.
void csvdom::carryValueDown | ( | uint64_t | position | ) |
For each record, at the field in column "position", if the value for the field in that record is empty, then replace it with the value of the field at the same position from the previous record.
Gets called when a column name is parsed.
Reimplemented from csvsax.
|
virtual |
Creates a new root node. This is useful for building a tree from scratch.
Reimplemented from dom.
Deletes the column named "name". Returns true on success and false if "name" doesn't exist.
bool csvdom::deleteColumn | ( | uint64_t | position | ) |
Deletes the column at "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::deleteEmptyColumns | ( | ) |
Deletes columns composed entirely of empty fields. Returns true on success and false on failure.
bool csvdom::deleteEmptyRecords | ( | ) |
Deletes records composed entirely of empty fields. Returns true on success and false on failure.
bool csvdom::deleteRecord | ( | uint64_t | position | ) |
Deletes the record at "position". Returns true on success and false if "position" doesn't exist.
Gets called when a field is parsed.
Reimplemented from csvsax.
uint64_t csvdom::getColumnCount | ( | ) |
Returns the number of columns in the CSV.
Returns true if the column "name" is composed entirely of empty fields.
bool csvdom::getColumnIsEmpty | ( | uint64_t | position | ) |
Returns true if the column at "position" is composed entirely of empty fields.
Returns the column name of the column at "position". Returns NULL if "position" doesn't exist.
Sets "position" to the position of the column named "name". Returns true on success and false if "name" doesn't exist.
Returns the value at "record","column" or NULL if no such record/column exists.
Returns the value at "record","column" or NULL if no such record/column exists.
domnode * csvdom::getRecord | ( | uint64_t | position | ) |
Returns record at "position".
uint64_t csvdom::getRecordCount | ( | ) |
Returns the number of records in the CSV.
bool csvdom::getRecordIsEmpty | ( | uint64_t | position | ) |
Returns true if the record at "position" is composed entirely of empty fields.
Gets called when the end of the header is encountered.
Reimplemented from csvsax.
Gets called when the start of the header is encountered.
Reimplemented from csvsax.
Inserts a column after column named "name" and names it "newname". Sets the column name to be quoted if "quoted" is true, or false otherwise. If "name" is null or empty then the column name will be set to an empty string. Returns true on success and false if "name" doesn't exist.
Inserts a column after "position" with name "name". Sets the column name to be quoted if "quoted" is true, or false otherwise. If "name" is null or empty then the column name will be set to an empty string. Returns true on success and false if "position" doesn't exist.
Inserts a column before column named "name" and names it "newname". Sets the column name to be quoted if "quoted" is true, or false otherwise. If "name" is null or empty then the column name will be set to an empty string. Returns true on success and false if "name" doesn't exist.
Inserts a column before "position" and names it "name". Sets the column name to be quoted if "quoted" is true, or false otherwise. If "name" is null or empty then the column name will be set to an empty string. Returns true on success and false if "position" doesn't exist.
bool csvdom::insertRecordAfter | ( | uint64_t | position | ) |
Inserts a record after "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::insertRecordBefore | ( | uint64_t | position | ) |
Inserts a record before "position". Returns true on success and false if "position" doesn't exist.
Left-trims the name of the column "name". Returns true on success and false if "name" doesn't exist.
bool csvdom::leftTrimColumn | ( | uint64_t | position | ) |
Left-trims the name of the column at "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::leftTrimColumns | ( | ) |
Left-trims all column names.
Left-trims the value at "record","column". Returns true on success and false if no such record/column exists.
bool csvdom::leftTrimField | ( | uint64_t | record, |
uint64_t | column ) |
Left-trims the value at "record","column". Returns true on success and false if no such record/column exists.
void csvdom::leftTrimFields | ( | ) |
Left-trims all values.
Lower-cases the name of the column "name". Returns true on success and false if "name" doesn't exist.
bool csvdom::lowerCaseColumn | ( | uint64_t | position | ) |
Lower-cases the name of the column at "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::lowerCaseColumns | ( | ) |
Lower-cases all column names.
Moves column named "name" to "position". Returns true on success and false if "name" doesn't exist, or if position is greater than the one more than the last position.
bool csvdom::moveColumn | ( | uint64_t | oldposition, |
uint64_t | newposition ) |
Moves column at "oldposition" to "newposition". Returns true on success and false if "oldposition" doesn't exist or if "newposition" is greater than one more than the last position.
bool csvdom::moveRecord | ( | uint64_t | oldposition, |
uint64_t | newposition ) |
Moves record at "oldposition" to "newposition". Returns true on success and false if "oldposition" doesn't exist or if "newposition" is greater than one more than the last position.
Parses generic input "in" and generates a DOM tree.
Reimplemented from sax.
Parses file "filename" and generates a DOM tree.
Reimplemented from sax.
Parses string "string" and generates a DOM tree.
Reimplemented from sax.
Gets called when the end of a record is encountered.
Reimplemented from csvsax.
Gets called when the start of a record is encountered.
Reimplemented from csvsax.
Renames all columns named any of the names in the NULL-terminated array of names "oldname" to "newname". Sets the column name to be quoted if "quoted" is true, or false otherwise. Returns true on success and false if "oldname" doesn't exist.
Renames all columns named "oldname" to "newname". Sets the column name to be quoted if "quoted" is true, or false otherwise. Returns true on success and false if "oldname" doesn't exist.
Renames the first column named any of the names in the NULL-terminated array of names "oldname" to "newname". Sets the column name to be quoted if "quoted" is true, or false otherwise. Returns true on success and false if "oldname" doesn't exist.
Renames the first column named "oldname" to "newname". Sets the column name to be quoted if "quoted" is true, or false otherwise. Returns true on success and false if "oldname" doesn't exist.
Renames the column at "position" to "name". Sets the column name to be quoted if "quoted" is true, or false otherwise. Returns true on success and false if "position" doesn't exist.
Right-trims the name of the column "name". Returns true on success and false if "name" doesn't exist.
bool csvdom::rightTrimColumn | ( | uint64_t | position | ) |
Right-trims the name of the column at "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::rightTrimColumns | ( | ) |
Right-trims all column names.
Right-trims the value at "record","column". Returns true on success and false if no such record/column exists.
bool csvdom::rightTrimField | ( | uint64_t | record, |
uint64_t | column ) |
Right-trims the value at "record","column". Returns true on success and false if no such record/column exists.
void csvdom::rightTrimFields | ( | ) |
Right-trims all values.
Sets the value at "record","column" to "value" and marks it "quoted". Returns true on success and false if no such record/column exists.
Sets the value at "record","column" to "value" and marks it "quoted". Returns true on success and false if no such record/column exists.
Upper-cases the name of the column "name". Returns true on success and false if "name" doesn't exist.
bool csvdom::upperCaseColumn | ( | uint64_t | position | ) |
Upper-cases the name of the column at "position". Returns true on success and false if "position" doesn't exist.
bool csvdom::upperCaseColumns | ( | ) |
Upper-cases all column names.