Alien-XGBoost

 view release on metacpan or  search on metacpan

xgboost/dmlc-core/src/config.cc  view on Meta::CPAN

/*!
 *  Copyright (c) 2015 by Contributors
 */
#include <sstream>
#include <exception>

#include "dmlc/config.h"
#include "dmlc/logging.h"

using namespace std;

namespace dmlc {

struct Token {
  std::string buf;
  bool is_string;
};

class TokenizeError : public exception {
 public:
  explicit TokenizeError(const string& msg = "tokenize error"): msg_(msg) { }
  ~TokenizeError() throw() {}
  virtual const char* what() const throw() {
    return msg_.c_str();
  }
 private:
  string msg_;
};

class Tokenizer {
 public:
  explicit Tokenizer(istream& is): is_(is), state_(kNone) {}  // NOLINT(*)
  bool GetNextToken(Token* tok) {
    // token is defined as
    // 1. [^\s=]+
    // 2. "[(^"|\\")]*"
    // 3. =
    state_ = kNone;
    tok->buf.clear();
    tok->is_string = false;
    char ch;
    while ( (ch = PeekChar()) != EOF && state_ != kFinish ) {
      switch (ch) {
      case ' ': case '\t': case '\n': case '\r':
        if (state_ == kToken) {
          state_ = kFinish;
        } else {
          EatChar();  // ignore
        }
        break;
      case '\"':
        ParseString(&tok->buf);
        state_ = kFinish;
        tok->is_string = true;
        break;
      case '=':
        if (state_ != kToken) {
          tok->buf = '=';
          EatChar();
        }
        state_ = kFinish;
        break;
      case '#':
        ParseComments();
        break;
      default:
        state_ = kToken;
        tok->buf += ch;
        EatChar();
        break;
      }
    }
    return PeekChar() != EOF;
  }

  void ParseString(string* tok) {
    EatChar();  // eat the first quotation mark
    char ch;
    while ( (ch = PeekChar()) != '\"' ) {
      switch (ch) {
        case '\\':
          EatChar();
          ch = PeekChar();
          if (ch == '\"') {
            *tok += '\"';
          } else {
            throw TokenizeError("error parsing escape characters");
          }
          break;
        case '\n': case '\r': case EOF:
          throw TokenizeError("quotation mark is not closed");
        default:
          *tok += ch;
          break;
      }
      EatChar();
    }
    EatChar();  // eat the last quotation mark
  }

  void ParseComments() {
    char ch;
    while ( (ch = PeekChar()) ) {
      if (ch == '\n' || ch == '\r' || ch == EOF) {
        break;  // end of comment
      }
      EatChar();  // ignore all others
    }
  }

 private:
  char PeekChar() {
    return is_.peek();
  }
  void EatChar() {
    is_.get();
  }

  enum ParseState {
    kNone = 0,
    kToken,
    kFinish,
  };
  istream& is_;
  ParseState state_;
};

//////////////////////// Config /////////////////////////////
Config::Config(bool m): multi_value_(m) {
  Clear();
}

Config::Config(istream& is, bool m): multi_value_(m) {
  Clear();
  LoadFromStream(is);
}

void Config::Clear() {
  config_map_.clear();
  order_.clear();
}

void Config::LoadFromStream(istream& is) {
  Tokenizer tokenizer(is);
  Token key, eqop, value;
  try {
    while ( true ) {
      tokenizer.GetNextToken(&key);
      if (key.buf.length() == 0) {
        break;  // no content left
      }



( run in 0.484 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )