StringTokenizer.h

00001 /* This file is Copyright 2000-2008 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */
00002 
00003 #ifndef MuscleStringTokenizer_h
00004 #define MuscleStringTokenizer_h
00005 
00006 #include "support/MuscleSupport.h"
00007 
00008 BEGIN_NAMESPACE(muscle);
00009 
00011 class StringTokenizer
00012 {
00013 public:
00021    StringTokenizer(const char * tokenizeMe, const char * separators = ", \t")
00022    {
00023       int tlen = strlen(tokenizeMe);
00024       int slen = strlen(separators);
00025  
00026       char * temp = newnothrow_array(char, slen+1+tlen+1);
00027       if (temp)
00028       {
00029          strcpy(temp, separators);
00030          _seps = temp;
00031          _next = temp + slen + 1;
00032          strcpy(_next, tokenizeMe);
00033       }
00034       else 
00035       {
00036          _seps = _next = NULL;
00037          WARN_OUT_OF_MEMORY;
00038       }
00039  
00040       _alloced = true;
00041    }
00042 
00051    StringTokenizer(bool junk, char * tokenizeMe, const char * separators = ", \t")
00052    {
00053       (void) junk;
00054       _next = tokenizeMe;
00055       _seps = separators;
00056       _alloced = false;
00057    }
00058 
00060    ~StringTokenizer()
00061    {
00062       // must cast to (char *) or VC++ complains :^P
00063       if (_alloced) delete [] ((char *)_seps);
00064    }
00065 
00067    char * GetNextToken()
00068    {
00069       if (_seps)
00070       {
00071          // Move until first non-sep char
00072          while((*_next)&&(strchr(_seps, *_next) != NULL)) _next++;
00073          if (*_next)
00074          {
00075             char * ret = _next;
00076 
00077             // Move until next sep-char
00078             while((*_next)&&(strchr(_seps, *_next) == NULL)) _next++;
00079             if (*_next) 
00080             {
00081                *_next = '\0';
00082                _next++;
00083             }
00084             return ret;
00085          }
00086       }
00087       return NULL;
00088    }
00089 
00091    char * operator()() {return GetNextToken();}
00092  
00097    char * GetRemainderOfString()
00098    {
00099       if (_seps)
00100       {
00101          // Move until first non-sep char
00102          while((*_next)&&(strchr(_seps, *_next) != NULL)) _next++;
00103          return (*_next) ? _next : NULL;  // and return from there
00104       }
00105       return NULL;
00106    }
00107 
00108 private:
00109    StringTokenizer(const StringTokenizer &);   // unimplemented on purpose
00110    StringTokenizer & operator = (const StringTokenizer &);  // unimplemented on purpose
00111 
00112    bool _alloced;
00113    const char * _seps;
00114    char * _next;
00115 };
00116 
00117 END_NAMESPACE(muscle);
00118 
00119 #endif

Generated on Thu Jun 5 17:47:54 2008 for MUSCLE by  doxygen 1.5.1