home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CFG.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  129 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* =======================================================================
  4.     CFG.c       Generic configuration file handler.
  5.  
  6.                 A. Reitsma, Delft, The Netherlands.
  7.                 v1.00  94-07-09  Public Domain.
  8.  ---------------------------------------------------------------------- */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include "cfg.h"
  14.  
  15. #define LINE_LEN_MAX    128                  /* actual max line length  */
  16. #define BUFFERSIZE      LINE_LEN_MAX +2      /* ... including \n and \0 */
  17.  
  18. enum RetVal
  19. {
  20.     NO_PROBLEMS,
  21.     ERR_FOPEN,
  22.     ERR_MEM,
  23. };
  24.  
  25. int CfgRead( char * Filename, struct CfgStrings * CfgInfo )
  26. {
  27.     char Buffer[ BUFFERSIZE ];
  28.     char * WorkPtr ;
  29.     char * CfgName ;
  30.     char * CfgData ;
  31.     struct CfgStrings * Cfg ;
  32.     FILE * CfgFile ;
  33.  
  34.     CfgFile = fopen( Filename, "r" );
  35.     if( NULL == CfgFile )
  36.     {
  37.         return ERR_FOPEN ;
  38.     }
  39.  
  40.     while( NULL != fgets( Buffer, BUFFERSIZE, CfgFile ))
  41.     {
  42.         /* clip off optional comment tail indicated by a semi-colon
  43.         */
  44.         if( NULL != (WorkPtr = strchr( Buffer, ';' )))
  45.             *WorkPtr = '\0';
  46.         else
  47.             WorkPtr = Buffer + strlen( Buffer );
  48.  
  49.         /* clip off trailing and leading white space
  50.         */
  51.         WorkPtr--;
  52.         while( isspace( *WorkPtr ) && WorkPtr >= Buffer )
  53.             *WorkPtr-- = '\0';
  54.         WorkPtr = Buffer;
  55.         while( isspace( *WorkPtr ))
  56.             WorkPtr++;
  57.         if( 0 == strlen( WorkPtr ))
  58.             continue;
  59.  
  60.         CfgName = strtok( WorkPtr, " =" );
  61.         if( NULL != CfgName )
  62.         {
  63.             /* Condition the name (lower case required),
  64.                and strip leading white and a 'late' = from data part.
  65.             */
  66.             strlwr( CfgName );
  67.             CfgData = strtok( NULL, "" );
  68.             while( isspace( *CfgData ))
  69.                 CfgData++;
  70.             if( '=' == *CfgData )
  71.                 CfgData++;
  72.             while( isspace( *CfgData ))
  73.                 CfgData++;
  74.  
  75.             /* look for matching 'name'
  76.             */
  77.             Cfg = CfgInfo ;
  78.             while( NULL != Cfg->name && 0 != strcmp( Cfg->name, CfgName ))
  79.                 Cfg++;
  80.  
  81.             /* duplicate the data if the name is found.
  82.             */
  83.             if( NULL != Cfg->name )
  84.             {
  85.                 Cfg->data = strdup( CfgData ); /* strdup is not ANSI    */
  86.                                            /* memory leaks if Cfg->data */
  87.                                            /* is malloc'ed already      */
  88.                 if( NULL == Cfg->data )
  89.                 {
  90.                     fclose( CfgFile );
  91.                     return ERR_MEM;
  92.                 }
  93.             }   /* undetected error on failure should not be a problem  */
  94.                 /* as configuration reading should be done early.       */
  95.         }       /* but test and handle it anyway ...                    */
  96.     }
  97.     fclose( CfgFile );
  98.     return NO_PROBLEMS ;
  99. }
  100.  
  101. #ifdef TEST
  102.  
  103. struct CfgStrings CfgDemoData[] =
  104. {
  105.     { "workpath", NULL },
  106.     { "archiver", NULL },
  107.     { "splitter", NULL },
  108.     { NULL, NULL }         /* array terminator. REQUIRED !!! */
  109. };
  110.  
  111. main( int argc, char * argv[] )
  112. {
  113.     int ix ;
  114.  
  115.     if( argc > 1 )
  116.         CfgRead( argv[ 1 ], CfgDemoData );
  117.     else
  118.         return ERR_FOPEN ;
  119.  
  120.     printf( "%s\n", argv[ 0 ]);
  121.     for( ix = 0; ix < 4 ; ix++ ) /* intentionally one too much */
  122.        printf( "CfgItem \"%s\" is \"%s\"\n", CfgDemoData[ ix ].name,
  123.                                              CfgDemoData[ ix ].data );
  124.     return NO_PROBLEMS;
  125. }
  126. #endif
  127.  
  128. /* ==== CFG.c end ===================================================== */
  129.