home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TOOLS.ZIP / SETUP.C < prev    next >
C/C++ Source or Header  |  1993-02-26  |  5KB  |  177 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4. #include "tools.h"
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10.  
  11.  
  12.  
  13. /***
  14.  *
  15.  *  Function   :   getsetupparam
  16.  *
  17.  *  Topics     :   Read paramaters in setup file.
  18.  *
  19.  *  Decisions  :   Expected format is a list of lines of the form
  20.  *
  21.  *                          <keyword>=<value>
  22.  *
  23.  *                 - All letters are treated (and returned) as uppercases.
  24.  *                 - Blanks and tabs are skipped.
  25.  *
  26.  *                 - <value> can be enclosed between double quotes
  27.  *             to preserve lowercases and blanks.
  28.  *
  29.  *            - 2 consecutive double quotes allow to keep
  30.  *                   a double quote in <value>.
  31.  *
  32.  *                 - A number sign (#) in first column forces the line
  33.  *                   to be ignored.
  34.  *
  35.  *
  36.  *  Parameters :    in    FILE *file        input file
  37.  *                  in    char *keyword     keyword to match
  38.  *                  out   char *value       value associated to keyword
  39.  *
  40.  *  Return code:     0     OK
  41.  *                  -1     keyword not found
  42.  *                  -2     file problem
  43.  *
  44.  *  Precond    :   Input file must be opened with read access.
  45.  *
  46.  */
  47.  
  48. static int near getsetupparam( FILE *file, char *keyword, char *value )
  49.  
  50. { char buffer[256], *ptr;
  51.   int length;
  52.  
  53.   if ( fseek(file, 0, SEEK_SET) ) return -2;
  54.  
  55.   while ( fgets(buffer, 255, file) )
  56.     {
  57.       if ( *buffer == '#' ) continue;
  58.  
  59.       strreduce( buffer );
  60.       strreduce( keyword );
  61.  
  62.           length = strlen( keyword );
  63.       if ( strncmp(buffer, keyword, length) ) continue;
  64.       ptr = buffer + length;
  65.           if ( *ptr++ != '=' ) continue;
  66.  
  67.       strcpy( value, ptr );
  68.       return 0;
  69.     }
  70.  
  71.   return -1;
  72.  
  73. }
  74.  
  75.  
  76.  
  77. /***
  78.  *
  79.  *  Function   :   getsetup
  80.  *
  81.  *  Topics     :   Read paramaters in setup file.
  82.  *
  83.  *  Decisions  :   Expected format is a list of lines of the form
  84.  *
  85.  *                          <keyword>=<value>
  86.  *
  87.  *                 - All letters are treated (and returned) as uppercases.
  88.  *                 - Blanks and tabs are skipped.
  89.  *
  90.  *                 - <value> can be enclosed between double quotes
  91.  *             to preserve lowercases and blanks.
  92.  *
  93.  *            - 2 consecutive double quotes allow to keep
  94.  *                   a double quote in <value>.
  95.  *
  96.  *                 - A number sign (#) in first column forces the line
  97.  *                   to be ignored.
  98.  *
  99.  *                 - The input formats in parameter 'format' are C standard
  100.  *                   ones ( %d, %s, %f,...).
  101.  *
  102.  *                 - Special format added: "%b" will translate
  103.  *                   True/False, Yes/No, On/Off, 1/0 into integer 1/0
  104.  *
  105.  *                 - Special format added: "%S" will result into copying
  106.  *                   all the input line (with spaces in it if any).
  107.  *                   It will not be processed by scanf-like processing.
  108.  *
  109.  *  Parameters :    in    FILE *file        input file
  110.  *                  in    char *format      "keyword1=format keyword2=format"
  111.  *                  out   pointer list
  112.  *
  113.  *  Return code:    number of found parameters
  114.  *                  -1     syntax error
  115.  *                  -2     file error
  116.  *
  117.  *  Precond    :   Input file must be opened with read access.
  118.  *
  119.  */
  120.  
  121. int getsetup( FILE *file, const char *format, ... )
  122.  
  123. { va_list list;
  124.   int status, count = 0;
  125.   char par_value[255], fmt[255], *keyword, *ptr;
  126.   void *param;
  127.  
  128.   va_start(list, format);
  129.   strcpy( fmt, format );
  130.   for ( keyword = strtok(fmt, "="); keyword; keyword = strtok(NULL, "=") )
  131.       {
  132.         param = va_arg( list, void* );
  133.         status = getsetupparam( file, keyword, par_value );
  134.         if ( status == -2 ) return -2;
  135.         if ( ! status ) count++;
  136.         format = strtok( NULL, " \t\r\n" );
  137.         /* handle boolean (%b) format */
  138.         ptr = strstr( format, "%b" );
  139.         if ( ptr++ )
  140.            {
  141.              char buffer[255] = "";
  142. /*  When available use _stricmp instead of strcmp */
  143. /*  in case of string has been between quotes     */
  144. /*  and has not been translated into uppercases.  */
  145. #if ! defined(_MSC_VER) && ! defined(__TURBOC__)
  146. # define _stricmp strcmp
  147. #endif
  148.              *ptr = 's';
  149.  
  150.              if ( sscanf(par_value, format, buffer) != 1 ) return -1;
  151.              if ( _stricmp(buffer, "TRUE") ||
  152.                   _stricmp(buffer, "YES") ||
  153.                   _stricmp(buffer, "ON") ||
  154.                   _stricmp(buffer, "1")
  155.                 ) *(int*)param = 1;
  156.              else *(int*)param = 0;
  157.  
  158.              continue;
  159.            }
  160.  
  161.         /* Handle %S to get spaces inside strings */
  162.         ptr = strstr( format, "%S" );
  163.         if ( ptr )
  164.            {
  165.              strcpy( ((char*)param), par_value );
  166.              continue;
  167.            }
  168.  
  169.         if ( sscanf(par_value, format, param) != 1 ) return -1;
  170.       }
  171.  
  172.   va_end( list );
  173.  
  174.   return count;
  175. }
  176.  
  177.