cb21075d |
1 | // StringTokenizer.C -*- C++ -*- |
2 | // Copyright (c) 1997, 1998 Etienne BERNARD |
a6339323 |
3 | // Copyright (c) 2005 Clinton Ebadi |
cb21075d |
4 | |
5 | // This program is free software; you can redistribute it and/or modify |
6 | // it under the terms of the GNU General Public License as published by |
7 | // the Free Software Foundation; either version 2 of the License, or |
8 | // any later version. |
9 | |
10 | // This program is distributed in the hope that it will be useful, |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | // GNU General Public License for more details. |
14 | |
15 | // You should have received a copy of the GNU General Public License |
16 | // along with this program; if not, write to the Free Software |
39b022cb |
17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
cb21075d |
18 | |
19 | #include "StringTokenizer.H" |
cfa82921 |
20 | |
a6339323 |
21 | #include <string> |
cfa82921 |
22 | |
a6339323 |
23 | #include <cstring> |
cb21075d |
24 | |
a6339323 |
25 | StringTokenizer::StringTokenizer(std::string s) |
26 | : st(s), pos(0) |
cb21075d |
27 | { } |
28 | |
29 | bool |
a6339323 |
30 | StringTokenizer::more_tokens_p () |
cb21075d |
31 | { |
32 | if (pos == st.length()) |
33 | return false; |
34 | |
cf8ea873 |
35 | for (std::string::size_type i = pos; i < st.length(); i++) |
cb21075d |
36 | if (st[i] != ' ' && st[i] != '\t') |
37 | return true; |
38 | |
39 | return false; |
40 | } |
41 | |
42 | bool |
a6339323 |
43 | StringTokenizer::more_tokens_p (char c) |
cb21075d |
44 | { |
45 | if (pos == st.length()) |
46 | return false; |
47 | |
cf8ea873 |
48 | for (std::string::size_type i = pos; i < st.length(); i++) |
cb21075d |
49 | if (st[i] != c) |
50 | return true; |
51 | |
52 | return false; |
53 | } |
54 | |
cf8ea873 |
55 | unsigned int |
a6339323 |
56 | StringTokenizer::count_tokens () |
cb21075d |
57 | { |
cf8ea873 |
58 | unsigned int i = 0; |
a6339323 |
59 | StringTokenizer s (st); |
60 | |
61 | while (s.more_tokens_p ()) |
62 | { |
63 | s.next_token (); |
64 | i++; |
65 | } |
cb21075d |
66 | |
cb21075d |
67 | return i; |
68 | } |
69 | |
cf8ea873 |
70 | unsigned int |
a6339323 |
71 | StringTokenizer::count_tokens (char c) |
cb21075d |
72 | { |
cf8ea873 |
73 | unsigned int i = 0; |
cb21075d |
74 | StringTokenizer s(st); |
75 | |
a6339323 |
76 | while (s.more_tokens_p (c)) |
77 | { |
78 | s.next_token (c); |
79 | i++; |
80 | } |
81 | |
cb21075d |
82 | return i; |
83 | } |
84 | |
a6339323 |
85 | std::string |
86 | StringTokenizer::next_token() |
cb21075d |
87 | { |
cf8ea873 |
88 | std::string::size_type i = pos; |
cb21075d |
89 | |
90 | while (i < st.length() && (st[i] == ' ' || st[i] == '\t')) |
91 | i++; |
92 | |
cf8ea873 |
93 | for (std::string::size_type j = i; j < st.length(); j++) |
a6339323 |
94 | if (st[j] == ' ' || st[j] == '\t') |
95 | { |
96 | pos = j + 1; |
0bca109b |
97 | return st.substr (i, j - i); |
a6339323 |
98 | } |
cb21075d |
99 | |
100 | pos = st.length(); |
0bca109b |
101 | return st.substr (i, st.length() - i); |
cb21075d |
102 | } |
103 | |
a6339323 |
104 | std::string |
105 | StringTokenizer::next_token (char c, bool empty) |
cb21075d |
106 | { |
cf8ea873 |
107 | std::string::size_type i = pos; |
108 | std::string::size_type j = 0; |
cb21075d |
109 | |
110 | while (i < st.length() && (st[i] == c)) |
cf8ea873 |
111 | { |
112 | i++; |
113 | } |
114 | |
115 | for (j = i; j < st.length(); j++) |
116 | { |
117 | if (st[j] == c) |
118 | { |
119 | pos = j + 1; |
120 | return st.substr (i, j - i); |
121 | } |
122 | } |
cb21075d |
123 | |
124 | if (empty) |
125 | return ""; |
a6339323 |
126 | |
cb21075d |
127 | pos = st.length(); |
0bca109b |
128 | return st.substr (i, st.length() - i); |
cb21075d |
129 | } |
130 | |
a6339323 |
131 | std::string |
cb21075d |
132 | StringTokenizer::rest() |
133 | { |
134 | if (pos == st.length()) |
135 | return ""; |
136 | |
a6339323 |
137 | // Skip whitespace |
138 | while (pos < st.length() && (std::isspace (st[pos]))) |
cb21075d |
139 | pos++; |
140 | |
0bca109b |
141 | return st.substr (pos, st.length() - pos); |
cb21075d |
142 | } |