SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
IParser.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "Log.h"
7 #include <vector>
8 #include <string>
9 #include <algorithm>
10 #include "String.h"
11 
12 namespace ai {
13 
14 class IParser {
15 private:
16  std::string _error;
17 
18 protected:
19  void setError(const char* msg, ...) __attribute__((format(printf, 2, 3)));
20 
21  inline void resetError() {
22  _error = "";
23  }
24 
25  inline std::string getBetween (const std::string& str, const std::string& tokenStart, const std::string& tokenEnd) {
26  const std::size_t start = str.find(tokenStart);
27  if (start == std::string::npos) {
28  return "";
29  }
30 
31  const std::size_t end = str.find(tokenEnd);
32  if (end == std::string::npos) {
33  setError("syntax error - expected %s", tokenEnd.c_str());
34  return "";
35  }
36  const size_t startIndex = start + 1;
37  const size_t endIndex = end - startIndex;
38  if (endIndex <= 0) {
39  return "";
40  }
41  const std::string& between = str.substr(startIndex, endIndex);
42  return between;
43  }
44 
45 public:
46  const std::string& getError() const;
47 };
48 
49 inline void IParser::setError(const char* msg, ...) {
50  va_list args;
51  va_start(args, msg);
52  char buf[1024];
53  std::vsnprintf(buf, sizeof(buf), msg, args);
54  va_end(args);
55  if (buf[0] != '\0') {
56  ai_log_debug("%s", buf);
57  }
58  _error = buf;
59 }
60 
61 
62 inline const std::string& IParser::getError() const {
63  return _error;
64 }
65 
66 }
Definition: IParser.h:14
#define ai_log_debug(...)
Logging macro to provide your own loggers.
Definition: Types.h:37