cStringTokenizer Class Reference
[Utility classes]

String tokenizer class, modelled after strtok(). More...

#include <cstringtokenizer.h>

List of all members.

Public Member Functions

 cStringTokenizer (const char *str, const char *delimiters=NULL)
 cStringTokenizer (const cStringTokenizer &other)
 ~cStringTokenizer ()
cStringTokenizeroperator= (const cStringTokenizer &other)
void setDelimiter (const char *s)
bool hasMoreTokens ()
const char * nextToken ()
std::vector< std::string > asVector ()
std::vector< int > asIntVector ()
std::vector< double > asDoubleVector ()

Detailed Description

String tokenizer class, modelled after strtok().

It considers the input string to consist of tokens, separated by one or more delimiter characters. Repeated calls to nextToken() will enumerate the tokens in the string, returning NULL after the last token. The function hasMoreTokens() can be used to find out whether there are more tokens without consuming one.

Limitations: this class does not honor quotes, apostrophes or backslash quoting; nor does it return empty tokens if it encounters multiple delimiter characters in a row (so setting the delimiter to "," does not produce the desired results). This behaviour is consistent with strtok().

Example 1:

 const char *str = "one two three four";
 cStringTokenizer tokenizer(str);
 while (tokenizer.hasMoreTokens())
     ev << " [" << tokenizer.nextToken() << "]";
 

Example 2:

 const char *str = "42 13 46 72 41";
 std::vector<int> array = cStringTokenizer(str).asIntVector();
 

Constructor & Destructor Documentation

cStringTokenizer::cStringTokenizer ( const char *  str,
const char *  delimiters = NULL 
)

Constructor.

The class will make its own copy of the input string and of the delimiters string. The delimiters default to all whitespace characters (space, tab, CR, LF, FF).

cStringTokenizer::cStringTokenizer ( const cStringTokenizer other  ) 

Copy constructor.

It copies the current state of the tokenizer (i.e. does not re-position it to the first token.)


Member Function Documentation

bool cStringTokenizer::hasMoreTokens (  ) 

Returns true if there're more tokens (i.e.

the next nextToken() call won't return NULL).

const char* cStringTokenizer::nextToken (  ) 

Returns the next token.

The returned pointers will stay valid as long as the tokenizer object exists. If there're no more tokens, a NULL pointer will be returned.

cStringTokenizer& cStringTokenizer::operator= ( const cStringTokenizer other  ) 

Assignment operator.

It copies the current state of the tokenizer (i.e. does not re-position it to the first token.)

void cStringTokenizer::setDelimiter ( const char *  s  ) 

Change delimiters.

This allows for switching delimiters during tokenization.


The documentation for this class was generated from the following file:
Generated on Tue Dec 2 11:16:30 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3