DocumentationGenerated on Thu Aug 31 00:02:29 2006 |
||
StringTokenizer.cppGo to the documentation of this file.00001 00002 // $Id: StringTokenizer.cpp,v 1.17 2005/04/12 21:18:06 klas Exp $ 00003 // 00004 // Usage: Used to chop up a string in "tokens" 00005 // 00006 // A part of babyftpd, licensed under the GNU GPL, see the file 00007 // LICENSE for complete information. 00009 00010 #define STRINGTOKENIZER_CPP 00011 #include "StringTokenizer.h" 00012 00013 /* 00014 This class is made to handel strings. The StringTokenizer chops a string 00015 up into tokens using delimeters defined in a string passed ass an argument 00016 to the constructor. 00017 00018 The idea of writing this class comes from a similar class in java. but this 00019 is more functional and works in a better way. 00020 00021 The construktor takes three arguments, two strings and one bool. 00022 The first string is the string you want to make tokens of. 00023 The secound string is a string containing all the characters that are 00024 going to cut the string. 00025 The bool determins if the token chars should come with the tokens or not. 00026 If the bool is set to false, the chars used to chop up the string is taken 00027 away. 00028 */ 00029 00030 //Original constructor 00031 StringTokenizer::StringTokenizer(string str, string delim = " \t\n\r\f", 00032 bool returnDelims = false) 00033 { 00034 this->text = str; 00035 this->currentPosition = text.begin(); 00036 this->delimiters = delim; 00037 this->retDelims = returnDelims; 00038 } 00039 00040 //Private function used to get the position of the token. 00041 string::const_iterator 00042 StringTokenizer::scanToken(string::const_iterator startPos) const 00043 { 00044 while (startPos != this->text.end()) 00045 { 00046 if (isDelimiter(*startPos)) 00047 { 00048 if(retDelims) 00049 startPos++; 00050 break; 00051 } 00052 startPos++; 00053 } 00054 return startPos; 00055 } 00056 00057 //Private function that returns a position so that the delimiters is skipped. 00058 string::const_iterator 00059 StringTokenizer::skipDelimiters(string::const_iterator startPos) const 00060 { 00061 while (!retDelims && startPos != this->text.end()) 00062 { 00063 if (!isDelimiter(*startPos) || (delimiters.find(*startPos) == string::npos)) 00064 break; 00065 startPos++; 00066 } 00067 return startPos; 00068 } 00069 00070 //Public function that checks if the string got more tokens. 00071 bool StringTokenizer::hasMoreTokens() const 00072 { 00073 return((skipDelimiters(this->currentPosition) == this->text.end()) 00074 ? false : true); 00075 } 00076 00077 //Public function that returns the next token. 00078 string StringTokenizer::nextToken() 00079 { 00080 this->currentPosition = skipDelimiters(this->currentPosition); 00081 00082 if (this->currentPosition == this->text.end()) 00083 logging->log_this(2, TYPE_INFO, "NoSuchElementException."); 00084 00085 string::const_iterator start = this->currentPosition; 00086 this->currentPosition = scanToken(this->currentPosition); 00087 return string(start, this->currentPosition); 00088 } 00089 00090 //Public functoin that retutns the number of tokens the string is containing. 00091 int StringTokenizer::countTokens() const 00092 { 00093 int count = 0; 00094 string::const_iterator currpos = this->currentPosition; 00095 while (currpos != this->text.end()) { 00096 currpos = skipDelimiters(currpos); 00097 if (currpos == this->text.end()) 00098 break; 00099 currpos = scanToken(currpos); 00100 count++; 00101 } 00102 return count; 00103 } 00104 00105 //Public function that returns all the tokens after the last used. 00106 string StringTokenizer::restTokens(void) const 00107 { 00108 return(string((isDelimiter(*this->currentPosition) ? this->currentPosition 00109 + 1 :this->currentPosition), this->text.end())); 00110 } |
- Copyright © 2005, BabyFTPd
- Powered by: