lib/cfg_parse.c
/**
 * This function loads data from a file, and inserts / updates the specified cfg_struct.
 * New keys will be inserted.  Existing keys can optionally have values overwritten by those read from the file.
 * The format of config-files is "key=value", with any amount of whitespace.
 * Comments can be included by using a # character: processing ends at that point.
 * The maximum line size is CFG_MAX_LINE-1 bytes (see cfg_parse.h)
 * @param cfg Pointer to cfg_struct to update.
 * @param filename String containing filename to open and parse.
 * @param overwrite If 1, existing values will be overwritten.
 * @return EXIT_SUCCESS (0) on success, or EXIT_FAILURE if file could not be opened.
 */
This function loads data from a file, and inserts / updates the specified cfg_struct.
New keys will be inserted.  Existing keys can optionally have values overwritten by those read from the file.
The format of config-files is "key=value", with any amount of whitespace.
Comments can be included by using a # character: processing ends at that point.
The maximum line size is CFG_MAX_LINE-1 bytes
@param cfg Pointer to cfg_struct to update.
@param filename String containing filename to open and parse.
@param overwrite If 1, existing values will be overwritten.
@return EXIT_SUCCESS (0) on success, or EXIT_FAILURE if file could not be opened.
This function loads data from a file and inserts / updates the specified cfg_struct . New keys will be inserted . Existing keys can optionally have values overwritten by those read from the file . The format of config - files is " key = value " with any amount of whitespace . Comments can be included by using a # character : processing ends at that point . The maximum line size is CFG_MAX_LINE - 1 bytes @param cfg Pointer to cfg_struct to update . @param filename String containing filename to open and parse . @param overwrite If 1 existing values will be overwritten . @return EXIT_SUCCESS ( 0 ) on success or EXIT_FAILURE if file could not be opened .
int cfg_load(struct cfg_struct *cfg, const char *filename, int overwrite)
{
  FILE *fp;
  char buffer[CFG_MAX_LINE], *delim;
  if (cfg == NULL || filename == NULL) return EXIT_FAILURE;
  fp = fopen(filename, "r");
  if (fp == NULL) return EXIT_FAILURE;
  while (!feof(fp))
  {
    if (fgets(buffer,CFG_MAX_LINE,fp) != NULL)
    {
      delim = strchr(buffer, '#');
      if (delim != NULL) *delim = '\0';
      delim = strchr(buffer, '=');
      if (delim != NULL)
      {
        *delim = '\0';
        delim ++;
        if(overwrite == 1)
	        cfg_set(cfg,buffer,delim);
        else
        {
	        if(cfg_get(cfg, buffer) == NULL)
		        cfg_set(cfg,buffer,delim);
        }
      }
    }
  }
  fclose(fp);
  return EXIT_SUCCESS;
}
int cfg_load ( struct cfg_struct * cfg , const char * filename , int overwrite ) { FILE * fp ; char buffer [ CFG_MAX_LINE ] , * delim ; if ( cfg == NULL || filename == NULL ) return EXIT_FAILURE ; fp = fopen ( filename , " " ) ; if ( fp == NULL ) return EXIT_FAILURE ; while ( ! feof ( fp ) ) { if ( fgets ( buffer , CFG_MAX_LINE , fp ) != NULL ) { delim = strchr ( buffer , ' ' ) ; if ( delim != NULL ) * delim = ' \0 ' ; delim = strchr ( buffer , ' ' ) ; if ( delim != NULL ) { * delim = ' \0 ' ; delim ++ ; if ( overwrite == 1 ) cfg_set ( cfg , buffer , delim ) ; else { if ( cfg_get ( cfg , buffer ) == NULL ) cfg_set ( cfg , buffer , delim ) ; } } } } fclose ( fp ) ; return EXIT_SUCCESS ; }
This function loads data from a file, and inserts / updates the specified cfg_struct.
This function loads data from a file and inserts / updates the specified cfg_struct .
/* safety check: null input */ /* open file for reading */ /* locate first # sign and terminate string there (comment) */ /* locate first = sign and prepare to split */