00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cctype>
00019 #include <stdio.h>
00020 #include "stringtokenizer.h"
00021 #include "errors.h"
00022
00023
00024 StringTokenizer :: StringTokenizer ( const ASCString& _str, bool includeOperators_ )
00025 : str( _str ), i ( 0 )
00026 {
00027 includeOperators = includeOperators_ ;
00028
00029 delimitter = "=*/+->";
00030
00031 }
00032
00033 StringTokenizer :: StringTokenizer ( const ASCString& _str, const ASCString& delimitter_ )
00034 : str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
00035 {
00036 }
00037
00038 StringTokenizer :: StringTokenizer ( const ASCString& _str, const char* delimitter_ )
00039 : str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
00040 {
00041 }
00042
00043
00044
00045 int StringTokenizer::CharSpace ( char c )
00046 {
00047 if ( c <= ' ' )
00048 return 0;
00049
00050 const char* d = delimitter.c_str();
00051 do {
00052 if( *d == c && !includeOperators )
00053 return 2;
00054 if ( *d == 0 )
00055 return 1;
00056 d++;
00057 } while ( true );
00058 }
00059
00060 void StringTokenizer::skipTill(char endchar )
00061 {
00062 const char* s = str.c_str();
00063 while ( i < str.length() && s[i] != endchar )
00064 ++i;
00065 }
00066
00067 ASCString StringTokenizer::getNextToken( )
00068 {
00069 while ( i < str.length() && !CharSpace(str[i]) )
00070 i++;
00071
00072 if ( i == str.length() )
00073 return "";
00074
00075 int begin = i;
00076 int cs = CharSpace( str[i] );
00077 do {
00078 i++;
00079 } while ( i < str.length() && CharSpace( str[i] ) == cs );
00080 return str.substr(begin, i-begin);
00081 }
00082
00083
00084 ASCString StringTokenizer::getRemaining( )
00085 {
00086 return str.substr(i);
00087 }
00088
00089
00090
00091 StringSplit :: StringSplit ( const ASCString& _str, const ASCString& delimitter_ )
00092 : str( _str ), i ( 0 ), delimitter(delimitter_)
00093 {
00094 }
00095
00096
00097
00098 bool StringSplit::isDelimitter ( char c )
00099 {
00100 const char* d = delimitter.c_str();
00101 do {
00102 if( *d == c )
00103 return true;
00104 if ( *d == 0 )
00105 return false;
00106 d++;
00107 } while ( true );
00108 }
00109
00110
00111 ASCString StringSplit::getNextToken( )
00112 {
00113 const char* sp = str.c_str();
00114 while ( i < str.length() && isDelimitter(sp[i]) )
00115 i++;
00116
00117 if ( i == str.length() )
00118 return "";
00119
00120 int begin = i;
00121 while ( i < str.length() && !isDelimitter(sp[i]) )
00122 i++;
00123
00124 return str.substr(begin, i-begin);
00125 }
00126