home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / wincam.zip / winc_src.zip / config.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  3KB  |  115 lines

  1. /*
  2.  * configuration tool -- takes care of reading in the config file, and
  3.  * handles the defaulting of requested information.
  4.  * 
  5.  * Copyright (C) 1996, Paul G. Fox
  6.  * 
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License as published by the
  9.  * Free Software Foundation; either version 2 of the License, or (at your
  10.  * option) any later version.
  11.  * 
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License along
  18.  * with this program; if not, write to the Free Software Foundation, Inc.,
  19.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21. #define NDEBUG 1
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include "trace.h"
  27.  
  28. extern int mergeenv(char *file, int replace);
  29.  
  30. char *
  31. winc_configvar(char *var, char *defaultval)
  32. {
  33.     char *cp = 0;
  34.     char *s;
  35.     char var_with_id[256];
  36.     static env_inited = 0;
  37.     static char *id = 0;
  38.     char fname[512];
  39.  
  40.     if (!env_inited++) {
  41.     int i;
  42.         char *tempbuff;
  43.         tempbuff = getenv("WINC_RC");
  44.     if(tempbuff) 
  45.           strcpy(fname, tempbuff);
  46.         else
  47.          {
  48.             tempbuff = getenv("ETC");
  49.             strcpy(fname, tempbuff);
  50.         strcat(fname, "\\winc.rc");
  51.          }
  52.  
  53.     TRACE("r", __FILE__ ": reading config from %s\n", fname);
  54.  
  55.     i = mergeenv(fname, 0);
  56.     if (i < 0) {
  57.         errormsg(__FILE__ ": hard error reading \"%s\"\n", fname);
  58.         return NULL;
  59.     }
  60.     if (i)
  61.         errormsg(__FILE__ ": syntax error at line %d of \"%s\"\n",
  62.         i, fname);
  63.  
  64.     id = getenv("WinCamId");
  65.     TRACE("r", __FILE__ ": camera id is %s\n", id ? id:"null");
  66.     }
  67.  
  68.     s = "configured";
  69.     if (id) {
  70.     /* i wish snprintf were standard... */
  71.     strncpy(var_with_id, id, sizeof(var_with_id));
  72.     strncat(var_with_id, var, sizeof(var_with_id) - strlen(id));
  73.     cp = getenv(var_with_id);
  74.     if (cp)
  75.         var = var_with_id;  /* for the TRACE statement below */
  76.     }
  77.  
  78.     /* if there was no id, or if looking up the id'ed version failed, look
  79.      * for the non-id'ed version.
  80.      */
  81.     if (!cp) {
  82.     cp = getenv(var);
  83.     if (!cp) {
  84.         assert(defaultval);
  85.         cp = defaultval;
  86.         s = "default";
  87.     }
  88.     }
  89.  
  90.     TRACE("r", __FILE__ ": returning %s %s value: %s\n", var, s, cp);
  91.     return cp;
  92. }
  93.  
  94. int
  95. cfg_number(char *s)
  96. {
  97.     return strtol(s, 0, 0);
  98. }
  99.  
  100. int
  101. cfg_boolean(char *s)
  102. {
  103.     int n = strlen(s);
  104.     if (!strncmp("ON", s, n) ||
  105.     !strncmp("TRUE", s, n) ||
  106.     !strncmp("YES", s, n) ||
  107.     !strncmp("on", s, n) ||
  108.     !strncmp("true", s, n) ||
  109.     !strncmp("yes", s, n) ||
  110.     !strncmp("1", s, n))
  111.     return 1;
  112.  
  113.     return 0;
  114. }
  115.