xgboost
config.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_COMMON_CONFIG_H_
8 #define XGBOOST_COMMON_CONFIG_H_
9 
10 #include <cstdio>
11 #include <cstring>
12 #include <string>
13 #include <istream>
14 #include <fstream>
15 
16 namespace xgboost {
17 namespace common {
22  public:
27  inline const char *Name() const {
28  return s_name_.c_str();
29  }
34  inline const char *Val() const {
35  return s_val_.c_str();
36  }
41  inline bool Next() {
42  while (!this->IsEnd()) {
43  GetNextToken(&s_name_);
44  if (s_name_ == "=") return false;
45  if (GetNextToken(&s_buf_) || s_buf_ != "=") return false;
46  if (GetNextToken(&s_val_) || s_val_ == "=") return false;
47  return true;
48  }
49  return false;
50  }
51  // called before usage
52  inline void Init() {
53  ch_buf_ = this->GetChar();
54  }
55 
56  protected:
61  virtual int GetChar() = 0;
63  virtual bool IsEnd() = 0;
64 
65  private:
66  int ch_buf_;
67  std::string s_name_, s_val_, s_buf_;
68 
69  inline void SkipLine() {
70  do {
71  ch_buf_ = this->GetChar();
72  } while (ch_buf_ != EOF && ch_buf_ != '\n' && ch_buf_ != '\r');
73  }
74 
75  inline void ParseStr(std::string *tok) {
76  while ((ch_buf_ = this->GetChar()) != EOF) {
77  switch (ch_buf_) {
78  case '\\': *tok += this->GetChar(); break;
79  case '\"': return;
80  case '\r':
81  case '\n': LOG(FATAL)<< "ConfigReader: unterminated string";
82  default: *tok += static_cast<char>(ch_buf_);
83  }
84  }
85  LOG(FATAL) << "ConfigReader: unterminated string";
86  }
87  inline void ParseStrML(std::string *tok) {
88  while ((ch_buf_ = this->GetChar()) != EOF) {
89  switch (ch_buf_) {
90  case '\\': *tok += this->GetChar(); break;
91  case '\'': return;
92  default: *tok += static_cast<char>(ch_buf_);
93  }
94  }
95  LOG(FATAL) << "unterminated string";
96  }
97  // return newline
98  inline bool GetNextToken(std::string *tok) {
99  tok->clear();
100  bool new_line = false;
101  while (ch_buf_ != EOF) {
102  switch (ch_buf_) {
103  case '#' : SkipLine(); new_line = true; break;
104  case '\"':
105  if (tok->length() == 0) {
106  ParseStr(tok); ch_buf_ = this->GetChar(); return new_line;
107  } else {
108  LOG(FATAL) << "ConfigReader: token followed directly by string";
109  }
110  case '\'':
111  if (tok->length() == 0) {
112  ParseStrML(tok); ch_buf_ = this->GetChar(); return new_line;
113  } else {
114  LOG(FATAL) << "ConfigReader: token followed directly by string";
115  }
116  case '=':
117  if (tok->length() == 0) {
118  ch_buf_ = this->GetChar();
119  *tok = '=';
120  }
121  return new_line;
122  case '\r':
123  case '\n':
124  if (tok->length() == 0) new_line = true;
125  case '\t':
126  case ' ' :
127  ch_buf_ = this->GetChar();
128  if (tok->length() != 0) return new_line;
129  break;
130  default:
131  *tok += static_cast<char>(ch_buf_);
132  ch_buf_ = this->GetChar();
133  break;
134  }
135  }
136  if (tok->length() == 0) {
137  return true;
138  } else {
139  return false;
140  }
141  }
142 };
147  public:
152  explicit ConfigStreamReader(std::istream &fin) : fin_(fin) {}
153 
154  protected:
155  int GetChar() override {
156  return fin_.get();
157  }
159  bool IsEnd() override {
160  return fin_.eof();
161  }
162 
163  private:
164  std::istream &fin_;
165 };
166 
171  public:
176  explicit ConfigIterator(const char *fname) : ConfigStreamReader(fi_) {
177  fi_.open(fname);
178  if (fi_.fail()) {
179  LOG(FATAL) << "cannot open file " << fname;
180  }
182  }
185  fi_.close();
186  }
187 
188  private:
189  std::ifstream fi_;
190 };
191 } // namespace common
192 } // namespace xgboost
193 #endif // XGBOOST_COMMON_CONFIG_H_
int GetChar() override
to be implemented by subclass, get next token, return EOF if end of file
Definition: config.h:155
base implementation of config reader
Definition: config.h:21
~ConfigIterator()
destructor
Definition: config.h:184
an iterator use stream base, allows use all types of istream
Definition: config.h:146
virtual bool IsEnd()=0
to be implemented by child, check if end of stream
const char * Name() const
get current name, called after Next returns true
Definition: config.h:27
an iterator that iterates over a configure file and gets the configures
Definition: config.h:170
ConfigIterator(const char *fname)
constructor
Definition: config.h:176
namespace of xgboost
Definition: base.h:79
bool IsEnd() override
to be implemented by child, check if end of stream
Definition: config.h:159
virtual int GetChar()=0
to be implemented by subclass, get next token, return EOF if end of file
bool Next()
move iterator to next position
Definition: config.h:41
void Init()
Definition: config.h:52
const char * Val() const
get current value, called after Next returns true
Definition: config.h:34
ConfigStreamReader(std::istream &fin)
constructor
Definition: config.h:152