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