GNU CommonC++
tokenizer.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_TOKENIZER_H_
44 #define CCXX_TOKENIZER_H_
45 
46 #ifndef CCXX_MISSING_H_
47 #include <cc++/missing.h>
48 #endif
49 
50 #ifndef CCXX_THREAD_H_
51 #include <cc++/thread.h>
52 #endif
53 
54 #ifdef CCXX_NAMESPACES
55 namespace ost {
56 #endif
57 
102 public:
108  static const char * const SPACE;
109 
119  // maybe move more global ?
121 
127  friend class StringTokenizer; // access our private constructors
128  private:
129  const StringTokenizer *myTok; // my StringTokenizer
130  const char *start; // start of current token
131  const char *tokEnd; // end of current token (->nxDelimiter)
132  const char *endp; // one before next token
133  char *token; // allocated token, if requested
134 
135  // for initialization of the itEnd iterator
136  iterator(const StringTokenizer &tok, const char *end)
137  : myTok(&tok),tokEnd(0),endp(end),token(0) {}
138 
139  iterator(const StringTokenizer &tok)
140  : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
141  ++(*this); // init first token.
142  }
143 
144  public:
145  iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
146 
147  // see also: comment in implementation of operator++
148  virtual ~iterator()
149  { if (token) *token='\0'; delete [] token; }
150 
154  // everything, but not responsible for the allocated token.
155  iterator(const iterator& i) :
156  myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
157  endp(i.endp),token(0) {}
158 
162  // everything, but not responsible for the allocated token.
163  iterator &operator = (const iterator &i)
164  {
165  myTok = i.myTok;
166  start = i.start; endp = i.endp; tokEnd = i.tokEnd;
167  if ( token )
168  delete [] token;
169  token = 0;
170  return *this;
171  }
172 
176  iterator &operator ++ () THROWS (NoSuchElementException);
177 
186  const char* operator * () THROWS (NoSuchElementException);
187 
194  inline char nextDelimiter() const
195  {return (tokEnd) ? *tokEnd : '\0';}
196 
201  // only compare the end-position. speed.
202  inline bool operator == (const iterator &other) const
203  {return (endp == other.endp);}
204 
209  // only compare the end position. speed.
210  inline bool operator != (const iterator &other) const
211  {return (endp != other.endp);}
212  };
213 private:
215  const char *str;
216  const char *delim;
217  bool skipAll, trim;
218  iterator itEnd;
219 
220 public:
259  StringTokenizer (const char *str,
260  const char *delim,
261  bool skipAllDelim = false,
262  bool trim = false);
263 
273  StringTokenizer (const char *s);
274 
278  iterator begin() const
279  {return iterator(*this);}
280 
285  void setDelimiters (const char *d)
286  {delim = d;}
287 
292  iterator begin(const char *d)
293  {
294  delim = d;
295  return iterator(*this);
296  }
297 
301  const iterator& end() const
302  {return itEnd;}
303 };
304 
305 #ifdef CCXX_NAMESPACES
306 }
307 #endif
308 
309 #endif
310