ucommon
commoncpp/tokenizer.h
Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along this program.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef COMMONCPP_TOKENIZER_H_
00044 #define COMMONCPP_TOKENIZER_H_
00045 
00046 #ifndef COMMONCPP_CONFIG_H_
00047 #include <commoncpp/config.h>
00048 #endif
00049 
00050 #ifndef COMMONCPP_THREAD_H_
00051 #include <commoncpp/thread.h>
00052 #endif
00053 
00054 #ifndef COMMMONCPP_EXCEPTION_H_
00055 #include <commoncpp/exception.h>
00056 #endif
00057 
00058 namespace ost {
00059 
00103 class __EXPORT StringTokenizer {
00104 public:
00110     static const char * const SPACE;
00111 
00121     // maybe move more global ?
00122     class NoSuchElementException { };
00123 
00128     class __EXPORT iterator {
00129         friend class StringTokenizer;  // access our private constructors
00130     private:
00131         const StringTokenizer *myTok; // my StringTokenizer
00132         const char *start;      // start of current token
00133         const char *tokEnd;     // end of current token (->nxDelimiter)
00134         const char *endp;       // one before next token
00135         char *token;            // allocated token, if requested
00136 
00137         // for initialization of the itEnd iterator
00138         iterator(const StringTokenizer &tok, const char *end)
00139             : myTok(&tok),tokEnd(0),endp(end),token(0) {}
00140 
00141         iterator(const StringTokenizer &tok)
00142             : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
00143             ++(*this); // init first token.
00144         }
00145 
00146     public:
00147         iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
00148 
00149         // see also: comment in implementation of operator++
00150         virtual ~iterator()
00151             { if (token) *token='\0'; delete [] token; }
00152 
00156         // everything, but not responsible for the allocated token.
00157         iterator(const iterator& i) :
00158             myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
00159             endp(i.endp),token(0) {}
00160 
00164         // everything, but not responsible for the allocated token.
00165         iterator &operator=(const iterator &i) {
00166             myTok = i.myTok;
00167             start = i.start; endp = i.endp; tokEnd = i.tokEnd;
00168             if ( token )
00169                 delete [] token;
00170             token = 0;
00171             return *this;
00172         }
00173 
00177         iterator &operator++() THROWS (NoSuchElementException);
00178 
00187         const char*  operator*() THROWS (NoSuchElementException);
00188 
00195         inline char nextDelimiter() const
00196             {return (tokEnd) ? *tokEnd : '\0';}
00197 
00202         // only compare the end-position. speed.
00203         inline bool operator == (const iterator &other) const
00204             {return (endp == other.endp);}
00205 
00210         // only compare the end position. speed.
00211         inline bool operator != (const iterator &other) const
00212             {return (endp != other.endp);}
00213     };
00214 private:
00215     friend class StringTokenizer::iterator;
00216     const char *str;
00217     const char *delim;
00218     bool skipAll, trim;
00219     iterator itEnd;
00220 
00221 public:
00260     StringTokenizer (const char *str,
00261              const char *delim,
00262              bool skipAllDelim = false,
00263              bool trim = false);
00264 
00274     StringTokenizer (const char *s);
00275 
00279     iterator begin() const
00280         {return iterator(*this);}
00281 
00286     void setDelimiters (const char *d)
00287         {delim = d;}
00288 
00293     iterator begin(const char *d) {
00294         delim = d;
00295         return iterator(*this);
00296     }
00297 
00301     const iterator& end() const
00302         {return itEnd;}
00303 };
00304 
00305 } // namespace ost
00306 
00307 #endif
00308