| 1 | tim | 1594 | /* | 
| 2 |  |  | * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 |  |  | * | 
| 4 |  |  | * Contact: oopse@oopse.org | 
| 5 |  |  | * | 
| 6 |  |  | * This program is free software; you can redistribute it and/or | 
| 7 |  |  | * modify it under the terms of the GNU Lesser General Public License | 
| 8 |  |  | * as published by the Free Software Foundation; either version 2.1 | 
| 9 |  |  | * of the License, or (at your option) any later version. | 
| 10 |  |  | * All we ask is that proper credit is given for our work, which includes | 
| 11 |  |  | * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 |  |  | * of your source code files, and to any copyright notice that you may distribute | 
| 13 |  |  | * with programs based on this work. | 
| 14 |  |  | * | 
| 15 |  |  | * This program is distributed in the hope that it will be useful, | 
| 16 |  |  | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 |  |  | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 |  |  | * GNU Lesser General Public License for more details. | 
| 19 |  |  | * | 
| 20 |  |  | * You should have received a copy of the GNU Lesser General Public License | 
| 21 |  |  | * along with this program; if not, write to the Free Software | 
| 22 |  |  | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 23 |  |  | * | 
| 24 |  |  | */ | 
| 25 |  |  |  | 
| 26 |  |  | /** | 
| 27 |  |  | * @file StringTokenizer.hpp | 
| 28 |  |  | * @author tlin | 
| 29 |  |  | * @date 09/20/2004 | 
| 30 |  |  | * @time 11:30am | 
| 31 |  |  | * @version 1.0 | 
| 32 |  |  | */ | 
| 33 |  |  |  | 
| 34 |  |  | #ifndef UTIL_STRINGTOKENIZER_HPP | 
| 35 |  |  | #define UTIL_STRINGTOKENIZER_HPP | 
| 36 |  |  |  | 
| 37 |  |  | #include <vector> | 
| 38 |  |  |  | 
| 39 |  |  | #include "util/NoSuchElementException.hpp" | 
| 40 |  |  |  | 
| 41 |  |  | namespace oopse{ | 
| 42 |  |  |  | 
| 43 |  |  | /** | 
| 44 |  |  | * @class StringTokenizer.hpp "util/StringTokenizer.hpp" | 
| 45 |  |  | * | 
| 46 |  |  | * @brief The string tokenizer class allows an application to break a string into tokens | 
| 47 |  |  | * | 
| 48 |  |  | * The set of delimiters (the characters that separate tokens) may be specified either | 
| 49 |  |  | * at creation time or on a per-token basis. | 
| 50 |  |  | * An instance of StringTokenizer behaves in one of two ways, depending on whether it was | 
| 51 |  |  | * created with the returnTokens flag having the value true or false. | 
| 52 |  |  | */ | 
| 53 |  |  | class StringTokenizer{ | 
| 54 |  |  |  | 
| 55 |  |  | public: | 
| 56 |  |  |  | 
| 57 |  |  | /** | 
| 58 |  |  | * Constructs a string tokenizer for the specified string. The characters in the delim argument | 
| 59 |  |  | * are the delimiters for separating tokens. characters are skipped and only serve as | 
| 60 |  |  | * separators between tokens. | 
| 61 |  |  | * @param str a string to be parsed. | 
| 62 |  |  | * @param delim the delimiters, default value is "\t\n\r". | 
| 63 |  |  | */ | 
| 64 |  |  | StringTokenizer(const std::string& str, const std::string& delim = "\t\n\r"); | 
| 65 |  |  |  | 
| 66 |  |  | /** | 
| 67 |  |  | * Constructs a string tokenizer for the specified string. The characters in the delim argument | 
| 68 |  |  | * are the delimiters for separating tokens. | 
| 69 |  |  | * If the returnTokens flag is true, then the delimiter characters are also returned as tokens. | 
| 70 |  |  | * Each delimiter is returned as a string of length one. If the flag is false, the delimiter | 
| 71 |  |  | * characters are skipped and only serve as separators between tokens. | 
| 72 |  |  | * @param str a string to be parsed. | 
| 73 |  |  | * @param delim the delimiters. | 
| 74 |  |  | * @param returnTokens flag indicating whether to return the delimiters as tokens. | 
| 75 |  |  | */ | 
| 76 |  |  | StringTokenizer(const std::string& str, const std::string& delim, bool returnTokens); | 
| 77 |  |  |  | 
| 78 |  |  | /** | 
| 79 |  |  | * Calculates the number of times that this tokenizer's nextToken method can be called | 
| 80 |  |  | * before it generates an exception. | 
| 81 |  |  | * | 
| 82 |  |  | * @return the number of tokens remaining in the string using the current delimiter set. | 
| 83 |  |  | */ | 
| 84 |  |  | int countTokens(); | 
| 85 |  |  |  | 
| 86 |  |  | /** | 
| 87 |  |  | * Tests if there are more tokens available from this tokenizer's string. | 
| 88 |  |  | * | 
| 89 |  |  | * @return true if there are more tokens available from this tokenizer's string, false otherwise | 
| 90 |  |  | */ | 
| 91 |  |  | bool hasMoreTokens(); | 
| 92 |  |  |  | 
| 93 |  |  | /** | 
| 94 |  |  | * Returns the next token from this string tokenizer. | 
| 95 |  |  | * | 
| 96 |  |  | * @return the next token from this string tokenizer. | 
| 97 |  |  | * | 
| 98 |  |  | * @exception NoSuchElementException if there are no more tokens in this tokenizer's string | 
| 99 |  |  | */ | 
| 100 |  |  | std::string nextToken(); | 
| 101 |  |  |  | 
| 102 |  |  | /** | 
| 103 |  |  | * Returns the next token in this string tokenizer's string. The new delimiter set remains the | 
| 104 |  |  | * default after this call. | 
| 105 |  |  | * | 
| 106 |  |  | * @param newDelim the new delimiters. | 
| 107 |  |  | * | 
| 108 |  |  | * @return the next token, after switching to the new delimiter set. | 
| 109 |  |  | * | 
| 110 |  |  | * @exception NoSuchElementException if there are no more tokens in this tokenizer's string. | 
| 111 |  |  | * | 
| 112 |  |  | */ | 
| 113 |  |  | std::string nextToken(const std::string& newDelim); | 
| 114 |  |  |  | 
| 115 |  |  | /** | 
| 116 |  |  | * Returns the current delimiter set of this string tokenizer | 
| 117 |  |  | * | 
| 118 |  |  | * @return the current delimiter set | 
| 119 |  |  | */ | 
| 120 |  |  | std::string getDelimiter(); | 
| 121 |  |  |  | 
| 122 |  |  | private: | 
| 123 |  |  |  | 
| 124 |  |  | /** | 
| 125 |  |  | * Test if character is in current delimiter set. | 
| 126 |  |  | * | 
| 127 |  |  | * @param c character to be tested | 
| 128 |  |  | * | 
| 129 |  |  | * @return true if character is in current delimiter set, flase otherwise. | 
| 130 |  |  | */ | 
| 131 |  |  | bool isDelimiter(char c); | 
| 132 |  |  |  | 
| 133 |  |  | std::string delim_;  /**< current delimiter set of this string tokenizer */ | 
| 134 |  |  |  | 
| 135 |  |  | bool returnTokens_; /**< flag indicating whether to return the delimiters as tokens */ | 
| 136 |  |  | }; | 
| 137 |  |  |  | 
| 138 |  |  |  | 
| 139 |  |  | } //namespace oopse | 
| 140 |  |  | #endif //UTIL_STRINGTOKENIZER_HPP |