Isis 3 Programmer Reference
Isis::CSVParser< TokenStore > Class Template Reference

CSV Parser seperates fields (tokens) from a string with a delimeter. More...

#include <CSVReader.h>

Collaboration diagram for Isis::CSVParser< TokenStore >:
Collaboration graph

Public Types

typedef TokenStore TokenType
 Token storage type. More...
 
typedef TNT::Array1D< TokenTypeTokenList
 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 TokenTypeoperator() (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...
 

Detailed Description

template<typename TokenStore = QString>
class Isis::CSVParser< TokenStore >

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.

See also
CSVReader
Author
2006-08-14 Kris Becker, USGS
History:

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.

Member Typedef Documentation

◆ TokenList

template<typename TokenStore = QString>
typedef TNT::Array1D<TokenType> Isis::CSVParser< TokenStore >::TokenList

List of tokens.

Definition at line 88 of file CSVReader.h.

◆ TokenType

template<typename TokenStore = QString>
typedef TokenStore Isis::CSVParser< TokenStore >::TokenType

Token storage type.

Definition at line 87 of file CSVReader.h.

Constructor & Destructor Documentation

◆ CSVParser() [1/2]

template<typename TokenStore = QString>
Isis::CSVParser< TokenStore >::CSVParser ( )
inline

Default constructor.

Definition at line 91 of file CSVReader.h.

◆ ~CSVParser()

template<typename TokenStore = QString>
virtual Isis::CSVParser< TokenStore >::~CSVParser ( )
inlinevirtual

Destructor.

Definition at line 93 of file CSVReader.h.

◆ CSVParser() [2/2]

template<typename TokenStore = QString>
Isis::CSVParser< TokenStore >::CSVParser ( const QString &  str,
const char &  delimiter = ',',
bool  keepEmptyParts = true 
)
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.

Parameters
strQString to parse
delimiterCharacter that separates individual tokens in the string
keepEmptyPartsSpecifies 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().

Member Function Documentation

◆ operator()()

template<typename TokenStore = QString>
const TokenType& Isis::CSVParser< TokenStore >::operator() ( const int  nth) const
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.

Parameters
nthIndicates the nth value to return - valid range is 0 to n_elements.
Returns
const TokenType& Reference to the nth token in the parsed list

Definition at line 132 of file CSVReader.h.

References Isis::CSVParser< TokenStore >::_elements.

◆ parse()

template<typename TokenStore = QString>
int Isis::CSVParser< TokenStore >::parse ( const QString &  str,
const char &  delimiter = ',',
bool  keepEmptyParts = true 
)
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.

Parameters
strQString to parse into tokens separated by the delimiter
delimiterCharacter that separates each token in str
keepEmptyPartsSpecifies the occurance of successive tokens is to treated as one token (false) or each delimiter indicates an empty token (true)
Returns
int Number of tokens found/parsed in the input string

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().

◆ result()

template<typename TokenStore = QString>
TokenList Isis::CSVParser< TokenStore >::result ( ) const
inline

Returns the list of tokens.

Returns
This method returns the complete list of tokens. Note that it utilizes the most efficient method of storing and exporting tokens, namely a reference counted array.

Definition at line 172 of file CSVReader.h.

References Isis::CSVParser< TokenStore >::_elements.

Referenced by Isis::CSVReader::getTable().

◆ size()

template<typename TokenStore = QString>
int Isis::CSVParser< TokenStore >::size ( ) const
inline

Returns the number of tokens in the parsed string.

Returns
int

Definition at line 117 of file CSVReader.h.

References Isis::CSVParser< TokenStore >::_elements.

Referenced by Isis::CSVReader::getColumn().

Member Data Documentation

◆ _elements

template<typename TokenStore = QString>
TokenList Isis::CSVParser< TokenStore >::_elements
private

The documentation for this class was generated from the following file: