| 1 |
#ifndef _PROTO_STRING |
| 2 |
|
| 3 |
// This module will include mostly "helper" classes for parsing |
| 4 |
// and manipulating "char" strings. At the moment, there are |
| 5 |
// _not_ plans to create yet another C++ "Strings" class. |
| 6 |
|
| 7 |
/*************** |
| 8 |
|
| 9 |
The ProtoTokenator provides an easy means to parse a string, item by item, |
| 10 |
with each item separated by a "delimiter" (white space by default) |
| 11 |
|
| 12 |
Additional optional parameters: |
| 13 |
|
| 14 |
1) If 'stripWhitespace' is true, leading/trailing white space is removed from return items |
| 15 |
|
| 16 |
2) The 'maxCount' limits the number of items returned with the last item returned |
| 17 |
consisting of the remainder of the string (leading/trailing whitespace removed |
| 18 |
if 'strip' is set to 'true' |
| 19 |
|
| 20 |
3) The 'reverse' option tokenizes the string in reverse order. |
| 21 |
|
| 22 |
4) If 'stripTokens' is true, leading (or trailing for 'reverse' case) 'extraneous' |
| 23 |
token characters are stripped _only_ when 'maxCount' is limited (non-zero). |
| 24 |
The main use case here is when ProtoTokenator is used to split file path strings |
| 25 |
(e.g., delimited by PROTO_PATH_DELIMITER) and it removes trailing, extraneous |
| 26 |
file path delimiters from directory prefixes (reverse split, maxCount=1) or leading, |
| 27 |
extraneous file path delimiters from the file name portion (forward split, maxCount=1) |
| 28 |
|
| 29 |
*******************/ |
| 30 |
|
| 31 |
|
| 32 |
#include "protoDefs.h" // for 'NULL' definition |
| 33 |
|
| 34 |
// (The "strip_whitespace" option removes leading/trailing white space) |
| 35 |
// |
| 36 |
class ProtoTokenator |
| 37 |
{ |
| 38 |
public: |
| 39 |
ProtoTokenator(const char* text, // string to parse |
| 40 |
char delimiter=' ', // what delimeter to use |
| 41 |
bool stripWhitespace=true, // remove leading/trailing whitespace from fields |
| 42 |
unsigned int maxCount = 0, // non-zero limits number of splits |
| 43 |
bool reverse=false, // tokenize backwards from end of string |
| 44 |
bool stripTokens=false); // for non-zero maxCount, strip any extra tokens |
| 45 |
// found at last break point (useful for file paths) |
| 46 |
~ProtoTokenator(); |
| 47 |
|
| 48 |
const char* const GetNextItem(bool detach=false); // if "detach" is true, caller must delete[] returned array. |
| 49 |
// Otherwise, returned pointer is only valid until next |
| 50 |
// call to GetNextItem() or ProtoTokenator destruction |
| 51 |
|
| 52 |
const char* GetNextPtr() const // pointer to unparsed remainder |
| 53 |
{return next_ptr;} |
| 54 |
|
| 55 |
void Reset(const char* text=NULL, char delimiter='\0'); // default args retain current text/delimiter values |
| 56 |
|
| 57 |
void SetNullDelimiter() // with "stripWhitespace" enabled, this enables |
| 58 |
{token = '\0';} // simple leading/trailing stripping |
| 59 |
|
| 60 |
|
| 61 |
// If this is called, caller is responsible to delete |
| 62 |
// the memory allocated for the previous item returned. |
| 63 |
// (This enables deferred, 'detach' decision) |
| 64 |
char* DetachPreviousItem() |
| 65 |
{ |
| 66 |
char* item = (char*)prev_item; |
| 67 |
prev_item = NULL; |
| 68 |
return item; |
| 69 |
} |
| 70 |
bool TokenMatch(char c) const; |
| 71 |
|
| 72 |
private: |
| 73 |
char token; |
| 74 |
bool strip_whitespace; // strip leading/trailing whitespace if true |
| 75 |
bool strip_tokens; // string leading/trailing tokens if true |
| 76 |
bool reverse; |
| 77 |
unsigned int max_count; |
| 78 |
unsigned int remain; |
| 79 |
const char* text_ptr; |
| 80 |
const char* next_ptr; |
| 81 |
char* prev_item; |
| 82 |
}; // end class ProtoTokenator |
| 83 |
|
| 84 |
#endif // !_PROTO_STRING |