Example #1
0
bool Config::ReadConfig(std::istream &in, const char *path) {
  string line, errstr="";

  std::tuple<string, function<bool(string&)>>
  rules[] = {
    std::make_tuple("database",         cfg_path(database_)),
    std::make_tuple("verbosity",        cfg_numeric(verbosity_)),
    std::make_tuple("quiet",            cfg_bool(quiet_)),
    std::make_tuple("package_depends",  cfg_bool(package_depends_)),
    std::make_tuple("json",             cfg_json(json_,errstr)),
    std::make_tuple("jobs",             cfg_numeric(max_jobs_)),
    std::make_tuple("file_lists",       cfg_bool(package_filelist_)),
  };

  size_t lineno = 0;
  while (std::getline(in, line)) {
    ++lineno;
    size_t start = line.find_first_not_of(" \t\n\r");
    // no content
    if (start == string::npos)
      continue;
    // comment lines
    if (line[start] == '#' || line[start] == '/' || line[start] == ';')
      continue;

    errstr = "";
    for (auto &r : rules) {
      string &name(std::get<0>(r));
      auto    fn = std::get<1>(r);
      if (line.compare(start, name.length(), name) == 0) {
        size_t eq = line.find_first_not_of(" \t\n\r", start+name.length());
        if (eq == string::npos) {
          Log(Warn, "%s:%lu: invalid config entry\n",
              path, (unsigned long)lineno);
          break;
        }
        if (line[eq] != '=') {
          Log(Warn, "%s:%lu: missing `=` in config entry\n",
              path, (unsigned long)lineno);
          break;
        }
        start = line.find_first_not_of(" \t\n\r", eq+1);
        line = move(line.substr(start));
        if (!fn(line)) {
          if (!errstr.empty())
            Log(Error, "%s:%lu: %s\n", path, (unsigned long)lineno,
                errstr.c_str());
          return false;
        }
        break;
      }
    }
  }
  return true;
}
Example #2
0
static bool ReadConfig(std::istream &in, const char *path) {
  std::string line;

  size_t lineno = 0;
  while (std::getline(in, line)) {
    ++lineno;
    size_t start = line.find_first_not_of(" \t\n\r");
    // no content
    if (start == std::string::npos)
      continue;
    // comment lines
    if (line[start] == '#' ||
        line[start] == '/' ||
        line[start] == ';')
    {
      continue;
    }

    std::tuple<std::string, std::function<bool(std::string&)>>
    rules[] = {
      std::make_tuple("database",         cfg_database),
      std::make_tuple("verbosity",        cfg_numeric(opt_verbosity)),
      std::make_tuple("quiet",            cfg_bool(opt_quiet)),
      std::make_tuple("package_depends",  cfg_bool(opt_package_depends)),
      std::make_tuple("josn",             cfg_json),
      std::make_tuple("json",             cfg_json),
      std::make_tuple("jobs",             cfg_numeric(opt_max_jobs)),
      std::make_tuple("file_lists",       cfg_bool(opt_package_filelist)),
    };

    for (auto &r : rules) {
      std::string &name(std::get<0>(r));
      auto         fn = std::get<1>(r);
      if (line.compare(start, name.length(), name) == 0) {
        size_t eq = line.find_first_not_of(" \t\n\r", start+name.length());
        if (eq == std::string::npos) {
          log(Warn, "%s:%lu: invalid config entry\n",
              path, (unsigned long)lineno);
          break;
        }
        if (line[eq] != '=') {
          log(Warn, "%s:%lu: missing `=` in config entry\n",
              path, (unsigned long)lineno);
          break;
        }
        start = line.find_first_not_of(" \t\n\r", eq+1);
        line = std::move(line.substr(start));
        if (!fn(line))
          return false;
        break;
      }
    }
  }
  return true;
}