| 1 | 
tim | 
1736 | 
/* | 
| 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 | 
  | 
  | 
 | 
| 36 | 
  | 
  | 
#define UTIL_STRINGTOKENIZER_HPP | 
| 37 | 
  | 
  | 
 | 
| 38 | 
  | 
  | 
#include <string> | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
//#include "util/NoSuchElementException.hpp" | 
| 41 | 
  | 
  | 
 | 
| 42 | 
  | 
  | 
namespace oopse { | 
| 43 | 
  | 
  | 
 | 
| 44 | 
  | 
  | 
/** | 
| 45 | 
  | 
  | 
 * @class StringTokenizer.hpp "util/StringTokenizer.hpp" | 
| 46 | 
  | 
  | 
 * | 
| 47 | 
  | 
  | 
 * @brief The string tokenizer class allows an application to break a string into tokens | 
| 48 | 
  | 
  | 
 * | 
| 49 | 
  | 
  | 
 * The set of delimiters (the characters that separate tokens) may be specified either  | 
| 50 | 
  | 
  | 
 * at creation time or on a per-token basis.  | 
| 51 | 
  | 
  | 
 * An instance of StringTokenizer behaves in one of two ways, depending on whether it was  | 
| 52 | 
  | 
  | 
 * created with the returnTokens flag having the value true or false. | 
| 53 | 
  | 
  | 
 */ | 
| 54 | 
  | 
  | 
