Add gnulib gettext module for config.rpath
[clinton/bobotpp.git] / source / StringTokenizer.C
... / ...
CommitLineData
1// StringTokenizer.C -*- C++ -*-
2// Copyright (c) 1997, 1998 Etienne BERNARD
3// Copyright (c) 2005 Clinton Ebadi
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
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19#include "StringTokenizer.H"
20
21#include <string>
22
23#include <cstring>
24
25StringTokenizer::StringTokenizer(std::string s)
26 : st(s), pos(0)
27{ }
28
29bool
30StringTokenizer::more_tokens_p ()
31{
32 if (pos == st.length())
33 return false;
34
35 for (std::string::size_type i = pos; i < st.length(); i++)
36 if (st[i] != ' ' && st[i] != '\t')
37 return true;
38
39 return false;
40}
41
42bool
43StringTokenizer::more_tokens_p (char c)
44{
45 if (pos == st.length())
46 return false;
47
48 for (std::string::size_type i = pos; i < st.length(); i++)
49 if (st[i] != c)
50 return true;
51
52 return false;
53}
54
55unsigned int
56StringTokenizer::count_tokens ()
57{
58 unsigned int i = 0;
59 StringTokenizer s (st);
60
61 while (s.more_tokens_p ())
62 {
63 s.next_token ();
64 i++;
65 }
66
67 return i;
68}
69
70unsigned int
71StringTokenizer::count_tokens (char c)
72{
73 unsigned int i = 0;
74 StringTokenizer s(st);
75
76 while (s.more_tokens_p (c))
77 {
78 s.next_token (c);
79 i++;
80 }
81
82 return i;
83}
84
85std::string
86StringTokenizer::next_token()
87{
88 std::string::size_type i = pos;
89
90 while (i < st.length() && (st[i] == ' ' || st[i] == '\t'))
91 i++;
92
93 for (std::string::size_type j = i; j < st.length(); j++)
94 if (st[j] == ' ' || st[j] == '\t')
95 {
96 pos = j + 1;
97 return st.substr (i, j - i);
98 }
99
100 pos = st.length();
101 return st.substr (i, st.length() - i);
102}
103
104std::string
105StringTokenizer::next_token (char c, bool empty)
106{
107 std::string::size_type i = pos;
108 std::string::size_type j = 0;
109
110 while (i < st.length() && (st[i] == c))
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 }
123
124 if (empty)
125 return "";
126
127 pos = st.length();
128 return st.substr (i, st.length() - i);
129}
130
131std::string
132StringTokenizer::rest()
133{
134 if (pos == st.length())
135 return "";
136
137 // Skip whitespace
138 while (pos < st.length() && (std::isspace (st[pos])))
139 pos++;
140
141 return st.substr (pos, st.length() - pos);
142}