ucommon
tokenizer.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef COMMONCPP_TOKENIZER_H_
45 #define COMMONCPP_TOKENIZER_H_
46 
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
49 #endif
50 
51 #ifndef COMMONCPP_THREAD_H_
52 #include <commoncpp/thread.h>
53 #endif
54 
55 #ifndef COMMMONCPP_EXCEPTION_H_
56 #include <commoncpp/exception.h>
57 #endif
58 
59 NAMESPACE_COMMONCPP
60 
104 class __EXPORT StringTokenizer {
105 public:
111  static const char * const SPACE;
112 
122  // maybe move more global ?
124 
129  class __EXPORT iterator {
130  friend class StringTokenizer; // access our private constructors
131  private:
132  const StringTokenizer *myTok; // my StringTokenizer
133  const char *start; // start of current token
134  const char *tokEnd; // end of current token (->nxDelimiter)
135  const char *endp; // one before next token
136  char *token; // allocated token, if requested
137 
138  // for initialization of the itEnd iterator
139  iterator(const StringTokenizer &tok, const char *end)
140  : myTok(&tok),tokEnd(0),endp(end),token(0) {}
141 
142  iterator(const StringTokenizer &tok)
143  : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
144  ++(*this); // init first token.
145  }
146 
147  public:
148  iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
149 
150  // see also: comment in implementation of operator++
151  virtual ~iterator()
152  { if (token) *token='\0'; delete [] token; }
153 
157  // everything, but not responsible for the allocated token.
158  iterator(const iterator& i) :
159  myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
160  endp(i.endp),token(0) {}
161 
165  // everything, but not responsible for the allocated token.
166  iterator &operator = (const iterator &i)
167  {
168  myTok = i.myTok;
169  start = i.start; endp = i.endp; tokEnd = i.tokEnd;
170  if ( token )
171  delete [] token;
172  token = 0;
173  return *this;
174  }
175 
179  iterator &operator ++ () THROWS (NoSuchElementException);
180 
189  const char* operator * () THROWS (NoSuchElementException);
190 
197  inline char nextDelimiter() const
198  {return (tokEnd) ? *tokEnd : '\0';}
199 
204  // only compare the end-position. speed.
205  inline bool operator == (const iterator &other) const
206  {return (endp == other.endp);}
207 
212  // only compare the end position. speed.
213  inline bool operator != (const iterator &other) const
214  {return (endp != other.endp);}
215  };
216 private:
217  friend class StringTokenizer::iterator;
218  const char *str;
219  const char *delim;
220  bool skipAll, trim;
221  iterator itEnd;
222 
223 public:
262  StringTokenizer (const char *str,
263  const char *delim,
264  bool skipAllDelim = false,
265  bool trim = false);
266 
276  StringTokenizer (const char *s);
277 
281  iterator begin() const
282  {return iterator(*this);}
283 
288  void setDelimiters (const char *d)
289  {delim = d;}
290 
295  iterator begin(const char *d)
296  {
297  delim = d;
298  return iterator(*this);
299  }
300 
304  const iterator& end() const
305  {return itEnd;}
306 };
307 
308 END_NAMESPACE
309 
310 #endif
311 
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Definition: thread.h:1814
iterator begin() const
returns the begin iterator
Definition: tokenizer.h:281
The input forward iterator for tokens.
Definition: tokenizer.h:129
void setDelimiters(const char *d)
changes the set of delimiters used in subsequent iterations.
Definition: tokenizer.h:288
static const char *const SPACE
a delimiter string containing all usual whitespace delimiters.
Definition: tokenizer.h:111
Common C++ thread class and sychronization objects.
iterator(const iterator &i)
copy constructor.
Definition: tokenizer.h:158
Splits delimited string into tokens.
Definition: tokenizer.h:104
GNU Common C++ exception model base classes.
const iterator & end() const
the iterator marking the end.
Definition: tokenizer.h:304
iterator begin(const char *d)
returns a begin iterator with an alternate set of delimiters.
Definition: tokenizer.h:295
Exception thrown, if someone tried to read beyond the end of the tokens.
Definition: tokenizer.h:123