00001 // Author: stephan beal <stephan@s11n.net> 00002 // License: Public Domain 00003 #ifndef s11n_STDSTRINGTOKENIZER_H 00004 #define s11n_STDSTRINGTOKENIZER_H 00005 00006 #include <string> 00007 #include <queue> 00008 00009 namespace s11n 00010 { 00011 using std::string; 00012 /** 00013 stdstring_tokenizer: 00014 00015 License: Public Domain 00016 Author: stephan@s11n.net 00017 00018 Based heavily off of work by: 00019 00020 Martin Jones (mjones@kde.org) 00021 Torben Weis (weis@kde.org) 00022 Waldo Bastian (bastian@kde.org) 00023 00024 which i originally found as string_tokenizer in the KDE 1.x 00025 source tree. i have received explicit permission from each 00026 of those gentlemen to release the string_tokenizer code into 00027 into the Public Domain. (Many thanks to them for that 00028 permission, without which this whole library would 00029 necessarily be released under the GNU GPL.) 00030 00031 This class is meant to be API- and behaviour-compatible 00032 with string_tokenizer. This implementation is, however, MUCH 00033 less efficient. 00034 00035 stdstring_tokenizer tokenizes strings in a way which is 00036 consistent with the way a Unix shell does. This makes it 00037 appropriate for use in parsing many types of arbitrary user 00038 input, from command-line arguments to comma-separated 00039 files. 00040 */ 00041 class stdstring_tokenizer 00042 { 00043 public: 00044 stdstring_tokenizer(); 00045 /** 00046 Same as creating a stdstring_tokenizer and calling it's tokenize( str, separators ). 00047 */ 00048 stdstring_tokenizer( const string & str, const string & separators ); 00049 ~stdstring_tokenizer(); 00050 00051 /** 00052 str is split up at points matching any element in 00053 separators. Adjecent separators in str are 00054 interpreted as empty elements. Thus the string 00055 "1;;3", separated by ";", has 3 tokens: 00056 ("1","","3"). 00057 00058 To collect the tokens, do this: 00059 00060 <pre> 00061 stdstring_tokenizer tok( "some string", " " ); 00062 while( tok.has_tokens() ) cout << "Token: " << tok.next_token() << endl; 00063 </pre> 00064 */ 00065 void tokenize( const string & str, const string & separators ); 00066 /** 00067 Returns the next token in our list. 00068 Calling next_token() when has_tokens() returns 00069 false has undefined behaviour. 00070 */ 00071 string next_token(); 00072 /** 00073 Returns true if this object has more tokens to give you. 00074 */ 00075 bool has_tokens() const; 00076 00077 private: 00078 typedef std::queue < std::string > queue_type; 00079 //StringList m_list; 00080 queue_type m_list; 00081 }; 00082 00083 }; // namespace s11n 00084 #endif // s11n_STDSTRINGTOKENIZER_H