home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / tcpsrv12.exe / TCPSRV12.TAR / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-27  |  2.2 KB  |  102 lines

  1. /* config.c */
  2. /*
  3.  
  4.  * Copyright 1994 A.Oliver De Guzman
  5.  * All rights reserved
  6.  
  7.  *   Permission is granted to any individual to copy, use, and/or
  8.  * distribute this software provided that the distribution retains this
  9.  * entire copyright notice. No part of this software may be used and/or
  10.  * sold for profit or used with any commercial product.
  11.  
  12.  * DISCLAIMER:
  13.  *   This software comes with NO WARRANTIES of any kind. In no event
  14.  * will the author be liable for any financial, physical, moral, and/or
  15.  * mental damages incurred directly or indirectly by the use or intent
  16.  * to use of this software.
  17.  
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include "config.h"
  25.  
  26. #define MAXPATH            1024
  27.  
  28. int readconfig(fname, configs, nconfigs)
  29. char *fname;
  30. Config configs[];
  31. int nconfigs;
  32. {
  33.     FILE *fp;
  34.     char *home, *comment, path[MAXPATH];
  35.     char line[MAXCFGLINE];
  36.     char line2[MAXCFGLINE];
  37.     int n=0, k=0;
  38.  
  39.     strcpy(path, fname);
  40.  
  41.     if (!(fp = fopen(path, "r"))) return -1;
  42.  
  43.     while (fgets(line, MAXCFGLINE, fp) != NULL){
  44.         k += strlen(line);
  45.         while (isspace(line[strlen(line)-1]))
  46.             line[strlen(line)-1] = '\0';
  47.  
  48.         while (!((comment=(char *)strstr(line,"#")) &&
  49.             !(comment!=line && (*(comment-1)=='%'))) &&
  50.             line[strlen(line)-1] == '\\'){
  51.  
  52.             line[strlen(line)-1] = '\0';
  53.             if (fgets(line2, MAXCFGLINE,fp))
  54.                 strncat(line, line2, MAXCFGLINE);
  55.         }
  56.  
  57.         if (comment && !(comment!=line && (*(comment-1)=='%')))
  58.             *comment = '\0';
  59.  
  60.          n += (configline(line, configs, nconfigs) != -1);
  61.     }
  62.  
  63.     fclose(fp);
  64.     return(n);
  65. }
  66.  
  67.  
  68. int configline(line, configs, nconfigs)
  69. char *line;
  70. Config configs[];
  71. int nconfigs;
  72. {
  73.     char name[MAXCFGLINE], *s;
  74.     int i, n= -1;
  75.  
  76.     name[0] = '\0';
  77.     sscanf(line," %[_A-Za-z]", name);
  78.     for (i=0; i<nconfigs; i++){
  79.         if (!strcmp(name, configs[i].cfgname)){
  80.             n = i;
  81.             switch (configs[i].cfgtype){
  82.                 case CFG_KEY :
  83.                     break;
  84.                 case CFG_INT :
  85.                     sscanf(line," %[_A-Za-z] %d",name,configs[i].cfgvar);
  86.                     break;
  87.                 case CFG_CHAR :
  88.                     sscanf(line," %[_A-Za-z] %c",name,configs[i].cfgvar);
  89.                     break;
  90.                 case CFG_STR :
  91.                     s = line+strlen(name);
  92.                     while (*s && isspace(*s)) s++;
  93.                     strcpy((char *)configs[i].cfgvar, s);    
  94.                     break;
  95.                 default:
  96.                     break;
  97.             }
  98.         }
  99.     }
  100.     return(n);
  101. }
  102.