Isis Developer Reference
|
CSV Parser seperates fields (tokens) from a string with a delimeter. More...
#include <CSVReader.h>
Public Types | |
typedef TokenStore | TokenType |
Token storage type. | |
typedef TNT::Array1D< TokenType > | TokenList |
List of tokens. | |
Public Member Functions | |
CSVParser () | |
Default constructor | |
virtual | ~CSVParser () |
Destructor. | |
CSVParser (const QString &str, const char &delimiter=',', bool keepEmptyParts=true) | |
Constructor that parses strings according to given parameters. | |
int | size () const |
Returns the number of tokens in the parsed string. | |
const TokenType & | operator() (const int nth) const |
Returns the nth token in the parsed string. | |
int | parse (const QString &str, const char &delimiter=',', bool keepEmptyParts=true) |
Parser method accepting string, delimiter and multiple token handling. | |
TokenList | result () const |
Returns the list of tokens. | |
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.
typedef TNT::Array1D<TokenType> Isis::CSVParser< TokenStore >::TokenList |
List of tokens.
typedef TokenStore Isis::CSVParser< TokenStore >::TokenType |
Token storage type.
|
inline |
Default constructor
|
inlinevirtual |
Destructor.
|
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) |
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. |
|
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) |
Referenced by Isis::CSVParser< TokenStore >::CSVParser(), Isis::CSVReader::getColumn(), and Isis::CSVReader::getTable().
|
inline |
Returns the list of tokens.
Referenced by Isis::CSVReader::getTable().
|
inline |
Returns the number of tokens in the parsed string.
Referenced by Isis::CSVReader::getColumn().