class StringTokenizer { | 
| 55 | 
  | 
  | 
    public: | 
| 56 | 
  | 
  | 
 | 
| 57 | 
  | 
  | 
        static const std::string defaultDelim; | 
| 58 | 
  | 
  | 
        /** | 
| 59 | 
  | 
  | 
         * Constructs a string tokenizer for the specified string. The characters in the delim argument | 
| 60 | 
  | 
  | 
         * are the delimiters for separating tokens. characters are skipped and only serve as  | 
| 61 | 
  | 
  | 
         * separators between tokens. | 
| 62 | 
  | 
  | 
         * @param str a string to be parsed. | 
| 63 | 
  | 
  | 
         * @param delim the delimiters, default value is " \t\n\r". | 
| 64 | 
  | 
  | 
         */ | 
| 65 | 
  | 
  | 
        StringTokenizer(const std::string & str, | 
| 66 | 
  | 
  | 
                        const std::string & delim = defaultDelim); | 
| 67 | 
  | 
  | 
 | 
| 68 | 
  | 
  | 
        StringTokenizer(std::string::const_iterator& first, std::string::const_iterator& last, | 
| 69 | 
  | 
  | 
                        const std::string & delim = defaultDelim); | 
| 70 | 
  | 
  | 
 | 
| 71 | 
  | 
  | 
        /** | 
| 72 | 
  | 
  | 
         * Constructs a string tokenizer for the specified string. The characters in the delim argument | 
| 73 | 
  | 
  | 
         * are the delimiters for separating tokens.  | 
| 74 | 
  | 
  | 
         * If the returnTokens flag is true, then the delimiter characters are also returned as tokens.  | 
| 75 | 
  | 
  | 
         * Each delimiter is returned as a string of length one. If the flag is false, the delimiter  | 
| 76 | 
  | 
  | 
         * characters are skipped and only serve as separators between tokens. | 
| 77 | 
  | 
  | 
         * @param str a string to be parsed.  | 
| 78 | 
  | 
  | 
         * @param delim the delimiters.  | 
| 79 | 
  | 
  | 
         * @param returnTokens flag indicating whether to return the delimiters as tokens. | 
| 80 | 
  | 
  | 
         */ | 
| 81 | 
  | 
  | 
        StringTokenizer(const std::string&str, const std::string&delim, | 
| 82 | 
  | 
  | 
                        bool returnTokens); | 
| 83 | 
  | 
  | 
 | 
| 84 | 
  | 
  | 
        /** | 
| 85 | 
  | 
  | 
         * Calculates the number of times that this tokenizer's nextToken method can be called  | 
| 86 | 
  | 
  | 
         * before it generates an exception. | 
| 87 | 
  | 
  | 
         * @return the number of tokens remaining in the string using the current delimiter set. | 
| 88 | 
  | 
  | 
         */ | 
| 89 | 
  | 
  | 
        int countTokens(); | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
        /** | 
| 92 | 
  | 
  | 
         * Tests if there are more tokens available from this tokenizer's string. | 
| 93 | 
  | 
  | 
         * @return true if there are more tokens available from this tokenizer's string, false otherwise | 
| 94 | 
  | 
  | 
         */ | 
| 95 | 
  | 
  | 
        bool hasMoreTokens(); | 
| 96 | 
  | 
  | 
 | 
| 97 | 
  | 
  | 
        /** | 
| 98 | 
  | 
  | 
         * Returns the next token from this string tokenizer. | 
| 99 | 
  | 
  | 
         * @return the next token from this string tokenizer. | 
| 100 | 
  | 
  | 
         * @exception NoSuchElementException if there are no more tokens in this tokenizer's string | 
| 101 | 
  | 
  | 
         */ | 
| 102 | 
  | 
  | 
        std::string nextToken(); | 
| 103 | 
  | 
  | 
 | 
| 104 | 
  | 
  | 
        /** | 
| 105 | 
  | 
  | 
         * Returns the next token from this string tokenizer as an integer. | 
| 106 | 
  | 
  | 
         * @return the next token from this string tokenizer  as an integer. | 
| 107 | 
  | 
  | 
         */ | 
| 108 | 
  | 
  | 
        int nextTokenAsInt(); | 
| 109 | 
  | 
  | 
 | 
| 110 | 
  | 
  | 
        /** | 
| 111 | 
  | 
  | 
         * Returns the next token from this string tokenizer as a float. | 
| 112 | 
  | 
  | 
         * @return the next token from this string tokenizer as a float. | 
| 113 | 
  | 
  | 
         */ | 
| 114 | 
  | 
  | 
        float nextTokenAsFloat(); | 
| 115 | 
  | 
  | 
 | 
| 116 | 
  | 
  | 
        /** | 
| 117 | 
  | 
  | 
         * Returns the next token without advancing the position of the StringTokenizer. | 
| 118 | 
  | 
  | 
         * @return the next token | 
| 119 | 
  | 
  | 
         */ | 
| 120 | 
  | 
  | 
        std::string  peekNextToken(); | 
| 121 | 
  | 
  | 
 | 
| 122 | 
  | 
  | 
        /** | 
| 123 | 
  | 
  | 
         * Returns the current delimiter set of this string tokenizer | 
| 124 | 
  | 
  | 
         * @return the current delimiter set | 
| 125 | 
  | 
  | 
         */ | 
| 126 | 
  | 
  | 
        const std::string& getDelimiters() { | 
| 127 | 
  | 
  | 
            return delim_; | 
| 128 | 
  | 
  | 
        } | 
| 129 | 
  | 
  | 
 | 
| 130 | 
  | 
  | 
        /**  | 
| 131 | 
  | 
  | 
         * Returns the original string before tokenizing. | 
| 132 | 
  | 
  | 
         * @return the original string before tokenizing  | 
| 133 | 
  | 
  | 
         */ | 
| 134 | 
  | 
  | 
        const std::string& getOriginal() { | 
| 135 | 
  | 
  | 
            return tokenString_; | 
| 136 | 
  | 
  | 
        } | 
| 137 | 
  | 
  | 
 | 
| 138 | 
  | 
  | 
    private: | 
| 139 | 
  | 
  | 
 | 
| 140 | 
  | 
  | 
        /** | 
| 141 | 
  | 
  | 
         * Test if character is in current delimiter set. | 
| 142 | 
  | 
  | 
         * @param c character to be tested | 
| 143 | 
  | 
  | 
         * @return true if character is in current delimiter set, flase otherwise. | 
| 144 | 
  | 
  | 
         */ | 
| 145 | 
  | 
  | 
        bool isDelimiter(char c); | 
| 146 | 
  | 
  | 
 | 
| 147 | 
  | 
  | 
        std::string tokenString_; | 
| 148 | 
  | 
  | 
 | 
| 149 | 
  | 
  | 
        std::string delim_;         /**< current delimiter set of this string tokenizer */ | 
| 150 | 
  | 
  | 
 | 
| 151 | 
  | 
  | 
        bool returnTokens_; /**< flag indicating whether to return the delimiters as tokens */ | 
| 152 | 
  | 
  | 
 | 
| 153 | 
  | 
  | 
        std::string::const_iterator currentPos_; | 
| 154 | 
  | 
  | 
        std::string::const_iterator end_; | 
| 155 | 
  | 
  | 
}; | 
| 156 | 
  | 
  | 
 | 
| 157 | 
  | 
  | 
}                               //namespace oopse | 
| 158 | 
  | 
  | 
 | 
| 159 | 
  | 
  | 
#endif                          //UTIL_STRINGTOKENIZER_HPP |