home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 329_01 / cs_hash.c < prev    next >
C/C++ Source or Header  |  1990-10-22  |  3KB  |  119 lines

  1. /* hash.c */
  2. /* storing, handling and lookup of the substitutions.
  3.  * We use a second hash table, not the same as for symbols.
  4.  */
  5. /*
  6.  
  7. This programme is shareware.
  8. If you found this programme useful then send me a colourful postcard
  9. with the words "Happy Birthday" (or equivalent) so that it arrives
  10. at my address around the first of January 1991.
  11.  
  12. Henri de Feraudy
  13. 27 rue Chef de Ville
  14. 92140 Clamart 
  15. France
  16.  
  17.  */
  18.  
  19. #include "csubst.h" 
  20.  
  21. #define HASHSIZE 103 /* a prime number */
  22.  
  23. extern int Just_met_EOF; /* in main.c */
  24.  
  25. char O_string_buffer[SYMBOL_SIZE + 1]; /* used in csubst.l */
  26.  
  27. /* this structure is used to store a substitution pair */
  28. struct substitution{
  29.             char *original;/* text of original identifier */
  30.             char *replacement;/* text of new identifier */
  31.             struct substitution *next; /* link in "bucket" */
  32.             };
  33. /* this global is the hash table used to store all pairs */
  34. static struct substitution *Hash_table[HASHSIZE];
  35.  
  36. /* this function is used to find the adresses in the hash table of a 
  37.    substitution 
  38.  */
  39.  
  40. int hash(str)
  41. char *str;
  42. {
  43. int ret = 0;
  44.  
  45. while(*str)
  46.      ret += *str++;
  47.  
  48. return ret % HASHSIZE;
  49. }
  50.  
  51. /* This routine tells the programme that r_string is going to
  52.    be the string associated with o_string
  53.  */
  54. void install_subst(o_string,r_string)
  55. char *o_string;
  56. char *r_string;
  57. {
  58. #ifdef ANSI_C
  59. extern void *malloc();
  60. #else
  61. extern char *malloc();
  62. #endif
  63.  
  64. int index;
  65. struct substitution *bucket;
  66.  
  67. index = hash(o_string);
  68. bucket = Hash_table[index];
  69. while(bucket != NULL && strncmp(o_string, bucket->original,SYMBOL_SIZE))
  70.      bucket = bucket->next;
  71. if(bucket == NULL){
  72.   bucket = (struct substitution *)malloc(sizeof(struct substitution));
  73.   bucket->original = (char *)malloc(strlen(o_string)+1);
  74.   bucket->replacement = (char *)malloc(strlen(r_string)+1);
  75.   strcpy(bucket->original, o_string);
  76.   strcpy(bucket->replacement, r_string);
  77.   bucket->next = Hash_table[index];
  78.   Hash_table[index] = bucket;
  79.   }
  80. }
  81.  
  82. /* This routine stuffs the substitution pairs into the hash table
  83.  * It supposes that identifiers are at most SYMBOL_SIZE chars long
  84.  * It will only be called if the programme is reading substitution
  85.  * pairs.
  86.  */
  87. void init_csubst()
  88. {
  89. int i;
  90.  
  91. for(i = 0; i < HASHSIZE; i++)
  92.    {
  93.    Hash_table[i] = NULL;
  94.    }
  95.  
  96. O_string_buffer[SYMBOL_SIZE] = '\0'; /* to make sure you can print it */
  97. }
  98.  
  99. /* this function returns null if o_string is not to be replaced
  100.    otherwise it returns the repalcement string.
  101.  */
  102. char *lookup_subst(o_string)
  103. char *o_string;
  104. {
  105. int index;
  106. struct substitution *bucket;
  107. index = hash(o_string);
  108. bucket = Hash_table[index];
  109. while(bucket != NULL && strcmp(o_string, bucket->original))
  110.      {
  111.      bucket = bucket->next;
  112.      }
  113. if(bucket == NULL)
  114.   return NULL;
  115. else
  116.   return bucket->replacement;
  117. }
  118. /* end of file */
  119.