Logo Search packages:      
Sourcecode: odyssey version File versions

bool ConfigFile::read_configfile ( char *  filename,
StringMap c 
) [protected]

An internal helper function that will read the configuration values of a configuration file into a StringMap.

Parameters:
filename The name of the configuration file to read.
c A StringMap in which to store the configuration variables.
Returns:
A boolean value indicating if the read succeeded.

Definition at line 86 of file ConfigFile.cxx.

References THROW_ERROR.

Referenced by reread(), and writeback().

                                                             {
      char readbuf[100];
      FILE *fp;
      int i, line;
      string section;

      line = 0;
      fp = fopen(filename, "r");
      if(fp == NULL) return false;
      while(fgets(readbuf, sizeof(readbuf), fp) != NULL) {
            line++;
            string buf(readbuf);
            /* Clear out leading and trailing whitespace, including the newline */
            buf.erase(0, buf.find_first_not_of(string(" \t")));
            i = buf.find_last_not_of(string(" \t\n\r"));
            buf.erase(i+1, buf.length()-i-1);

            /* Skip comment and blank lines */
            if((buf.size() == 0) || (buf.find('#') == 0)) continue;

            /* Check for new section header */
            if(buf.find('[') == 0) {
                  i = buf.find_first_of(']');
                  if(i < 0) goto parse_error;
                  section = buf.erase(i);
                  section.erase(0, 1);
                  continue;
            }

            i = buf.find('=');
            if(i < 0) goto parse_error;

            /* Extract the value and remove leading whitespace */
            string value(buf, i+1, buf.length()-i-1);
            value.erase(0, value.find_first_not_of(string(" \t")));

            /* Extract the key and remove trailing whitespace */
            string key(buf, 0, i);
            i = key.find_last_not_of(string(" \t"));
            key.erase(i+1, key.length()-i-1);

            i = key.find('.');
            if(i < 0) {
                  /* Windows ini style entry so make sure there's a section */
                  if(section.size() == 0)
                        THROW_ERROR(runtime_error, "Variable defined outside " \
                          "of a section in %s, line %d", filename, line);
                  /* Create the actual map key */
                  key = section + "." + key;
            }

            c.erase(key);
            c.insert(StringMap::value_type(key, value));
      }
      fclose(fp);
      return true;

parse_error:;
      THROW_ERROR(runtime_error, "Error parsing line %d in %s\n",
        line, filename);
}


Generated by  Doxygen 1.6.0   Back to index