iipsrv 0.9.9
|
00001 /* 00002 Simple String Tokenizer Class 00003 00004 Copyright (C) 2000-2001 Ruven Pillay. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 00022 #ifndef _TOKENIZER_H 00023 #define _TOKENIZER_H 00024 00025 #include <string> 00026 00027 00029 00030 class Tokenizer{ 00031 00032 private: 00033 std::string arg; 00034 std::string delim; 00035 std::string _nextToken(); 00036 00037 public: 00038 00040 00043 Tokenizer( const std::string& s, const std::string& d ); 00044 00046 std::string nextToken(); 00047 00049 int hasMoreTokens(); 00050 00051 }; 00052 00053 00054 00055 inline Tokenizer::Tokenizer( const std::string& s, const std::string& t ) 00056 { 00057 arg = s; 00058 delim = t; 00059 } 00060 00061 00062 00063 inline std::string Tokenizer::_nextToken() 00064 { 00065 int n; 00066 std::string result; 00067 00068 n = arg.find( delim ); 00069 00070 // No token in string, so return original 00071 if( n < 0 ){ 00072 result = arg; 00073 arg = std::string(); 00074 } 00075 else{ 00076 result = arg.substr( 0, n ); 00077 arg = arg.substr( n + delim.length(), arg.length() ); 00078 } 00079 00080 return result; 00081 } 00082 00083 00084 00085 inline std::string Tokenizer::nextToken() 00086 { 00087 std::string result; 00088 do{ 00089 result = _nextToken(); 00090 } 00091 while( result.empty() && !arg.empty() ); 00092 00093 return result; 00094 00095 } 00096 00097 00098 inline int Tokenizer::hasMoreTokens() 00099 { 00100 int n = arg.find_first_not_of( delim, 0 ); 00101 if( n >= 0 ) return 1; 00102 else return 0; 00103 } 00104 00105 00106 00107 #endif