7 #ifndef XGBOOST_COMMON_CONFIG_H_ 8 #define XGBOOST_COMMON_CONFIG_H_ 27 inline const char *
Name()
const {
28 return s_name_.c_str();
34 inline const char *
Val()
const {
35 return s_val_.c_str();
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;
63 virtual bool IsEnd() = 0;
67 std::string s_name_, s_val_, s_buf_;
69 inline void SkipLine() {
72 }
while (ch_buf_ != EOF && ch_buf_ !=
'\n' && ch_buf_ !=
'\r');
75 inline void ParseStr(std::string *tok) {
76 while ((ch_buf_ = this->
GetChar()) != EOF) {
78 case '\\': *tok += this->
GetChar();
break;
81 case '\n': LOG(FATAL)<<
"ConfigReader: unterminated string";
82 default: *tok +=
static_cast<char>(ch_buf_);
85 LOG(FATAL) <<
"ConfigReader: unterminated string";
87 inline void ParseStrML(std::string *tok) {
88 while ((ch_buf_ = this->
GetChar()) != EOF) {
90 case '\\': *tok += this->
GetChar();
break;
92 default: *tok +=
static_cast<char>(ch_buf_);
95 LOG(FATAL) <<
"unterminated string";
98 inline bool GetNextToken(std::string *tok) {
100 bool new_line =
false;
101 while (ch_buf_ != EOF) {
103 case '#' : SkipLine(); new_line =
true;
break;
105 if (tok->length() == 0) {
106 ParseStr(tok); ch_buf_ = this->
GetChar();
return new_line;
108 LOG(FATAL) <<
"ConfigReader: token followed directly by string";
111 if (tok->length() == 0) {
112 ParseStrML(tok); ch_buf_ = this->
GetChar();
return new_line;
114 LOG(FATAL) <<
"ConfigReader: token followed directly by string";
117 if (tok->length() == 0) {
124 if (tok->length() == 0) new_line =
true;
128 if (tok->length() != 0)
return new_line;
131 *tok +=
static_cast<char>(ch_buf_);
136 if (tok->length() == 0) {
179 LOG(FATAL) <<
"cannot open file " << fname;
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