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" |
a6339323 |
20 | #include <string> |
21 | #include <cstring> |
cb21075d |
22 | |
a6339323 |
23 | StringTokenizer::StringTokenizer(std::string s) |
24 | : st(s), pos(0) |
cb21075d |
25 | { } |
26 | |
27 | bool |
a6339323 |
28 | StringTokenizer::more_tokens_p () |
cb21075d |
29 | { |
30 | if (pos == st.length()) |
31 | return false; |
32 | |
cf8ea873 |
33 | for (std::string::size_type i = pos; i < st.length(); i++) |
cb21075d |
34 | if (st[i] != ' ' && st[i] != '\t') |
35 | return true; |
36 | |
37 | return false; |
38 | } |
39 | |
40 | bool |
a6339323 |
41 | StringTokenizer::more_tokens_p (char c) |
cb21075d |
42 | { |
43 | if (pos == st.length()) |
44 | return false; |
45 | |
cf8ea873 |
46 | for (std::string::size_type i = pos; i < st.length(); i++) |
cb21075d |
47 | if (st[i] != c) |
48 | return true; |
49 | |
50 | return false; |
51 | } |
52 | |
cf8ea873 |
53 | unsigned int |
a6339323 |
54 | StringTokenizer::count_tokens () |
cb21075d |
55 | { |
cf8ea873 |
56 | unsigned int i = 0; |
a6339323 |
57 | StringTokenizer s (st); |
58 | |
59 | while (s.more_tokens_p ()) |
60 | { |
61 | s.next_token (); |
62 | i++; |
63 | } |
cb21075d |
64 | |
cb21075d |
65 | return i; |
66 | } |
67 | |
cf8ea873 |
68 | unsigned int |
a6339323 |
69 | StringTokenizer::count_tokens (char c) |
cb21075d |
70 | { |
cf8ea873 |
71 | unsigned int i = 0; |
cb21075d |
72 | StringTokenizer s(st); |
73 | |
a6339323 |
74 | while (s.more_tokens_p (c)) |
75 | { |
76 | s.next_token (c); |
77 | i++; |
78 | } |
79 | |
cb21075d |
80 | return i; |
81 | } |
82 | |
a6339323 |
83 | std::string |
84 | StringTokenizer::next_token() |
cb21075d |
85 | { |
cf8ea873 |
86 | std::string::size_type i = pos; |
cb21075d |
87 | |
88 | while (i < st.length() && (st[i] == ' ' || st[i] == '\t')) |
89 | i++; |
90 | |
cf8ea873 |
91 | for (std::string::size_type j = i; j < st.length(); j++) |
a6339323 |
92 | if (st[j] == ' ' || st[j] == '\t') |
93 | { |
94 | pos = j + 1; |
0bca109b |
95 | return st.substr (i, j - i); |
a6339323 |
96 | } |
cb21075d |
97 | |
98 | pos = st.length(); |
0bca109b |
99 | return st.substr (i, st.length() - i); |
cb21075d |
100 | } |
101 | |
a6339323 |
102 | std::string |
103 | StringTokenizer::next_token (char c, bool empty) |
cb21075d |
104 | { |
cf8ea873 |
105 | std::string::size_type i = pos; |
106 | std::string::size_type j = 0; |
cb21075d |
107 | |
108 | while (i < st.length() && (st[i] == c)) |
cf8ea873 |
109 | { |
110 | i++; |
111 | } |
112 | |
113 | for (j = i; j < st.length(); j++) |
114 | { |
115 | if (st[j] == c) |
116 | { |
117 | pos = j + 1; |
118 | return st.substr (i, j - i); |
119 | } |
120 | } |
cb21075d |
121 | |
122 | if (empty) |
123 | return ""; |
a6339323 |
124 | |
cb21075d |
125 | pos = st.length(); |
0bca109b |
126 | return st.substr (i, st.length() - i); |
cb21075d |
127 | } |
128 | |
a6339323 |
129 | std::string |
cb21075d |
130 | StringTokenizer::rest() |
131 | { |
132 | if (pos == st.length()) |
133 | return ""; |
134 | |
a6339323 |
135 | // Skip whitespace |
136 | while (pos < st.length() && (std::isspace (st[pos]))) |
cb21075d |
137 | pos++; |
138 | |
0bca109b |
139 | return st.substr (pos, st.length() - pos); |
cb21075d |
140 | } |