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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /**** init_globals(fp, names, types, ...)
  4. **
  5. ** public domain by Raymond Gardner     Sept. 1991
  6. **
  7. ** fp is a FILE * to the (already fopen'ed) file containing
  8. **      initialization data
  9. **  names is a space-separated list of names of globals (as they
  10. **      are to appear in the data file)
  11. **  types is a list of datatype characters, corresponding to names
  12. **      i  for a pointer to integer
  13. **      s  for a pointer to string (already allocated char array)
  14. **      p  for a pointer to pointer to char (space will be malloc'd)
  15. **    (NOTE: no whitespace allowed in types !!)
  16. **  followed by var arg list of pointers to variables to init
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include "initvars.h"
  24.  
  25. int init_globals(FILE *fp, char *names, char *types, ...)
  26. {
  27.     char ln[LNSZ];
  28.     char *p;
  29.     va_list arglist;
  30.     char *namep, *typep, name[40], *e;
  31.     void *argp;
  32.     int k;
  33.  
  34.     while ( fgets(ln, LNSZ, fp) ) {             /* read init file */
  35.         while ( isspace(ln[0]) )            /* drop leading whitespace */
  36.             memmove(ln, ln+1, strlen(ln));
  37.         if ( ln[0] == 0 )                       /* skip if blank line */
  38.             continue;
  39.         p = strchr(ln, '=');                    /* find equal sign */
  40.         if ( p == NULL )                        /* error if none */
  41.             return -1;  /* or continue; */
  42.         while ( p > ln && isspace(p[-1]) ) {    /* remove whitespace */
  43.             memmove(p-1, p, strlen(p-1));       /*   before = sign */
  44.             --p;
  45.         }
  46.         *p++ = 0;                               /* plug EOS over = sign */
  47.         while ( isspace(p[0]) )             /* remove leading space on */
  48.             memmove(p, p+1, strlen(p));         /*    init string */
  49.         k = strlen(p) - 1;                      /* init string length */
  50.         if ( k < 1 )
  51.             return -1;
  52.  
  53.         if ( p[k] != '\n' )             /* if '\n' is missing, input */
  54.             return -1;                  /*   exceeded buffer; error return */
  55.         p[k] = 0;                       /* plug EOS over newline */
  56.  
  57.         va_start(arglist, types);       /* setup for arglist search */
  58.  
  59.         namep = names;                  /* init ptr to var names */
  60.         typep = types;                  /* init ptr to var types */
  61.         while ( *namep == ' ' )         /* skip blanks before namelist */
  62.             ++namep;
  63.         while ( *typep ) {          /* while any typelist items left...*/
  64.  
  65.             argp = (void *)va_arg(arglist, void *); /* get var arg */
  66.  
  67.             k = strcspn(namep, " ");        /* length of namelist entry */
  68.             memmove(name, namep, k);        /* put into name hold area */
  69.             name[k] = 0;                    /* terminate it */
  70.             if ( strcmp(name, ln) != 0 ) { /* if it doesn't match... */
  71.                 namep += k;                 /* get next name */
  72.                 while ( *namep == ' ' )
  73.                     ++namep;
  74.                 ++typep;                    /* get next type */
  75.             } else {                        /* else name is found... */
  76.                 if ( *typep == 'i' ) {      /* if it's an int, init it */
  77.                     *(int *)argp = atoi(p);
  78.                 } else if ( *typep == 's' || *typep == 'p' ) {
  79.                     if ( *p == '"' ) {      /* is string in quotes? */
  80.                         ++p;                /* skip leading quote, and */
  81.                         e = strchr(p, '"'); /* look for trailing quote */
  82.                         if ( e )            /* terminate string if found */
  83.                             *e = 0;
  84.                     }
  85.                     if ( *typep == 'p' ) {      /* if it's a char *ptr */
  86.                         e = malloc(strlen(p) + 1); /* get space */
  87.                         if ( e == 0 ) {         /* error if no space */
  88.                             return -1; /* call va_end(arglist); first? */
  89.                         }
  90.                         *(char **)argp = e;
  91.                         strcpy(*(char **)argp, p);  /* copy in string */
  92.                     } else                          /* must be char array */
  93.                         strcpy(argp, p);            /* copy in string */
  94.                 } else {
  95.                     return -1;                      /* bad type */
  96.                 }
  97.                 break;              /* break search; get next line */
  98.             }
  99.         }
  100.         va_end(arglist);
  101.     }
  102.     return 0;
  103. }
  104.  
  105. #ifdef TEST
  106.  
  107. #include "errors.h"                 /* For cant()     */
  108.  
  109. int foo;
  110. char bar[80];
  111. int baz;
  112. char *quux;
  113.  
  114. int main(int argc, char **argv)
  115. {
  116.     FILE *fp;
  117.     int k;
  118.  
  119.     if ( argc < 2 ) {
  120.         fprintf(stderr, "missing arg\n");
  121.         exit(1);
  122.     }
  123.  
  124.     fp = cant(argv[1], "r");
  125.     k = init_globals(fp, "foo bar baz quux", "isip",
  126.         &foo, bar, &baz, &quux);
  127.     printf("k: %d\n", k);
  128.     printf("foo: %d\nbar: <%s>\nbaz: %d\nquux: <%s>\n",
  129.         foo, bar, baz, quux);
  130.     fclose(fp);
  131.  
  132.     return 0;
  133. }
  134.  
  135. #endif /* TEST */
  136.