StrTokens

Alexander Liss

01/08/2001

Source code of a class StrTokens is in "strtokens.h" and "strtokens.cpp" files. This class uses a class StrBuffer, source code of which is in "strbuf.h" and "strbuf.cpp" files. A class StrTokens takes a string, copies it in an internal StrBuffer and allows parsing of it. It manages memory of its internal StrBuffer. String's delimiters are set in a constructor - once per an instance of this class. The string is set by a public method - different strings can be parsed with the same instance. A class StrTokens does not need a copy constructor or an assignment operator; hence, they are placed in a private section and not implemented.

A constructor

StrTokens(const char * delimiters=",\n");

takes a zero terminated string of symbols-delimiters.

Public Methods

Public Methods return zero on success and non-zero in the case of an error, except number() method.

int number();

returns a number of StrTokens in the string.

There are three string-setting functions

1. int parse(const char * string);

sets a "string" to parse and starts parsing.

2. int translate_parse(const char * string, const char *from, char to=',');

sets a "string" to parse, translates each symbol in this string, which matches any symbol in the string "from", to the symbol "to", and starts parsing.

3. int shrink_parse(const char * string, const char *from=" /t", char to=' ');

sets a "string" to parse, translates each sequence of symbols in this string, which consists of symbols matching symbols in the string "from", to a one symbol "to", and starts parsing.

Parsing function

int get_next(StrBuffer& token);

gives a token in the parameter "token" and moves to a next token; it returns non-zero, if there are no more StrTokens.

int start();

starts parsing the same string again.

int move_next();

moves to a next token without returning a token to a caller; it returns non-zero, if there are no more StrTokens.

Private Members and Methods

Private Members and Methods are self-explanatory:

StrBuffer data, delimiters;
int token_number;
char *current,*start_pointer;

int parse();

Functions

Token functions return zero on success and non-zero on an error.

int translate_char(char * string, char from, char to);

translates each symbols in the "string", which matches a symbol "from" to the symbol "to".

int translate_char(char * string, const char *from, char to);

translates each symbols in the "string", which matches any symbol in the string "from", to the symbol "to".

int shrink(char * string, const char *from=" /t", char to=' ');

translates each sequence of symbols in the "string", which consists of symbols matching symbols in the string "from", to a one symbol "to".