Branch data Line data Source code
1 : : #define DIMLIB 2 : : #include "tokenstring.hxx" 3 : : 4 : 0 : TokenString::TokenString(char *str) 5 : : { 6 : 0 : token_buff = new char[((int)strlen(str)+1)*2]; 7 : 0 : token_ptr = token_buff; 8 : 0 : token_seps = 0; 9 : 0 : store_str(str); 10 : 0 : token_ptr = token_buff; 11 : 0 : curr_token_ptr = token_ptr; 12 : 0 : } 13 : : 14 : 0 : TokenString::TokenString(char *str, char *seps) 15 : : { 16 : 0 : token_buff = new char[((int)strlen(str)+1)*2]; 17 : 0 : token_ptr = token_buff; 18 : 0 : token_seps = new char[((int)strlen(seps)+1)]; 19 : 0 : strcpy(token_seps,seps); 20 : 0 : store_str(str); 21 : 0 : token_ptr = token_buff; 22 : 0 : curr_token_ptr = token_ptr; 23 : 0 : } 24 : : 25 : 0 : TokenString::~TokenString() 26 : : { 27 : 0 : if(token_buff) 28 : : { 29 : 0 : delete []token_buff; 30 : 0 : token_buff = 0; 31 : : } 32 : 0 : if(token_seps) 33 : : { 34 : 0 : delete []token_seps; 35 : 0 : token_seps = 0; 36 : : } 37 : 0 : } 38 : : 39 : 0 : void TokenString::store_str(char *str) 40 : : { 41 : 0 : int i, in_tok = 0; 42 : 0 : int sep = 0; 43 : : 44 : 0 : n_tokens = 0; 45 : 0 : if(!token_seps) 46 : : { 47 : 0 : while(*str) 48 : : { 49 : 0 : if( (*str == '@') || (*str == '|') || (*str == '/') || 50 : 0 : (*str == '=') || (*str == '(') || (*str == ')') || 51 : 0 : (*str == '.') || (*str == '\n')) 52 : : { 53 : 0 : if(in_tok) 54 : : { 55 : 0 : *token_ptr++ = '\0'; 56 : 0 : n_tokens++; 57 : : } 58 : 0 : *token_ptr++ = *str++; 59 : 0 : *token_ptr++ = '\0'; 60 : 0 : n_tokens++; 61 : 0 : in_tok = 0; 62 : : } 63 : 0 : else if(*str == '"') 64 : : { 65 : 0 : if(in_tok) 66 : : { 67 : 0 : *token_ptr++ = '\0'; 68 : 0 : n_tokens++; 69 : : } 70 : 0 : *token_ptr++ = *str++; 71 : 0 : while(*str != '"') 72 : : { 73 : 0 : *token_ptr++ = *str++; 74 : : } 75 : 0 : *token_ptr++ = *str++; 76 : 0 : *token_ptr++ = '\0'; 77 : 0 : n_tokens++; 78 : 0 : in_tok = 0; 79 : : } 80 : 0 : else if(*str == ':') 81 : : { 82 : 0 : if(*(str+1) == ':') 83 : : { 84 : 0 : if(in_tok) 85 : : { 86 : 0 : *token_ptr++ = '\0'; 87 : 0 : n_tokens++; 88 : : } 89 : 0 : *token_ptr++ = *str++; 90 : 0 : *token_ptr++ = *str++; 91 : 0 : *token_ptr++ = '\0'; 92 : 0 : n_tokens++; 93 : 0 : in_tok = 0; 94 : : } 95 : : else 96 : : { 97 : 0 : *token_ptr++ = *str++; 98 : 0 : in_tok = 1; 99 : : } 100 : : } 101 : : else 102 : : { 103 : 0 : *token_ptr++ = *str++; 104 : 0 : in_tok = 1; 105 : : } 106 : : } 107 : : } 108 : : else 109 : : { 110 : 0 : while(*str) 111 : : { 112 : 0 : sep = 0; 113 : 0 : for(i = 0; i < (int)strlen(token_seps); i++) 114 : : { 115 : 0 : if(*str == token_seps[i]) 116 : : { 117 : 0 : if(in_tok) 118 : : { 119 : 0 : *token_ptr++ = '\0'; 120 : 0 : n_tokens++; 121 : : } 122 : 0 : *token_ptr++ = *str++; 123 : 0 : *token_ptr++ = '\0'; 124 : 0 : sep = 1; 125 : 0 : in_tok = 0; 126 : 0 : n_tokens++; 127 : 0 : break; 128 : : } 129 : : } 130 : 0 : if(!sep) 131 : : { 132 : 0 : *token_ptr++ = *str++; 133 : 0 : in_tok = 1; 134 : : } 135 : : } 136 : : } 137 : 0 : if(in_tok) 138 : : { 139 : 0 : *token_ptr++ = '\0'; 140 : 0 : n_tokens++; 141 : : } 142 : 0 : *token_ptr++ = '\0'; 143 : 0 : } 144 : : 145 : 0 : int TokenString::getToken(char *&token) 146 : : { 147 : : 148 : 0 : if(!*token_ptr) 149 : : { 150 : 0 : token_ptr = token_buff; 151 : 0 : curr_token_ptr = token_ptr; 152 : 0 : return(0); 153 : : } 154 : : 155 : 0 : curr_token_ptr = token_ptr; 156 : 0 : token_ptr += (int)strlen(curr_token_ptr)+1; 157 : 0 : token = curr_token_ptr; 158 : : 159 : 0 : return(1); 160 : : } 161 : : 162 : 0 : void TokenString::pushToken() 163 : : { 164 : 0 : push_token_ptr = token_ptr; 165 : 0 : } 166 : : 167 : 0 : void TokenString::popToken() 168 : : { 169 : 0 : token_ptr = push_token_ptr; 170 : 0 : } 171 : : 172 : 0 : int TokenString::cmpToken(char *str) 173 : : { 174 : 0 : if(!strcmp(curr_token_ptr, str)) 175 : 0 : return(1); 176 : 0 : return(0); 177 : : } 178 : : 179 : 0 : int TokenString::firstToken() 180 : : { 181 : 0 : if(curr_token_ptr == token_buff) 182 : 0 : return(1); 183 : 0 : return(0); 184 : : } 185 : : 186 : 0 : int TokenString::getNTokens() 187 : : { 188 : 0 : return n_tokens; 189 : : } 190 : : 191 : 0 : int TokenString::getNTokens(char *str) 192 : : { 193 : 0 : int n = 0; 194 : : char *token; 195 : : 196 : 0 : while(getToken(token)) 197 : : { 198 : 0 : if(!strcmp(token,str)) 199 : 0 : n++; 200 : : } 201 : 0 : return n; 202 : : } 203 : : /* 204 : : main(int argc, char *argv[]) 205 : : { 206 : : TokenString *ts; 207 : : char *token; 208 : : 209 : : ts = new TokenString(argv[1],"/)"); 210 : : cout << "n = " << ts->getNTokens() << "\n"; 211 : : cout.flush(); 212 : : while(ts->getToken(token)) 213 : : { 214 : : cout << token << "\n"; 215 : : cout.flush(); 216 : : } 217 : : } 218 : : */