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