Choreonoid  1.8
EasyScanner.h
Go to the documentation of this file.
1 
7 #ifndef CNOID_UTIL_EASYSCANNER_H_INCLUDED
8 #define CNOID_UTIL_EASYSCANNER_H_INCLUDED
9 
10 #include <unordered_map>
11 #include <string>
12 #include <vector>
13 #include <memory>
14 #include "exportdecl.h"
15 
16 namespace cnoid {
17 
21 class CNOID_EXPORT EasyScanner {
22 
23 public:
24 
25  class Endl {
26  //int dummy;
27  };
28 
29  class CNOID_EXPORT Exception {
30 public:
31  std::string message;
32  std::string filename;
34  std::string getFullMessage() const;
35  };
36 
37  enum TokenType {
38  T_NONE = 0, T_SPACE, T_ALPHABET, T_INTEGER, T_DOUBLE, T_WORD,
39  T_STRING, T_SIGLUM, T_LF, T_EOF
40  };
41 
42  typedef std::unordered_map<std::string, int> SymbolMap;
43  typedef std::pair<std::string, int> SymbolPair;
44 
46 
47  EasyScanner();
48  EasyScanner(std::string filename);
49  EasyScanner(const EasyScanner& org, bool copyText = false);
50  virtual ~EasyScanner();
51 
52  void putSymbols();
53 
54  inline void registerSymbol(int id, const std::string& symbol) {
55  symbols->insert(SymbolPair(symbol, id));
56  }
57 
58  void setSymbols(std::shared_ptr<SymbolMap> symbols){
59  this->symbols = symbols;
60  }
61 
62  inline int getSymbolID(const std::string& symbol) {
63  SymbolMap::iterator p = symbols->find(symbol);
64  return (p != symbols->end()) ? p->second : 0;
65  }
66 
68  void setCommentChar(char cc);
69 
70  void setLineOriented(bool on);
71  void setQuoteChar(char qc);
72  void setWhiteSpaceChar(char ws);
73 
74  void loadFile(const std::string& filename);
75 
76  void setText(const char* text, size_t len);
77 
78  void setLineNumberOffset(int offset);
79 
80  void setDefaultErrorMessage(const std::string& message){
81  defaultErrorMessage = message;
82  }
83 
84  void moveToHead();
85 
86  int readToken();
87 
88  void toLower();
89 
90  bool readFloat();
91  bool readDouble();
92  bool readInt();
93  bool readChar();
94  bool readChar(int chara);
95  int peekChar();
96 
101  inline bool readWord() {
102  skipSpace();
103  return readWord0();
104  }
105 
110  inline bool readString(const int delimiterChar = ',') {
111  skipSpace();
112  return readString0(delimiterChar);
113  }
114 
115  bool readString(const char* str);
116 
117  inline bool readString(const std::string& str) {
118  return readString(str.c_str());
119  }
120 
121  bool readQuotedString(bool allowNoQuotedWord = false);
122 
123  bool readUnquotedTextBlock();
124 
125  bool readSymbol();
126  bool readSymbol(int id);
127 
128  inline bool isEOF(){
129  skipSpace();
130  return (*text == '\0');
131  }
132 
134  inline bool readLF() {
135  skipSpace();
136  return readLF0();
137  }
138 
139  inline bool readLFEOF() {
140  skipSpace();
141  return readLF0() ? true : (*text == '\0');
142  }
143 
144  bool checkLF();
145 
146  bool readLine();
147  bool skipLine();
148  bool skipBlankLines();
149  void skipToLineEnd();
150 
151  void skipSpace();
152 
153  void throwException(const char* message);
154  void throwException(const std::string& message);
155 
160  inline int readIntEx(const char* message = 0) {
161  if(!readInt()) throwException(message);
162  return intValue;
163  }
168  inline double readDoubleEx(const char* message = 0) {
169  if(!readDouble()) throwException(message);
170  return doubleValue;
171  }
176  inline float readFloatEx(const char* message = 0) {
177  if(!readFloat()) throwException(message);
178  return floatValue;
179  }
184  inline int readCharEx(const char* message = 0) {
185  if(!readChar())
186  throwException(message);
187  return charValue;
188  }
192  inline void readCharEx(int chara, const char* message = 0) {
193  if(!readChar(chara)) throwException(message);
194  }
199  inline const std::string& readWordEx(const char* message = 0) {
200  if(!readWord()) throwException(message);
201  return stringValue;
202  }
203 
204  inline void checkStringEx(const char* str, const char* message = 0){
205  if(!readString(str)) throwException(message);
206  }
207 
212  inline const std::string& readStringEx(const char* message = 0) {
213  if(!readString()) throwException(message);
214  return stringValue;
215  }
216 
217  inline const std::string& readQuotedStringEx(const char* message = 0) {
218  if(!readQuotedString()) throwException(message);
219  return stringValue;
220  }
225  inline int readSymbolEx(const char* message = 0) {
226  if(!readSymbol()) throwException(message);
227  return symbolValue;
228  }
232  inline void readLFex(const char* message = 0) {
233  if(!readLF()) throwException(message);
234  }
235 
236  inline void readLFEOFex(const char* message = 0) {
237  if(!readLFEOF()) throwException(message);
238  }
239 
240  int intValue;
241  double doubleValue;
242  float floatValue;
243  std::string stringValue;
244  char charValue;
246 
247  std::string defaultErrorMessage;
249 
250  char* text;
251 
252  std::string filename;
253 
254 private:
255  void init();
256  bool extractQuotedString();
257 
258  bool readLF0();
259  bool readWord0();
260  bool readString0(const int delimiterChar);
261 
262  char* textBuf;
263  size_t size;
264  char* textBufEnd;
265  int lineNumberOffset;
266  int commentChar;
267  int quoteChar;
268  bool isLineOriented;
269 
270  std::vector<int> whiteSpaceChars;
271 
272  std::shared_ptr<SymbolMap> symbols;
273 
274  friend CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, double& value);
275  friend CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, int& value);
276  friend CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, const char* matchString);
277  friend CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, char matchChar);
278  friend CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, std::string& str);
279  friend CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, EasyScanner::Endl endl);
280 
281 };
282 
283 
284 CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, double& value);
285 CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, int& value);
286 CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, const char* matchString);
287 CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, char matchChar);
288 CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, std::string& str);
289 CNOID_EXPORT EasyScanner& operator>>(EasyScanner& scanner, EasyScanner::Endl endl);
290 }
291 
292 #endif
cnoid::EasyScanner::setDefaultErrorMessage
void setDefaultErrorMessage(const std::string &message)
Definition: EasyScanner.h:80
cnoid::EasyScanner::readDoubleEx
double readDoubleEx(const char *message=0)
Definition: EasyScanner.h:168
cnoid::EasyScanner::endl
Endl endl
Definition: EasyScanner.h:45
cnoid::EasyScanner::Exception::filename
std::string filename
Definition: EasyScanner.h:32
cnoid::EasyScanner::isEOF
bool isEOF()
Definition: EasyScanner.h:128
cnoid::EasyScanner::SymbolPair
std::pair< std::string, int > SymbolPair
Definition: EasyScanner.h:43
cnoid::EasyScanner::lineNumber
int lineNumber
Definition: EasyScanner.h:248
cnoid::EasyScanner::readCharEx
int readCharEx(const char *message=0)
Definition: EasyScanner.h:184
cnoid::EasyScanner::readString
bool readString(const int delimiterChar=',')
Definition: EasyScanner.h:110
cnoid::str
std::string str(const Vector3 &v)
Definition: EigenUtil.cpp:206
cnoid::EasyScanner::readLFex
void readLFex(const char *message=0)
Definition: EasyScanner.h:232
cnoid::EasyScanner::floatValue
float floatValue
Definition: EasyScanner.h:242
cnoid::EasyScanner::SymbolMap
std::unordered_map< std::string, int > SymbolMap
Definition: EasyScanner.h:42
cnoid::EasyScanner::readWord
bool readWord()
Definition: EasyScanner.h:101
cnoid::EasyScanner::defaultErrorMessage
std::string defaultErrorMessage
Definition: EasyScanner.h:247
cnoid::EasyScanner::readLF
bool readLF()
reading a line feed
Definition: EasyScanner.h:134
cnoid::EasyScanner::readLFEOF
bool readLFEOF()
Definition: EasyScanner.h:139
cnoid::EasyScanner::text
char * text
Definition: EasyScanner.h:250
cnoid::EasyScanner::readStringEx
const std::string & readStringEx(const char *message=0)
Definition: EasyScanner.h:212
cnoid::EasyScanner::registerSymbol
void registerSymbol(int id, const std::string &symbol)
Definition: EasyScanner.h:54
cnoid::EasyScanner::readFloatEx
float readFloatEx(const char *message=0)
Definition: EasyScanner.h:176
cnoid::EasyScanner::stringValue
std::string stringValue
Definition: EasyScanner.h:243
cnoid::EasyScanner::charValue
char charValue
Definition: EasyScanner.h:244
cnoid::EasyScanner
Definition: EasyScanner.h:21
cnoid::EasyScanner::T_WORD
@ T_WORD
Definition: EasyScanner.h:38
cnoid::EasyScanner::readWordEx
const std::string & readWordEx(const char *message=0)
Definition: EasyScanner.h:199
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::EasyScanner::TokenType
TokenType
Definition: EasyScanner.h:37
cnoid::EasyScanner::Endl
Definition: EasyScanner.h:25
cnoid::EasyScanner::intValue
int intValue
Definition: EasyScanner.h:240
cnoid::EasyScanner::readCharEx
void readCharEx(int chara, const char *message=0)
Definition: EasyScanner.h:192
cnoid::EasyScanner::T_STRING
@ T_STRING
Definition: EasyScanner.h:39
cnoid::EasyScanner::symbolValue
int symbolValue
Definition: EasyScanner.h:245
cnoid::EasyScanner::Exception::message
std::string message
Definition: EasyScanner.h:31
cnoid::EasyScanner::filename
std::string filename
Definition: EasyScanner.h:252
cnoid::EasyScanner::Exception
Definition: EasyScanner.h:29
cnoid::EasyScanner::setSymbols
void setSymbols(std::shared_ptr< SymbolMap > symbols)
Definition: EasyScanner.h:58
cnoid::EasyScanner::readString
bool readString(const std::string &str)
Definition: EasyScanner.h:117
cnoid::EasyScanner::readIntEx
int readIntEx(const char *message=0)
Definition: EasyScanner.h:160
cnoid::EasyScanner::readSymbolEx
int readSymbolEx(const char *message=0)
Definition: EasyScanner.h:225
cnoid::EasyScanner::readLFEOFex
void readLFEOFex(const char *message=0)
Definition: EasyScanner.h:236
cnoid::EasyScanner::checkStringEx
void checkStringEx(const char *str, const char *message=0)
Definition: EasyScanner.h:204
cnoid::EasyScanner::getSymbolID
int getSymbolID(const std::string &symbol)
Definition: EasyScanner.h:62
cnoid::operator>>
CNOID_EXPORT EasyScanner & operator>>(EasyScanner &scanner, double &value)
Definition: EasyScanner.cpp:681
cnoid::EasyScanner::readQuotedStringEx
const std::string & readQuotedStringEx(const char *message=0)
Definition: EasyScanner.h:217
cnoid::EasyScanner::doubleValue
double doubleValue
Definition: EasyScanner.h:241
cnoid::EasyScanner::Exception::lineNumber
int lineNumber
Definition: EasyScanner.h:33