home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 479a.lha / barn_v2.01 / source / configure.c < prev    next >
Text File  |  1991-02-10  |  5KB  |  182 lines

  1. /*
  2.  *    File Name:        configure.c
  3.  *    Project:        BARN - Bah's Amiga ReadNews.
  4.  *    Purpose:        Configuration variable setting, retrieving.
  5.  *    Functions:        Configure, SetVar, GetVar.
  6.  *    Author:            Jeff Van Epps
  7.  *    Created:        20 Oct 90
  8.  *    Last Modified:    20 Oct 90
  9.  *    Comments:
  10.  *        Generic configuration file processor.  Reads lines of the form:
  11.  *            var=value
  12.  *        from a specified configuration file and stores the names and values.
  13.  *        White space WILL be considered significant, so don't put any in there
  14.  *        unless you really want it.  No '=' may appear anywhere else on the line.
  15.  *
  16.  *        SetVar(name,value) changes an existing value or creates a new one.
  17.  *        GetVar(name) returns a pointer to the value of a variable.
  18.  *        Configure(filename) processes a configuration file.
  19.  *
  20.  *        A line in the configuration file beginning with '#' is a comment.
  21.  *        Comment lines and blank lines are skipped.
  22.  *
  23.  *        Currently implemented with static limits for name and value length as
  24.  *        well as number of possible variables.
  25.  *
  26.  *    History:
  27.  *        20 Oct 90/JVE    Created.
  28.  */
  29.  
  30. # include    <stdio.h>
  31. # include    <string.h>
  32. # include    "standard.h"
  33. # include    "configure.h"
  34.  
  35. # define    MAX_VARS            20
  36. # define    MAX_NAME_LEN        10
  37. # define    MAX_VALUE_LEN        40
  38.  
  39. static struct {
  40.     char        name[MAX_NAME_LEN+1];        /* name of variable */
  41.     char        value[MAX_VALUE_LEN+1];        /* its value */
  42.     } vars[MAX_VARS];
  43.  
  44. static int    n_vars = 0;
  45.  
  46.  
  47. /****************************************************************************/
  48. /*    FUNCTION:    Configure                                                    */
  49. /*                                                                            */
  50. /*    PURPOSE:    Parse ARN configuration file.                                */
  51. /*                                                                            */
  52. /*    INPUT PARAMETERS:                                                        */
  53. /*        NAME        I/O        DESCRIPTION                                        */
  54. /*        ----        ---        -----------                                        */
  55. /*        config_file     I        Name of configuration file to use.                */
  56. /*                                                                            */
  57. /*    RETURNS:                                                                */
  58. /*                                                                            */
  59. /*    COMMENTS:                                                                */
  60. /*                                                                            */
  61. /*    HISTORY:                                                                */
  62. /*        1.    20 Oct 90        Created.                                        */
  63. /*                                                                            */
  64. /****************************************************************************/
  65.  
  66. void Configure( config_file )
  67.  
  68. char            *config_file;
  69.  
  70. {
  71. FILE            *fp;
  72. char            s[MAXLINE], *name, *value;
  73.  
  74. if ( ( fp = fopen( config_file, "r" ) ) == NULL )
  75.     {
  76.     perror( config_file );
  77.     exit( 1 );
  78.     }
  79. while ( fgets( s, MAXLINE, fp ) != NULL )
  80.     {
  81.     s[strlen(s)-1] = NULL;                    /* kill trailing newline */
  82.     if ( s[0] != '\0' && s[0] != '#' )        /* skip blank and comment lines */
  83.         {
  84.         if ( ( name = strtok( s, "=" ) ) == NULL )
  85.             fprintf( stderr, "Can't parse '%s'\n", s );
  86.         else
  87.             {
  88.             value = strtok( NULL, "=" );
  89.             (void) SetVar( name, value );
  90.             }
  91.         }
  92.     }
  93. fclose( fp );
  94. }
  95.  
  96.  
  97. /****************************************************************************/
  98. /*    FUNCTION:    SetVar                                                        */
  99. /*                                                                            */
  100. /*    PURPOSE:    Set the value of a configuration variable.                    */
  101. /*                                                                            */
  102. /*    INPUT PARAMETERS:                                                        */
  103. /*        NAME        I/O        DESCRIPTION                                        */
  104. /*        ----        ---        -----------                                        */
  105. /*        name         I        Variable name.                                    */
  106. /*        value         I        Variable value.                                    */
  107. /*                                                                            */
  108. /*    RETURNS:                                                                */
  109. /*        OK            Successful.                                                */
  110. /*        ERROR        Unsuccessful.                                            */
  111. /*                                                                            */
  112. /*    COMMENTS:                                                                */
  113. /*                                                                            */
  114. /*    HISTORY:                                                                */
  115. /*        1.    20 Oct 90        Created.                                        */
  116. /*                                                                            */
  117. /****************************************************************************/
  118.  
  119. SetVar( name, value )
  120.  
  121. char            *name;
  122. char            *value;
  123.  
  124. {
  125. int                i;
  126.  
  127. if ( strlen( name ) > MAX_NAME_LEN )
  128.     name[MAX_NAME_LEN-1] = NULL;
  129. if ( strlen( value ) > MAX_VALUE_LEN )
  130.     value[MAX_VALUE_LEN-1] = NULL;
  131. for ( i = 0; i < n_vars; i++ )
  132.     if ( strcmp( vars[i].name, name ) == 0 )
  133.         {
  134.         strcpy( vars[i].value, value );
  135.         return OK;
  136.         }
  137. if ( n_vars >= MAX_VARS )
  138.     {
  139.     fprintf( stderr, "Too many variables!\n" );
  140.     return ERROR;
  141.     }
  142. strcpy( vars[i].name, name );
  143. strcpy( vars[i].value, value );
  144. n_vars++;
  145. return OK;
  146. }
  147.  
  148.  
  149. /****************************************************************************/
  150. /*    FUNCTION:    GetVar                                                        */
  151. /*                                                                            */
  152. /*    PURPOSE:    Return value of configuration variable.                        */
  153. /*                                                                            */
  154. /*    INPUT PARAMETERS:                                                        */
  155. /*        NAME        I/O        DESCRIPTION                                        */
  156. /*        ----        ---        -----------                                        */
  157. /*        name         I        Name of variable.                                */
  158. /*                                                                            */
  159. /*    RETURNS:                                                                */
  160. /*        (char *)        Pointer to value.                                    */
  161. /*        (char *) NULL    No such variable known.                                */
  162. /*                                                                            */
  163. /*    COMMENTS:                                                                */
  164. /*                                                                            */
  165. /*    HISTORY:                                                                */
  166. /*        1.    20 Oct 90        Created.                                        */
  167. /*                                                                            */
  168. /****************************************************************************/
  169.  
  170. char *GetVar( name )
  171.  
  172. char            *name;
  173.  
  174. {
  175. int                i;
  176.  
  177. for ( i = 0; i < n_vars; i++ )
  178.     if ( strcmp( vars[i].name, name ) == 0 )
  179.         return vars[i].value;
  180. return NULLP( char );
  181. }
  182.