USGS

Isis 3.0 Object Programmers' Reference

Home

Isis::CSVParser< TokenStore > Class Template Reference
[Parsing]

#include <CSVReader.h>

Collaboration diagram for Isis::CSVParser< TokenStore >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<typename TokenStore = iString>
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 Isis::iString 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 iString/StringTools merge
History:
2008-06-18 Christopher Austin - Fixed documentation

Definition at line 80 of file CSVReader.h.

Public Types

typedef TokenStore TokenType
 Token storage type.
typedef TNT::Array1D< TokenTypeTokenList
 List of tokens.

Public Member Functions

 CSVParser ()
 Default constructor.
virtual ~CSVParser ()
 Destructor.
 CSVParser (const std::string &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 TokenTypeoperator() (const int nth) const
 Returns the nth token in the parsed string.
int parse (const std::string &str, const char &delimiter= ',', bool keepEmptyParts=true)
 Parser method accepting string, delimiter and multiple token handling.
TokenList result () const
 Returns the list of tokens.

Private Attributes

TokenList _elements
 List of tokens parsed from string.


Member Typedef Documentation

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

List of tokens.

Definition at line 83 of file CSVReader.h.

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

Token storage type.

Definition at line 82 of file CSVReader.h.


Constructor & Destructor Documentation

template<typename TokenStore = iString>
Isis::CSVParser< TokenStore >::CSVParser (  )  [inline]

Default constructor.

Definition at line 86 of file CSVReader.h.

template<typename TokenStore = iString>
virtual Isis::CSVParser< TokenStore >::~CSVParser (  )  [inline, virtual]

Destructor.

Definition at line 88 of file CSVReader.h.

template<typename TokenStore = iString>
Isis::CSVParser< TokenStore >::CSVParser ( const std::string &  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:
str iString 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 103 of file CSVReader.h.

References Isis::CSVParser< TokenStore >::parse().


Member Function Documentation

template<typename TokenStore = iString>
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:
nth Indicates 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 122 of file CSVReader.h.

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

template<typename TokenStore = iString>
int Isis::CSVParser< TokenStore >::parse ( const std::string &  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:
str iString 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)
Returns:
int Number of tokens found/parsed in the input string

Definition at line 141 of file CSVReader.h.

References Isis::CSVParser< TokenStore >::_elements, and Isis::iString::Split().

Referenced by Isis::CSVParser< TokenStore >::CSVParser(), Isis::CSVReader::getColumn(), and Isis::CSVReader::getTable().

template<typename TokenStore = iString>
TokenList Isis::CSVParser< TokenStore >::result (  )  const [inline]

Returns the list of tokens.

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 160 of file CSVReader.h.

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

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

template<typename TokenStore = iString>
int Isis::CSVParser< TokenStore >::size (  )  const [inline]

Returns the number of tokens in the parsed string.

Definition at line 109 of file CSVReader.h.

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

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


Member Data Documentation

template<typename TokenStore = iString>
TokenList Isis::CSVParser< TokenStore >::_elements [private]

List of tokens parsed from string.

Definition at line 163 of file CSVReader.h.

Referenced by Isis::CSVParser< TokenStore >::operator()(), Isis::CSVParser< TokenStore >::parse(), Isis::CSVParser< TokenStore >::result(), and Isis::CSVParser< TokenStore >::size().


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