home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff294.lzh / DNet / amiga / lib / cfg.c next >
C/C++ Source or Header  |  1989-12-11  |  1KB  |  76 lines

  1.  
  2. /*
  3.  *  Cfg.C
  4.  *
  5.  *  CONFIGURATION FILE EXTRACTION
  6.  */
  7.  
  8. #include "lib.h"
  9.  
  10. static FILE *Fi;
  11.  
  12. int
  13. OpenCfgFile()
  14. {
  15.     CloseCfgFile();
  16.     Fi = fopen("s:dnet.config", "r");
  17.     return (Fi != NULL);
  18. }
  19.  
  20. char *
  21. GetCfgLine(what)
  22. char *what;
  23. {
  24.     static char Buf[128];
  25.     register char *ptr;
  26.     if (Fi) {
  27.     while (fgets(Buf, sizeof(Buf), Fi)) {
  28.         if (BCmp(Buf, what, 4) == 0) {
  29.         Buf[strlen(Buf)-1] = 0;
  30.         for (ptr = Buf + 4; *ptr == ' ' || *ptr == 9; ++ptr);
  31.         return(ptr);
  32.         }
  33.     }
  34.     }
  35.     return(NULL);
  36. }
  37.  
  38. void
  39. CloseCfgFile()
  40. {
  41.     if (Fi)
  42.     fclose(Fi);
  43.     Fi = NULL;
  44. }
  45.  
  46. void
  47. GetOneCfg(what)
  48. char *what;
  49. {
  50.     char *str;
  51.     OpenCfgFile();
  52.     str = GetCfgLine(what);
  53.     CloseCfgFile();
  54. }
  55.  
  56. int
  57. ExtractFieldVal(str, field, pidx)
  58. char *str, *field;
  59. short *pidx;
  60. {
  61.     short idx = (pidx) ? *pidx : 0;
  62.     short flen = strlen(field);
  63.  
  64.     while (*str) {
  65.     if (strncmp(str, field, flen) == 0) {
  66.         if (pidx)
  67.         *pidx = idx + flen;    /*  past the field but not the val */
  68.         return(atoi(str + flen));
  69.     }
  70.     ++str;
  71.     ++idx;
  72.     }
  73.     return(-1);
  74. }
  75.  
  76.