home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c329 / 2.img / EXAMPLES / LOOKUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-14  |  2.0 KB  |  80 lines

  1. /*
  2.  * LOOKUP.C
  3.  *
  4.  * Purpose: demonstrates calling NDP C function
  5.  * from NDP Pascal program
  6.  * Copyright (C) MicroWay, Inc 1989
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define STSIZE 200
  14. #define NAMESIZE 30
  15. #define NOTKNOWN 0
  16.  
  17. typedef struct SYMBOL {
  18.         char name [NAMESIZE];
  19.         int symtype;
  20.         struct SYMBOL *nextsym;
  21. };
  22.  
  23. extern struct SYMBOL *symtable[];
  24.  
  25. struct SYMBOL *lookup (identifier)
  26. char *identifier;
  27.  
  28. {
  29.         struct  SYMBOL  *p;
  30.         int     hashval;
  31.         register char *pi;      /* pointer to identifier  */
  32.  
  33.         pi = identifier;        /* into register for speed */
  34.  
  35.         hashval = 0;    
  36.         while (*pi)             /* sum all chars in string */
  37.                 hashval += *pi++;
  38.         
  39.                                 /* index is sum modulo size of array */
  40.         p = symtable [hashval % STSIZE];
  41.  
  42.         while (p != NULL) {
  43.                 if (strcmp (p->name,identifier) == 0)
  44.                         return (p);
  45.                 else
  46.                         p = p->nextsym;
  47.         }
  48.         return (NULL);
  49.  
  50. }       /* end of lookup */
  51.  
  52.  
  53. struct SYMBOL *install(identifier)
  54. char *identifier;
  55.  
  56. {
  57.         struct  SYMBOL  *p;
  58.         int hashval;
  59.         register char *pi;      /* pointer to identifier  */
  60.         
  61.         pi = identifier;        /* into register for speed */
  62.         
  63.         hashval = 0;
  64.         while (*pi)             /* sum all chars in string */
  65.                 hashval += *pi++;
  66.         hashval %= STSIZE;      /* index is sum modulo size of array */
  67.         
  68.         p = (struct SYMBOL *) malloc (sizeof (struct SYMBOL));
  69.         strcpy (p->name, identifier);
  70.         
  71.         p->symtype = NOTKNOWN;  /* at first, type is not known */
  72.         
  73.         /* insert new symbol table entry at head of linked list */
  74.         p->nextsym =  symtable [hashval];
  75.         symtable [hashval] = p;             
  76.  
  77.         return (p);
  78.  
  79. }       /* end of install */
  80.