EasyScanner.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * National Institute of Advanced Industrial Science and Technology (AIST)
8  */
9 
15 #ifndef OPENHRP_UTIL_EASYSCANNER_H_INCLUDED
16 #define OPENHRP_UTIL_EASYSCANNER_H_INCLUDED
17 
18 #include "config.h"
19 
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include <boost/shared_ptr.hpp>
24 
25 namespace hrp {
26 
28 
29  public:
30 
31  class Endl {
32  //int dummy;
33  };
34 
36  public:
37  std::string message;
38  std::string filename;
40  std::string getFullMessage();
41  };
42 
43  enum TokenType {
44  T_NONE = 0, T_SPACE, T_ALPHABET, T_INTEGER, T_DOUBLE, T_WORD,
45  T_STRING, T_SIGLUM, T_LF, T_EOF
46  };
47 
48  typedef std::map<std::string, int> SymbolMap;
49  typedef std::pair<std::string, int> SymbolPair;
50  typedef boost::shared_ptr<SymbolMap> SymbolMapPtr;
51 
53 
54  EasyScanner();
55  EasyScanner(std::string filename);
56  EasyScanner(const EasyScanner& scanner, bool copy_text = false);
57  virtual ~EasyScanner();
58 
59  void putSymbols();
60 
61  inline void registerSymbol(int id, const std::string& symbol) {
62  symbols->insert(SymbolPair(symbol, id));
63  }
64 
65  inline int getSymbolID(const std::string& symbol) {
66  SymbolMap::iterator p = symbols->find(symbol);
67  return (p != symbols->end()) ? p->second : 0;
68  }
69 
71  void setCommentChar(char cc);
72 
73  void setLineOriented(bool on);
74  void setQuoteChar(char qc);
75  void setWhiteSpaceChar(char ws);
76 
77  void loadFile(const std::string& filename);
78 
79  void setText(const char* text, int len);
80 
81  void setLineNumberOffset(int offset);
82 
83  void setDefaultErrorMessage(const std::string& message){
84  defaultErrorMessage = message;
85  }
86 
87  void moveToHead();
88 
89  int readToken();
90 
91  void toLower();
92 
93  bool readDouble();
94  bool readInt();
95  bool readChar();
96  bool readChar(int chara);
97  int peekChar();
98 
103  inline bool readWord() {
104  skipSpace();
105  return readWord0();
106  }
107 
112  inline bool readString(const int delimiterChar = ',') {
113  skipSpace();
114  return readString0(delimiterChar);
115  }
116 
117  bool readString(const char* str);
118 
119  inline bool readString(const std::string& str) {
120  return readString(str.c_str());
121  }
122 
123  bool readQuotedString(bool allowNoQuotedWord = false);
124 
125  bool readUnquotedTextBlock();
126 
127  bool readSymbol();
128  bool readSymbol(int id);
129 
130  inline bool isEOF(){
131  skipSpace();
132  return (*text == '\0');
133  }
134 
136  inline bool readLF() {
137  skipSpace();
138  return readLF0();
139  }
140 
141  inline bool readLFEOF() {
142  skipSpace();
143  return readLF0() ? true : (*text == '\0');
144  }
145 
146  bool checkLF();
147 
148  bool readLine();
149  bool skipLine();
150  bool skipBlankLines();
151 
152  void skipSpace();
153 
154  void throwException(const char* message);
155  void throwException(const std::string& message);
156 
161  int readIntEx(const char* message = 0) {
162  if(!readInt()) throwException(message);
163  return intValue;
164  }
169  double readDoubleEx(const char* message = 0) {
170  if(!readDouble()) throwException(message);
171  return doubleValue;
172  }
177  int readCharEx(const char* message = 0) {
178  if(!readChar())
179  throwException(message);
180  return charValue;
181  }
185  void readCharEx(int chara, const char* message = 0) {
186  if(!readChar(chara)) throwException(message);
187  }
192  std::string readWordEx(const char* message = 0) {
193  if(!readWord()) throwException(message);
194  return stringValue;
195  }
196 
201  std::string readStringEx(const char* message = 0) {
202  if(!readString()) throwException(message);
203  return stringValue;
204  }
205 
206  std::string readQuotedStringEx(const char* message = 0) {
207  if(!readQuotedString()) throwException(message);
208  return stringValue;
209  }
214  int readSymbolEx(const char* message = 0) {
215  if(!readSymbol()) throwException(message);
216  return symbolValue;
217  }
221  void readLFex(const char* message = 0) {
222  if(!readLF()) throwException(message);
223  }
224 
225  void readLFEOFex(const char* message = 0) {
226  if(!readLFEOF()) throwException(message);
227  }
228 
229  int intValue;
230  double doubleValue;
231  std::string stringValue;
232  char charValue;
234 
235  std::string defaultErrorMessage;
237 
238  char* text;
239 
240  std::string filename;
241 
242  private:
243  void init();
244  int extractQuotedString();
245 
246  inline void skipToLineEnd();
247  bool readLF0();
248  bool readWord0();
249  bool readString0(const int delimiterChar);
250 
251  char* textBuf;
252  int size;
253  char* textBufEnd;
258 
259  std::vector<int> whiteSpaceChars;
260 
261  SymbolMapPtr symbols;
262 
263  friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, double& value);
264  friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, int& value);
265  friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, const char* matchString);
266  friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, char matchChar);
267  friend HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, std::string& str);
269 
270  };
271 
272 
275  HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, const char* matchString);
276  HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, char matchChar);
277  HRP_UTIL_EXPORT EasyScanner& operator>>(EasyScanner& scanner, std::string& str);
279 
280  typedef boost::shared_ptr<EasyScanner> EasyScannerPtr;
281 
282 }
283 
284 #endif
void readCharEx(int chara, const char *message=0)
Definition: EasyScanner.h:185
SymbolMapPtr symbols
Definition: EasyScanner.h:261
bool readString(const int delimiterChar=',')
Definition: EasyScanner.h:112
std::string filename
Definition: EasyScanner.h:240
char * filename
Definition: cdjpeg.h:133
HRP_UTIL_EXPORT EasyScanner & operator>>(EasyScanner &scanner, double &value)
bool readLF()
reading a line feed
Definition: EasyScanner.h:136
png_voidp int value
Definition: png.h:2113
double readDoubleEx(const char *message=0)
Definition: EasyScanner.h:169
string message
int readSymbolEx(const char *message=0)
Definition: EasyScanner.h:214
std::string readWordEx(const char *message=0)
Definition: EasyScanner.h:192
int readCharEx(const char *message=0)
Definition: EasyScanner.h:177
void setDefaultErrorMessage(const std::string &message)
Definition: EasyScanner.h:83
std::string readStringEx(const char *message=0)
Definition: EasyScanner.h:201
std::pair< std::string, int > SymbolPair
Definition: EasyScanner.h:49
std::vector< int > whiteSpaceChars
Definition: EasyScanner.h:259
#define HRP_UTIL_EXPORT
void registerSymbol(int id, const std::string &symbol)
Definition: EasyScanner.h:61
std::map< std::string, int > SymbolMap
Definition: EasyScanner.h:48
bool readString(const std::string &str)
Definition: EasyScanner.h:119
void toLower(std::string &str)
int readIntEx(const char *message=0)
Definition: EasyScanner.h:161
int getSymbolID(const std::string &symbol)
Definition: EasyScanner.h:65
std::string stringValue
Definition: EasyScanner.h:231
void readLFex(const char *message=0)
Definition: EasyScanner.h:221
void readLFEOFex(const char *message=0)
Definition: EasyScanner.h:225
std::string readQuotedStringEx(const char *message=0)
Definition: EasyScanner.h:206
boost::shared_ptr< EasyScanner > EasyScannerPtr
Definition: EasyScanner.h:280
boost::shared_ptr< SymbolMap > SymbolMapPtr
Definition: EasyScanner.h:50
std::string defaultErrorMessage
Definition: EasyScanner.h:235


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03