Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00015 #ifndef OPENHRP_UTIL_EASYSCANNER_H_INCLUDED
00016 #define OPENHRP_UTIL_EASYSCANNER_H_INCLUDED
00017
00018 #include "config.h"
00019
00020 #include <map>
00021 #include <string>
00022 #include <vector>
00023 #include <boost/shared_ptr.hpp>
00024
00025 namespace hrp {
00026
00027 class HRP_UTIL_EXPORT EasyScanner {
00028
00029 public:
00030
00031 class Endl {
00032
00033 };
00034
00035 class HRP_UTIL_EXPORT Exception {
00036 public:
00037 std::string message;
00038 std::string filename;
00039 int lineNumber;
00040 std::string getFullMessage();
00041 };
00042
00043 enum TokenType {
00044 T_NONE = 0, T_SPACE, T_ALPHABET, T_INTEGER, T_DOUBLE, T_WORD,
00045 T_STRING, T_SIGLUM, T_LF, T_EOF
00046 };
00047
00048 typedef std::map<std::string, int> SymbolMap;
00049 typedef std::pair<std::string, int> SymbolPair;
00050 typedef boost::shared_ptr<SymbolMap> SymbolMapPtr;
00051
00052 Endl endl;
00053
00054 EasyScanner();
00055 EasyScanner(std::string filename);
00056 EasyScanner(const EasyScanner& scanner, bool copy_text = false);
00057 virtual ~EasyScanner();
00058
00059 void putSymbols();
00060
00061 inline void registerSymbol(int id, const std::string& symbol) {
00062 symbols->insert(SymbolPair(symbol, id));
00063 }
00064
00065 inline int getSymbolID(const std::string& symbol) {
00066 SymbolMap::iterator p = symbols->find(symbol);
00067 return (p != symbols->end()) ? p->second : 0;
00068 }
00069
00071 void setCommentChar(char cc);
00072
00073 void setLineOriented(bool on);
00074 void setQuoteChar(char qc);
00075 void setWhiteSpaceChar(char ws);
00076
00077 void loadFile(const std::string& filename);
00078
00079 void setText(const char* text, int len);
00080
00081 void setLineNumberOffset(int offset);
00082
00083 void setDefaultErrorMessage(const std::string& message){
00084 defaultErrorMessage = message;
00085 }
00086
00087 void moveToHead();
00088
00089 int readToken();
00090
00091 void toLower();
00092
00093 bool readDouble();
00094 bool readInt();
00095 bool readChar();
00096 bool readChar(int chara);
00097 int peekChar();
00098
00103 inline bool readWord() {
00104 skipSpace();
00105 return readWord0();
00106 }
00107
00112 inline bool readString(const int delimiterChar = ',') {
00113 skipSpace();
00114 return readString0(delimiterChar);
00115 }
00116
00117 bool readString(const char* str);
00118
00119 inline bool readString(const std::string& str) {
00120 return readString(str.c_str());
00121 }
00122
00123 bool readQuotedString(bool allowNoQuotedWord = false);
00124
00125 bool readUnquotedTextBlock();
00126
00127 bool readSymbol();
00128 bool readSymbol(int id);
00129
00130 inline bool isEOF(){
00131 skipSpace();
00132 return (*text == '\0');
00133 }
00134
00136 inline bool readLF() {
00137 skipSpace();
00138 return readLF0();
00139 }
00140
00141 inline bool readLFEOF() {
00142 skipSpace();
00143 return readLF0() ? true : (*text == '\0');
00144 }
00145
00146 bool checkLF();
00147
00148 bool readLine();
00149 bool skipLine();
00150 bool skipBlankLines();
00151
00152 void skipSpace();
00153
00154 void throwException(const char* message);
00155 void throwException(const std::string& message);
00156
00161 int readIntEx(const char* message = 0) {
00162 if(!readInt()) throwException(message);
00163 return intValue;
00164 }
00169 double readDoubleEx(const char* message = 0) {
00170 if(!readDouble()) throwException(message);
00171 return doubleValue;
00172 }
00177 int readCharEx(const char* message = 0) {
00178 if(!readChar())
00179 throwException(message);
00180 return charValue;
00181 }
00185 void readCharEx(int chara, const char* message = 0) {
00186 if(!readChar(chara)) throwException(message);
00187 }
00192 std::string readWordEx(const char* message = 0) {
00193 if(!readWord()) throwException(message);
00194 return stringValue;
00195 }
00196
00201 std::string readStringEx(const char* message = 0) {
00202 if(!readString()) throwException(message);
00203 return stringValue;
00204 }
00205
00206 std::string readQuotedStringEx(const char* message = 0) {
00207 if(!readQuotedString()) throwException(message);
00208 return stringValue;
00209 }
00214 int readSymbolEx(const char* message = 0) {
00215 if(!readSymbol()) throwException(message);
00216 return symbolValue;
00217 }
00221 void readLFex(const char* message = 0) {
00222 if(!readLF()) throwException(message);
00223 }
00224
00225 void readLFEOFex(const char* message = 0) {
00226 if(!readLFEOF()) throwException(message);
00227 }
00228
00229 int intValue;
00230 double doubleValue;
00231 std::string stringValue;
00232 char charValue;
00233 int symbolValue;
00234
00235 std::string defaultErrorMessage;
00236 int lineNumber;
00237
00238 char* text;
00239
00240 std::string filename;
00241
00242 private:
00243 void init();
00244 int extractQuotedString();
00245
00246 inline void skipToLineEnd();
00247 bool readLF0();
00248 bool readWord0();
00249 bool readString0(const int delimiterChar);
00250
00251 char* textBuf;
00252 int size;
00253 char* textBufEnd;
00254 int lineNumberOffset;
00255 int commentChar;
00256 int quoteChar;
00257 bool isLineOriented;
00258
00259 std::vector<int> whiteSpaceChars;
00260
00261 SymbolMapPtr symbols;
00262
00263 friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, double& value);
00264 friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, int& value);
00265 friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, const char* matchString);
00266 friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, char matchChar);
00267 friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, std::string& str);
00268 friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, EasyScanner::Endl endl);
00269
00270 };
00271
00272
00273 HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, double& value);
00274 HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, int& value);
00275 HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, const char* matchString);
00276 HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, char matchChar);
00277 HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, std::string& str);
00278 HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, EasyScanner::Endl endl);
00279
00280 typedef boost::shared_ptr<EasyScanner> EasyScannerPtr;
00281
00282 }
00283
00284 #endif