Isis 3 Programmer Reference
|
CSV Parser seperates fields (tokens) from a string with a delimeter. More...
#include <CSVReader.h>
Public Types | |
typedef TokenStore | TokenType |
Token storage type. More... | |
typedef TNT::Array1D< TokenType > | TokenList |
List of tokens. More... | |
Public Member Functions | |
CSVParser () | |
Default constructor. More... | |
virtual | ~CSVParser () |
Destructor. More... | |
CSVParser (const QString &str, const char &delimiter=',', bool keepEmptyParts=true) | |
Constructor that parses strings according to given parameters. More... | |
int | size () const |
Returns the number of tokens in the parsed string. More... | |
const TokenType & | operator() (const int nth) const |
Returns the nth token in the parsed string. More... | |
int | parse (const QString &str, const char &delimiter=',', bool keepEmptyParts=true) |
Parser method accepting string, delimiter and multiple token handling. More... | |
TokenList | result () const |
Returns the list of tokens. More... | |
Private Attributes | |
TokenList | _elements |
List of tokens parsed from string. More... | |
CSV Parser seperates fields (tokens) from a string with a delimeter.
CSVParser is a lightweigh parser that takes a string as an argument, either through the constructor or a method, and parses the string into tokens that are separated by a single delimiting character, usually a comma. It can work on spaces as well, but typically these types of strings have multiple spaces between them. For these cases, set keepEmptyParts = false, which treats succesive tokens as a single token.
One important note about its token storage mechanism: It uses the TNT 1-D array class which is reference counted. This makes exporting of tokens very efficient at the expense of all instances referring to the same token list. This is subtle but can cause many surprising results when other users change the contents of the tokens. Use the TNT copy() method if you require your own list.
This is a templated class that allows the user to select the token storage type. This type, which defaults to the QString class (I will explain the reason for this shortly), must provide a few features that are explicitly used in providing support for tokenization.
The TokenStore class must provide a default, unparameterized constructor since it will be used as the default initializer.
The class must also accept a string as a constructor option. This class uses a string splitter/tokenizer that returns a vector of strings. It then creates the token array and copies the result to it using an assignment statement from the string-accepted constructor of the TokenStore type.
This token storage class is primarily used in the CSVReader which has additional requirements on the TokenStore type. See the CSVReader class documentation for more details.
2007-06-05 Brendan George - Modified to work with QString/StringTools merge
2008-06-18 Christopher Austin - Fixed documentation
2017-09-22 Cole Neubauer - Fixed documentation. References #4807
2018-10-18 Kaitlyn Lee - Added "[]" around file names in exception messages. References #5520.
Definition at line 85 of file CSVReader.h.
typedef TNT::Array1D<TokenType> Isis::CSVParser< TokenStore >::TokenList |
List of tokens.
Definition at line 88 of file CSVReader.h.
typedef TokenStore Isis::CSVParser< TokenStore >::TokenType |
Token storage type.
Definition at line 87 of file CSVReader.h.
|
inline |
Default constructor.
Definition at line 91 of file CSVReader.h.
|
inlinevirtual |
Destructor.
Definition at line 93 of file CSVReader.h.
|
inline |
Constructor that parses strings according to given parameters.
This constructor accepts a string to parse, the delimiter that separates words and indicate whether successive occurances of the delimiter translates into a single value or they are taken as empty tokens.
str | QString to parse |
delimiter | Character that separates individual tokens in the string |
keepEmptyParts | Specifies the occurance of successive tokens is to treated as one token (false) or each delimiter indicates an empty token (true) |
Definition at line 108 of file CSVReader.h.
References Isis::CSVParser< TokenStore >::parse().
|
inline |
Returns the nth token in the parsed string.
Use of this method and size(), one can iterate through all the tokens in the resulting list using a for loop. Be sure the element exists before attempting to access it.
nth | Indicates the nth value to return - valid range is 0 to n_elements. |
Definition at line 132 of file CSVReader.h.
References Isis::CSVParser< TokenStore >::_elements.
|
inline |
Parser method accepting string, delimiter and multiple token handling.
This method duplicates the behavior of the constructor so that it can be maintained for subsequent use. There is little overhead involved in the construction of this lightweight class, but this allows the same instance to be resued.
str | QString to parse into tokens separated by the delimiter |
delimiter | Character that separates each token in str |
keepEmptyParts | Specifies the occurance of successive tokens is to treated as one token (false) or each delimiter indicates an empty token (true) |
Definition at line 153 of file CSVReader.h.
References Isis::CSVParser< TokenStore >::_elements.
Referenced by Isis::CSVParser< TokenStore >::CSVParser(), Isis::CSVReader::getColumn(), and Isis::CSVReader::getTable().
|
inline |
Returns the list of tokens.
Definition at line 172 of file CSVReader.h.
References Isis::CSVParser< TokenStore >::_elements.
Referenced by Isis::CSVReader::getTable().
|
inline |
Returns the number of tokens in the parsed string.
Definition at line 117 of file CSVReader.h.
References Isis::CSVParser< TokenStore >::_elements.
Referenced by Isis::CSVReader::getColumn().
|
private |
List of tokens parsed from string.
Definition at line 177 of file CSVReader.h.
Referenced by Isis::CSVParser< TokenStore >::operator()(), Isis::CSVParser< TokenStore >::parse(), Isis::CSVParser< TokenStore >::result(), and Isis::CSVParser< TokenStore >::size().