00001 00002 #ifndef INCL_STRING_TOKENIZER_H 00003 #define INCL_STRING_TOKENIZER_H 00004 00005 #include "utilit.h" 00029 class StringTokenizer{ 00030 char *text_ptr; 00031 char *text; 00032 int i; 00033 //------------------------------------------------------- 00034 string delims; 00035 bool is_delim(char ch){ 00036 return delims.find_first_of(ch) != string::npos; 00037 } 00038 char *_val; 00039 public: 00040 //------------------------------------------------------- 00041 StringTokenizer(const char *_text, const char *_delims) 00042 :delims(_delims) 00043 ,i(-1) 00044 { 00045 size_t leng = strlen(_text); 00046 text_ptr = new char[leng+2]; 00047 text = text_ptr; 00048 strcpy(text, _text); 00049 text[leng+1] = 0; 00050 } 00051 00052 //------------------------------------------------------- 00053 ~StringTokenizer(){ 00054 delete []text_ptr; 00055 } 00056 00057 //------------------------------------------------------- 00058 const char * operator ()(){ 00059 ++i; 00060 // skip delims 00061 while(*text && is_delim(*text)) 00062 text++; 00063 00064 if(*text == 0) return 0; 00065 00066 char *ret_val = text; 00067 00068 while(*text && !is_delim(*text)) 00069 text++; 00070 *text++ = 0; 00071 _val = ret_val; 00072 return ret_val; 00073 } 00074 string next_token () 00075 { 00076 const char* s = operator()(); 00077 if (s) 00078 return s; 00079 else 00080 return string(); 00081 } 00082 00083 const char * get_rest () 00084 { 00085 return text; 00086 }; 00087 00088 char *val(){return _val;} 00089 int count(){return i;} 00090 bool has_next(){return (*text) != 0;} 00091 00092 }; 00093 00094 00095 #endif // INCL_STRING_TOKENIZER_H