1 /*
2     Simple String Tokenizer Class
3 
4     Copyright (C) 2000-2013 Ruven Pillay.
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software Foundation,
18     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 
22 #ifndef _TOKENIZER_H
23 #define _TOKENIZER_H
24 
25 #include <string>
26 
27 
28 /// Simple utility class to split a string into tokens
29 
30 class Tokenizer{
31 
32  private:
33   std::string arg;
34   std::string delim;
35   std::string _nextToken();
36 
37  public:
38 
39   /// Constructor
40   /** \param s string to be split
41       \param d delimitter
42    */
43   Tokenizer( const std::string& s, const std::string& d );
44 
45   /// Return the next token
46   std::string nextToken();
47 
48   /// Indicate whether there are any tokens remaining
49   int hasMoreTokens();
50 
51 };
52 
53 
54 
Tokenizer(const std::string & s,const std::string & t)55 inline Tokenizer::Tokenizer( const std::string& s, const std::string& t )
56 {
57   arg = s;
58   delim = t;
59 }
60 
61 
62 
_nextToken()63 inline std::string Tokenizer::_nextToken()
64 {
65   int n;
66   std::string result;
67 
68   n = arg.find( delim );
69 
70   // No token in string, so return original
71   if( n < 0 ){
72     result = arg;
73     arg = std::string();
74   }
75   else{
76     result = arg.substr( 0, n );
77     arg = arg.substr( n + delim.length(), arg.length() );
78   }
79 
80   return result;
81 }
82 
83 
84 
nextToken()85 inline std::string Tokenizer::nextToken()
86 {
87   std::string result;
88   do{
89     result = _nextToken();
90   }
91   while( result.empty() && !arg.empty() );
92 
93   return result;
94 
95 }
96 
97 
hasMoreTokens()98 inline int Tokenizer::hasMoreTokens()
99 {
100   int n = arg.find_first_not_of( delim, 0 );
101   if( n >= 0 ) return 1;
102   else return 0;
103 }
104 
105 
106 
107 #endif
108