Exemple #1
0
void
mack::options::configuration_file::parse(
    std::string const& content,
    mack::options::program_options* options)
{
  if (options == NULL)
  {
    BOOST_THROW_EXCEPTION(mack::core::null_pointer_error());
  }
  size_t line_number = 1;
  size_t line_start = 0;
  size_t line_end = content.find('\n', line_start);
  while (line_end != std::string::npos)
  {
    do_parse_line(
        content.substr(line_start, line_end - line_start), options,
        line_number);
    // prepare for next line
    ++line_number;
    line_start = line_end + 1;
    line_end = content.find('\n', line_start);
  }
  do_parse_line(
      content.substr(line_start), options,
      line_number);
}
Exemple #2
0
bool config_parse_file(
    const char *file_name,
    const struct config_entry *config_table, size_t config_size)
{
    FILE *input = fopen(file_name, "r");
    if (!TEST_NULL_(input, "Unable to open config file \"%s\"", file_name))
        return false;

    /* Array of seen flags for each configuration entry, used to ensure that
     * every needed configuration setting is set. */
    bool seen[config_size];
    memset(seen, 0, sizeof(seen));

    /* Process each line in the file. */
    bool ok = true;
    bool eof = false;
    int line_number = 0;
    while (ok  &&  !eof)
    {
        char line_buffer[LINE_SIZE];
        ok =
            read_line(
                input, line_buffer, sizeof(line_buffer), &line_number, &eof)  &&
            do_parse_line(
                file_name, line_number, line_buffer,
                config_table, config_size, seen);
    }
    fclose(input);

    /* Check that all required entries were present. */
    errno = 0;      // Can linger over into error reporting
    for (size_t i = 0; ok  &&  i < config_size; i ++)
        ok = TEST_OK_(seen[i]  ||  config_table[i].optional,
            "No value specified for parameter: %s", config_table[i].name);

    return ok;
}