Main Page   Packages   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

Tokenizer.cpp

Go to the documentation of this file.
00001 /*
00002  * $Id: Tokenizer.cpp,v 1.3 2002/08/05 18:56:23 mindstorm2600 Exp $
00003  */
00004 #include<Tokenizer.h>
00005 #include<Exception.h>
00006 #include<string.h>
00007 #include<stdlib.h>
00008 
00009 namespace clawsoft{
00010 
00011 Tokenizer::Tokenizer(char *frase, char *d):Object("Tokenizer"){
00012         temp = 0;
00013         token = 0;
00014         buffer = strdup(frase);
00015         temp = strdup(frase);
00016         delim = strdup(d);
00017 }
00018 
00019 Tokenizer::Tokenizer(String frase, String d):Object("Tokenizer"){
00020         temp = 0;
00021         token = 0;
00022         buffer = strdup(frase.toCharPtr());
00023         temp = strdup(frase.toCharPtr());
00024         delim = strdup(d.toCharPtr());
00025 }
00026 
00027 Tokenizer::~Tokenizer(){
00028         if(temp != 0)
00029                 free(temp);
00030         if(buffer != 0)
00031                 free(buffer);
00032         if(token != 0)
00033                 free(token);
00034         if(delim != 0)
00035                 free(delim);
00036 }
00037 
00038 char *Tokenizer::cnext(){
00039         unsigned int i;
00040         unsigned int len;
00041         char *ptr, *tptr;;
00042         if(buffer == 0)
00043                 return 0;
00044         if(strstr(buffer, delim) == 0){
00045                         if(token == 0)
00046                                 token = strdup(buffer);
00047                         free(buffer);
00048                         buffer = 0;
00049                         return token;
00050         }
00051         if(temp == 0){
00052                 free(token);
00053                 token = 0;
00054                 free(buffer);
00055                 buffer = 0;
00056                 free(delim);
00057                 delim = 0;
00058                 return 0;
00059         }
00060         else{
00061                 if(token != 0){
00062                         free(token);
00063                         token = 0;
00064                 }
00065                 if((ptr = strstr(temp, delim)) == 0){
00066                         token = strdup(temp);
00067                         free(temp);
00068                         temp = 0;
00069                 }
00070                 else{
00071                         token = (char *)malloc(strlen(temp) * sizeof(char));
00072                         memset((void *)token, 0, strlen(temp));
00073                         len = (((unsigned int)ptr)- (unsigned int)temp);
00074                         for(i = 0; i < len; i++){
00075                                 token[i] = temp[i];
00076                         }
00077                         token[i] = 0;
00078                         tptr = strdup(ptr + strlen(delim));
00079                         free(temp);
00080                         temp = tptr;
00081                 }
00082         }
00083         if(token[0] == 0)
00084                 return cnext();
00085         return token;
00086 }
00087 
00088 String &Tokenizer::next(){
00089         char *ptr;
00090         ptr = cnext();
00091         if(ptr == 0){
00092                 throw NoMoreTokensException();
00093         }
00094         str = ptr;
00095         return str;
00096 }
00097 
00098 }

Powered by:

SourceForge Logo