|
Main Page
Class Hierarchy
Alphabetical List
Compound List
File List
Compound Members
|
00001 /********************************************************************************
00002 * *
00003 * S t r i n g T o k e n i z e r C l a s s *
00004 * *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson. All Rights Reserved. *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or *
00009 * modify it under the terms of the GNU Lesser General Public *
00010 * License as published by the Free Software Foundation; either *
00011 * version 2.1 of the License, or (at your option) any later version. *
00012 * *
00013 * This library is distributed in the hope that it will be useful, *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
00016 * Lesser General Public License for more details. *
00017 * *
00018 * You should have received a copy of the GNU Lesser General Public *
00019 * License along with this library; if not, write to the Free Software *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
00021 *********************************************************************************/
00022 #ifndef FXSTRINGTOKENIZER_H
00023 #define FXSTRINGTOKENIZER_H
00024
00025 namespace FXEX {
00026
00027 /// String tokenizer options
00028 enum {
00029 TOKENIZER_NORMAL=0,
00030 TOKENIZER_QUOTE_SINGLE=0x00000001, // dont break into tokens within single quotes
00031 TOKENIZER_QUOTE_DOUBLE=0x00000002 // dont break into tokens within double quotes
00032 };
00033
00034 /**
00035 * Tokenise a string by the seperators.
00036 *
00037 * Note:
00038 * 1. The seperators are single characters such as tab, comma's.
00039 * 2. You can use multiple seperators at any one time.
00040 * 3. It will skip over multiple adjacent seperators, so as to retrieve the next/previous token.
00041 * 4. You can enable single and/or double quoting mode so as to avoid seperating tokens
00042 * within the single/double quotes. If a double quote occurs inside a pair of single
00043 * quotes, then the double quote will be broken, since it occurs within a matched pair.
00044 * This applies to the other case - a single quote will be broken if matched within
00045 * a double quote pair.
00046 * 5. Double/single quotes can be uses as the seperators of tokens, though it wont make
00047 * sense when used with the TOKENIZER_QUOTE_xxx modes.
00048 */
00049 class FXAPI FXStringTokenizer {
00050 protected:
00051 FXString str; // the string to be broken up
00052 FXString seperators; // seperators that breaks up the tokens
00053 FXint index; // the current location within the string
00054 FXuint options; // string options
00055
00056 protected:
00057 /// indicates if a character is one of the seperators
00058 FXbool isSep(const FXchar c);
00059
00060 public:
00061 /// constuctors
00062 FXStringTokenizer(const FXString& s="",const FXString& seps=" \t\n\r",FXuint opts=TOKENIZER_NORMAL);
00063 FXStringTokenizer(const FXString& s,const FXchar* seps,FXuint opts=TOKENIZER_NORMAL);
00064 FXStringTokenizer(const FXString& s,const FXchar sep,FXuint opts=TOKENIZER_NORMAL);
00065
00066 /// length of text
00067 FXint length() { return str.length(); }
00068
00069 /// get the next token
00070 FXString next();
00071
00072 /// has more next tokens
00073 FXbool hasNext();
00074
00075 /// get the previous token
00076 FXString prev();
00077
00078 /// has more previous tokens
00079 FXbool hasPrev();
00080
00081 /// reset the tokenizer to the start of the string
00082 void reset() { index=-1; }
00083
00084 /// reset the tokenizer to the end of the string (so you can traverse backwards)
00085 void resetEnd() { index=str.length(); }
00086
00087 /// get the remaining text left at the end of the string
00088 FXString remaining() const { return str.right(str.length()-index+1); }
00089
00090 /// get the remaining text left at the start of the string
00091 FXString begining() const { return str.left(index); }
00092
00093 /// set the seperators
00094 void setSeperator(const FXchar sep) { seperators=FXString(sep,1); }
00095 void setSeperator(const FXchar* seps) { seperators=seps; }
00096 void setSeperators(const FXString& seps) { seperators=seps; }
00097
00098 /// add other seperators to the current set
00099 void addSeperator(const FXchar sep) { seperators+=FXString(sep,1); }
00100 void addSeperators(const FXchar* seps) { seperators+=seps; }
00101 void addSeperators(const FXString& seps) { seperators+=seps; }
00102
00103 /// get the current seperators
00104 FXString getSeperators() const { return seperators; }
00105
00106 /// set the tokenized string to a new string
00107 void setText(const FXString& s);
00108
00109 /// get the text
00110 FXString getText() const { return str; }
00111
00112 /// set the tokenized string to a another tokenizer
00113 FXStringTokenizer& operator=(const FXStringTokenizer& s);
00114
00115 /// set the tokenized string to a new string
00116 FXStringTokenizer& operator=(const FXString& s);
00117
00118 /// Return a non-const reference to the ith character
00119 FXchar& operator[](FXint i){ return str[i]; }
00120
00121 /// Return a const reference to the ith character
00122 const FXchar& operator[](FXint i) const { return str[i]; }
00123
00124 /// Comparison operators ==
00125 friend FXAPI FXbool operator==(const FXStringTokenizer &s1,const FXStringTokenizer &s2);
00126 friend FXAPI FXbool operator==(const FXString &s1,const FXStringTokenizer &s2);
00127 friend FXAPI FXbool operator==(const FXStringTokenizer &s1,const FXString &s2);
00128
00129 /// Comparison operators !=
00130 friend FXAPI FXbool operator!=(const FXStringTokenizer &s1,const FXStringTokenizer &s2);
00131 friend FXAPI FXbool operator!=(const FXString &s1,const FXStringTokenizer &s2);
00132 friend FXAPI FXbool operator!=(const FXStringTokenizer &s1,const FXString &s2);
00133
00134 /// Saving to a stream
00135 friend FXAPI FXStream& operator<<(FXStream& store,const FXStringTokenizer& s);
00136
00137 /// Load from a stream
00138 friend FXAPI FXStream& operator>>(FXStream& store,FXStringTokenizer& s);
00139
00140 /// dtor
00141 virtual ~FXStringTokenizer();
00142 };
00143
00144 } // namespace FXEX
00145 #endif // FXSTRINGTOKENIZER_H