home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsd / explain / !Explain / c / lookup next >
Encoding:
Text File  |  1994-04-30  |  2.6 KB  |  114 lines

  1. /* lookup.c - perform a similar function to msgs library, but allow non alpha characters in tokens */
  2.  
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #include "os.h"
  7.  
  8. #include "misc.h"
  9. #include "tracker.h"
  10.  
  11. static struct
  12. {  char *file;
  13.    char **lp;
  14.    int size, lc;
  15. } lookup__info;
  16.  
  17. os_error *lookup_init(char *name)
  18. {  os_filestr fcb;
  19.    os_error *err;
  20.    char *cp, *cpl;
  21.    int lc;
  22.  
  23.    fcb.action = 17;
  24.    fcb.name   = name;
  25.    if (err = os_file(&fcb), err) return err;
  26.  
  27.    if (fcb.action != 1)
  28.    {  fcb.loadaddr = fcb.action;
  29.       fcb.action   = 19;
  30.       return os_file(&fcb);
  31.    }
  32.  
  33.    lookup__info.size = fcb.start;
  34.  
  35.    if (err = misc_malloc((void **) &lookup__info.file, lookup__info.size + 1), err)
  36.       return err;
  37.  
  38.    fcb.action   = 0xFF;
  39.    fcb.loadaddr = (int) lookup__info.file;
  40.    fcb.execaddr = 0;
  41.  
  42.    if (err = os_file(&fcb), err) goto fail;
  43.  
  44.    /* Loaded file, count lines */
  45.  
  46.    for (cp = lookup__info.file, cpl = lookup__info.file + lookup__info.size, lc = 0; cp < cpl; cp++)
  47.       if (*cp == '\n') ++lc;
  48.  
  49.    if (lookup__info.file[lookup__info.size-1] != '\n')
  50.    {  lookup__info.file[lookup__info.size++] = '\n';
  51.       ++lc; /* + one if last line has no \n */
  52.    }
  53.  
  54.    if (err = misc_malloc((void **) &lookup__info.lp, lc * sizeof(char *)), err)
  55.       goto fail;
  56.  
  57.    for (cp = lookup__info.file, cpl = lookup__info.file + lookup__info.size, lc = 0; cp < cpl; )
  58.    {  char *lp = cp; /* remember line start */
  59.  
  60.       while (cp < cpl && *cp != '\n' && isspace(*cp))
  61.          cp++;
  62.  
  63.       switch (*cp)
  64.       {  case '\n': case '#':
  65.             break;
  66.  
  67.          default:  /* look for a ':' */
  68.             while (cp < cpl && !isspace(*cp) && *cp != ':')
  69.                cp++;
  70.  
  71.             if (*cp == ':')
  72.                lookup__info.lp[lc++] = lp;
  73.             else
  74.             {  err = misc_err("lookup1", name);
  75.                goto fail2;
  76.             }
  77.             break;
  78.       }
  79.  
  80.       while (cp < cpl && *cp != '\n')
  81.          cp++;
  82.  
  83.       if (*cp == '\n') *cp++ = '\0'; /* convert '\n' to '\0'; */
  84.    }
  85.  
  86.    lookup__info.lc = lc;
  87.  
  88.    return NULL;
  89.  
  90. fail2:
  91.    misc_free((void **) &lookup__info.lp);
  92.    lookup__info.lp = NULL;
  93.  
  94. fail:
  95.    misc_free((void **) &lookup__info.file);
  96.    lookup__info.file = NULL;
  97.    return err;
  98. }
  99.  
  100. char *lookup(char *token)
  101. {  int ln, l;
  102.  
  103.    for (l = 0; token[l]; l++) if (token[l] == ':') break;
  104.  
  105.    for (ln = 0; ln < lookup__info.lc; ln++)
  106.    {  if (strlen(lookup__info.lp[ln]) > l + 1 &&
  107.           lookup__info.lp[ln][l] == ':' &&
  108.           memcmp(token, lookup__info.lp[ln], l) == 0)
  109.          return lookup__info.lp[ln] + l + 1;
  110.    }
  111.  
  112.    return token[l] == ':' ? token + l + 1 : token;
  113. }
  114.