| 1 | /* | 
| 2 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 | * | 
| 4 | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | * non-exclusive, royalty free, license to use, modify and | 
| 6 | * redistribute this software in source and binary code form, provided | 
| 7 | * that the following conditions are met: | 
| 8 | * | 
| 9 | * 1. Acknowledgement of the program authors must be made in any | 
| 10 | *    publication of scientific results based in part on use of the | 
| 11 | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | *    the article in which the program was described (Matthew | 
| 13 | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | * | 
| 18 | * 2. Redistributions of source code must retain the above copyright | 
| 19 | *    notice, this list of conditions and the following disclaimer. | 
| 20 | * | 
| 21 | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 | *    notice, this list of conditions and the following disclaimer in the | 
| 23 | *    documentation and/or other materials provided with the | 
| 24 | *    distribution. | 
| 25 | * | 
| 26 | * This software is provided "AS IS," without a warranty of any | 
| 27 | * kind. All express or implied conditions, representations and | 
| 28 | * warranties, including any implied warranty of merchantability, | 
| 29 | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 | * be liable for any damages suffered by licensee as a result of | 
| 32 | * using, modifying or distributing the software or its | 
| 33 | * derivatives. In no event will the University of Notre Dame or its | 
| 34 | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 | * direct, indirect, special, consequential, incidental or punitive | 
| 36 | * damages, however caused and regardless of the theory of liability, | 
| 37 | * arising out of the use of or inability to use software, even if the | 
| 38 | * University of Notre Dame has been advised of the possibility of | 
| 39 | * such damages. | 
| 40 | */ | 
| 41 |  | 
| 42 | #include <iostream> | 
| 43 | #include <iterator> | 
| 44 | #include <sstream> | 
| 45 | #include "utils/StringTokenizer.hpp" | 
| 46 |  | 
| 47 | namespace oopse { | 
| 48 |  | 
| 49 |  | 
| 50 | StringTokenizer::StringTokenizer(const std::string & str, const std::string & delim) | 
| 51 | : tokenString_(str), delim_(delim), returnTokens_(false), | 
| 52 | currentPos_(tokenString_.begin()), end_(tokenString_.end()){ | 
| 53 |  | 
| 54 | } | 
| 55 |  | 
| 56 | StringTokenizer::StringTokenizer(std::string::const_iterator& first, std::string::const_iterator& last, | 
| 57 | const std::string & delim) | 
| 58 | : tokenString_(first, last) , delim_(delim), returnTokens_(false), | 
| 59 | currentPos_(tokenString_.begin()), end_(tokenString_.end()) { | 
| 60 |  | 
| 61 | } | 
| 62 |  | 
| 63 | StringTokenizer::StringTokenizer(const std::string&str, const std::string&delim, | 
| 64 | bool returnTokens) | 
| 65 | : tokenString_(str), delim_(delim), returnTokens_(returnTokens), | 
| 66 | currentPos_(tokenString_.begin()), end_(tokenString_.end()) { | 
| 67 |  | 
| 68 | } | 
| 69 |  | 
| 70 | bool StringTokenizer::isDelimiter(const char c) { | 
| 71 | return delim_.find(c) == std::string::npos ? false : true; | 
| 72 | } | 
| 73 |  | 
| 74 | int StringTokenizer::countTokens() { | 
| 75 |  | 
| 76 | std::string::const_iterator tmpIter = currentPos_; | 
| 77 | int numToken = 0; | 
| 78 |  | 
| 79 | while (true) { | 
| 80 |  | 
| 81 | //skip delimiter first | 
| 82 | while( tmpIter != end_ && isDelimiter(*tmpIter)) { | 
| 83 | ++tmpIter; | 
| 84 |  | 
| 85 | if (returnTokens_) { | 
| 86 | //if delimiter is consider as token | 
| 87 | ++numToken; | 
| 88 | } | 
| 89 | } | 
| 90 |  | 
| 91 | if (tmpIter == end_) { | 
| 92 | break; | 
| 93 | } | 
| 94 |  | 
| 95 | //encount a token here | 
| 96 | while ( tmpIter != end_ && !isDelimiter(*tmpIter) ) { | 
| 97 | ++tmpIter; | 
| 98 | } | 
| 99 |  | 
| 100 | ++numToken; | 
| 101 |  | 
| 102 | } | 
| 103 |  | 
| 104 | return numToken; | 
| 105 | } | 
| 106 |  | 
| 107 | bool StringTokenizer::hasMoreTokens() { | 
| 108 |  | 
| 109 | if (currentPos_ == end_) { | 
| 110 | return false; | 
| 111 | } else if (returnTokens_) { | 
| 112 | return true; | 
| 113 | } else { | 
| 114 | std::string::const_iterator i = currentPos_; | 
| 115 |  | 
| 116 | //walk through the remaining string to check whether it contains non-delimeter or not | 
| 117 | while(i != end_ && isDelimiter(*i)) { | 
| 118 | ++i; | 
| 119 | } | 
| 120 |  | 
| 121 | return i != end_ ? true : false; | 
| 122 | } | 
| 123 | } | 
| 124 |  | 
| 125 | std::string StringTokenizer::nextToken() { | 
| 126 | std::string result; | 
| 127 |  | 
| 128 | if(currentPos_ != end_) { | 
| 129 | std::insert_iterator<std::string> insertIter(result, result.begin()); | 
| 130 |  | 
| 131 | while( currentPos_ != end_ && isDelimiter(*currentPos_)) { | 
| 132 |  | 
| 133 | if (returnTokens_) { | 
| 134 | *insertIter++ = *currentPos_++; | 
| 135 | return result; | 
| 136 | } | 
| 137 |  | 
| 138 | ++currentPos_; | 
| 139 | } | 
| 140 |  | 
| 141 | while (currentPos_ != end_ && !isDelimiter(*currentPos_)) { | 
| 142 | *insertIter++ = *currentPos_++; | 
| 143 | } | 
| 144 |  | 
| 145 | } | 
| 146 |  | 
| 147 | return result; | 
| 148 | } | 
| 149 |  | 
| 150 | bool StringTokenizer::nextTokenAsBool() { | 
| 151 | std::string token = nextToken(); | 
| 152 | std::istringstream iss(token); | 
| 153 | bool result; | 
| 154 |  | 
| 155 | if (iss >> result) { | 
| 156 | return result; | 
| 157 | } else { | 
| 158 | std::cerr << "unable to convert " << token << " to a bool" << std::endl; | 
| 159 | return false; | 
| 160 | } | 
| 161 | } | 
| 162 |  | 
| 163 | //Since libstdc++(GCC 3.2) has an i/ostream::operator>>/<<(streambuf*) bug (Bug 9318) | 
| 164 | //Instead of using iostream facility, we use C library | 
| 165 | int StringTokenizer::nextTokenAsInt() { | 
| 166 | std::string token = nextToken(); | 
| 167 |  | 
| 168 | return atoi(token.c_str()); | 
| 169 | } | 
| 170 |  | 
| 171 | float StringTokenizer::nextTokenAsFloat() { | 
| 172 | std::string token = nextToken(); | 
| 173 | convertFortranNumber(token); | 
| 174 | return (float) (atof(token.c_str())); | 
| 175 | } | 
| 176 |  | 
| 177 | RealType StringTokenizer::nextTokenAsDouble() { | 
| 178 | std::string token = nextToken(); | 
| 179 | convertFortranNumber(token); | 
| 180 | return atof(token.c_str()); | 
| 181 | } | 
| 182 |  | 
| 183 | std::string  StringTokenizer::peekNextToken() { | 
| 184 | std::string result; | 
| 185 | std::string::const_iterator tmpIter = currentPos_; | 
| 186 |  | 
| 187 | if(tmpIter != end_) { | 
| 188 | std::insert_iterator<std::string> insertIter(result, result.begin()); | 
| 189 |  | 
| 190 | while(tmpIter != end_ && isDelimiter(*tmpIter)) { | 
| 191 |  | 
| 192 | if (returnTokens_) { | 
| 193 | *insertIter++ = *tmpIter++; | 
| 194 | return result; | 
| 195 | } | 
| 196 |  | 
| 197 | ++tmpIter; | 
| 198 | } | 
| 199 |  | 
| 200 | while (tmpIter != end_ && !isDelimiter(*tmpIter)) { | 
| 201 | *insertIter++ = *tmpIter++; | 
| 202 | } | 
| 203 | } | 
| 204 |  | 
| 205 | return result; | 
| 206 | } | 
| 207 |  | 
| 208 | std::vector<std::string>  StringTokenizer::getAllTokens() { | 
| 209 | std::vector<std::string> tokens; | 
| 210 | while (hasMoreTokens()) { | 
| 211 | tokens.push_back(nextToken()); | 
| 212 | } | 
| 213 | return tokens; | 
| 214 | } | 
| 215 | void StringTokenizer::convertFortranNumber(std::string& fortranNumber) { | 
| 216 | std::string::iterator i; | 
| 217 | for(i = fortranNumber.begin(); i != fortranNumber.end(); ++i) { | 
| 218 | if (*i == 'd' || *i == 'D') { | 
| 219 | *i = 'E'; | 
| 220 | } | 
| 221 | } | 
| 222 | } | 
| 223 |  | 
| 224 | }//end namespace oopse | 
| 225 |  |