home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1993 Marc Stern (internet: stern@mble.philips.be) */
-
- #include <stdarg.h>
- #include <stdio.h>
- #include <string.h>
- #include "strings.h"
- #include "tools.h"
-
-
-
-
- /***
- *
- * Function : getsetupparam
- *
- * Topics : Read paramaters in setup file.
- *
- * Decisions : Expected format is a list of lines of the form
- *
- * <keyword>=<value>
- *
- * - All letters are treated (and returned) as uppercases.
- * - Blanks and tabs are skipped.
- *
- * - <value> can be enclosed between double quotes
- * to preserve lowercases and blanks.
- *
- * - 2 consecutive double quotes allow to keep
- * a double quote in <value>.
- *
- * - A number sign (#) in first column forces the line
- * to be ignored.
- *
- *
- * Parameters : in FILE *file input file
- * in char *keyword keyword to match
- * out char *value value associated to keyword
- *
- * Return code: 0 OK
- * -1 keyword not found
- * -2 file problem
- *
- * Precond : Input file must be opened with read access.
- *
- */
-
- static int near getsetupparam( FILE *file, char *keyword, char *value )
-
- { char buffer[256], *ptr;
- int length;
-
- if ( fseek(file, 0, SEEK_SET) ) return -2;
-
- while ( fgets(buffer, 255, file) )
- {
- if ( *buffer == '#' ) continue;
-
- strreduce( buffer );
- strreduce( keyword );
-
- length = strlen( keyword );
- if ( strncmp(buffer, keyword, length) ) continue;
- ptr = buffer + length;
- if ( *ptr++ != '=' ) continue;
-
- strcpy( value, ptr );
- return 0;
- }
-
- return -1;
-
- }
-
-
-
- /***
- *
- * Function : getsetup
- *
- * Topics : Read paramaters in setup file.
- *
- * Decisions : Expected format is a list of lines of the form
- *
- * <keyword>=<value>
- *
- * - All letters are treated (and returned) as uppercases.
- * - Blanks and tabs are skipped.
- *
- * - <value> can be enclosed between double quotes
- * to preserve lowercases and blanks.
- *
- * - 2 consecutive double quotes allow to keep
- * a double quote in <value>.
- *
- * - A number sign (#) in first column forces the line
- * to be ignored.
- *
- * - The input formats in parameter 'format' are C standard
- * ones ( %d, %s, %f,...)
- *
- * Parameters : in FILE *file input file
- * in char *format "keyword1=format keyword2=format"
- * out pointer list
- *
- * Return code: 0 OK
- * -1 keyword not found
- * -2 file problem
- *
- * Precond : Input file must be opened with read access.
- *
- */
-
- int getsetup( FILE *file, const char *format, ... )
-
- { va_list list;
- int count;
- char buffer[255], fmt[255], *keyword;
- void *ptr;
-
- va_start(list, format);
- strcpy( fmt, format );
- for ( keyword = strtok(fmt, "="); keyword; keyword = strtok(NULL, "=") )
- {
- ptr = va_arg( list, void* );
- count = getsetupparam( file, keyword, buffer );
- if ( count ) return count;
- format = strtok( NULL, " " );
- count = sscanf( buffer, format, ptr );
- if ( count != 1 ) return -1;
- }
-
- va_end( list );
-
- return 0;
- }
-
-