ucommon
tokenizer.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 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 Lesser General Public License
15 // along this program. If not, see <http://www.gnu.org/licenses/>.
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 COMMONCPP_TOKENIZER_H_
44 #define COMMONCPP_TOKENIZER_H_
45 
46 #ifndef COMMONCPP_CONFIG_H_
47 #include <commoncpp/config.h>
48 #endif
49 
50 #ifndef COMMONCPP_THREAD_H_
51 #include <commoncpp/thread.h>
52 #endif
53 
54 #ifndef COMMMONCPP_EXCEPTION_H_
55 #include <commoncpp/exception.h>
56 #endif
57 
58 namespace ost {
59 
103 class __EXPORT StringTokenizer {
104 public:
110  static const char * const SPACE;
111 
121  // maybe move more global ?
123 
128  class __EXPORT iterator {
129  friend class StringTokenizer; // access our private constructors
130  private:
131  const StringTokenizer *myTok; // my StringTokenizer
132  const char *start; // start of current token
133  const char *tokEnd; // end of current token (->nxDelimiter)
134  const char *endp; // one before next token
135  char *token; // allocated token, if requested
136 
137  // for initialization of the itEnd iterator
138  iterator(const StringTokenizer &tok, const char *end)
139  : myTok(&tok),tokEnd(0),endp(end),token(0) {}
140 
141  iterator(const StringTokenizer &tok)
142  : myTok(&tok),tokEnd(0),endp(myTok->str-1),token(0) {
143  ++(*this); // init first token.
144  }
145 
146  public:
147  iterator() : myTok(0),start(0),tokEnd(0),endp(0),token(0) {}
148 
149  // see also: comment in implementation of operator++
150  virtual ~iterator()
151  { if (token) *token='\0'; delete [] token; }
152 
156  // everything, but not responsible for the allocated token.
157  iterator(const iterator& i) :
158  myTok(i.myTok),start(i.start),tokEnd(i.tokEnd),
159  endp(i.endp),token(0) {}
160 
164  // everything, but not responsible for the allocated token.
166  myTok = i.myTok;
167  start = i.start; endp = i.endp; tokEnd = i.tokEnd;
168  if ( token )
169  delete [] token;
170  token = 0;
171  return *this;
172  }
173 
177  iterator &operator++() THROWS (NoSuchElementException);
178 
187  const char* operator*() THROWS (NoSuchElementException);
188 
195  inline char nextDelimiter() const
196  {return (tokEnd) ? *tokEnd : '\0';}
197 
202  // only compare the end-position. speed.
203  inline bool operator == (const iterator &other) const
204  {return (endp == other.endp);}
205 
210  // only compare the end position. speed.
211  inline bool operator != (const iterator &other) const
212  {return (endp != other.endp);}
213  };
214 private:
215  friend class StringTokenizer::iterator;
216  const char *str;
217  const char *delim;
218  bool skipAll, trim;
219  iterator itEnd;
220 
221 public:
260  StringTokenizer (const char *str,
261  const char *delim,
262  bool skipAllDelim = false,
263  bool trim = false);
264 
274  StringTokenizer (const char *s);
275 
279  iterator begin() const
280  {return iterator(*this);}
281 
286  void setDelimiters (const char *d)
287  {delim = d;}
288 
293  iterator begin(const char *d) {
294  delim = d;
295  return iterator(*this);
296  }
297 
301  const iterator& end() const
302  {return itEnd;}
303 };
304 
305 } // namespace ost
306 
307 #endif
308 
Common C++ thread class and sychronization objects.
Definition: address.h:58
Splits delimited string into tokens.
Definition: tokenizer.h:103
void setDelimiters(const char *d)
changes the set of delimiters used in subsequent iterations.
Definition: tokenizer.h:286
iterator(const iterator &i)
copy constructor.
Definition: tokenizer.h:157
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Definition: thread.h:1870
Exception thrown, if someone tried to read beyond the end of the tokens.
Definition: tokenizer.h:122
iterator & operator=(const iterator &i)
assignment operator.
Definition: tokenizer.h:165
static const char *const SPACE
a delimiter string containing all usual whitespace delimiters.
Definition: tokenizer.h:110
const iterator & end() const
the iterator marking the end.
Definition: tokenizer.h:301
iterator begin(const char *d)
returns a begin iterator with an alternate set of delimiters.
Definition: tokenizer.h:293
The input forward iterator for tokens.
Definition: tokenizer.h:128
iterator begin() const
returns the begin iterator
Definition: tokenizer.h:279
GNU Common C++ exception model base classes.