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_xtrct.c < prev    next >
C/C++ Source or Header  |  1990-11-09  |  4KB  |  176 lines

  1. /* cs_xtrct.c */
  2. /* These routines take care of a "symbol table" which contains
  3.  * all things one might want to extract from a source file
  4.  * when mode is EXTRACT_SYMBOLS
  5.  */
  6. #include "csubst.h"
  7.  
  8. #define HASHSIZE 103 /* a prime number */
  9. #define MAXTOTAL 32767  /* the maximum value of symbols */
  10.  
  11.  
  12. struct symbol{
  13.             char *original;/* text of original symbol */
  14.             symbol_type_t type;    /* flag indicates origin */
  15.             struct symbol *next; /* link in "bucket" */
  16.             };
  17. typedef int count_t; /* The type we use to count symbols */
  18. static struct symbol *Symbol_table[HASHSIZE];
  19. extern int hash();
  20.  
  21. #ifdef ANSI_C
  22. extern void *malloc();
  23. #else
  24. extern char *malloc();
  25. #endif
  26.  
  27. void init_extract()
  28. {
  29. int index;
  30. for(index = 0; index < HASHSIZE; index++)
  31.    Symbol_table[index] = NULL;
  32.  
  33. }
  34.  
  35. /* this routine returns 1 iff the symbol is in the table */
  36. int symbol_lookup(string)
  37. char *string;
  38. {
  39. int index;
  40. struct symbol *bucket;
  41.  
  42. index = hash(string);
  43. bucket = Symbol_table[index];
  44. while(bucket != NULL && strncmp(string, bucket->original,SYMBOL_SIZE))
  45.      bucket = bucket->next;
  46. return bucket != NULL;
  47. }
  48.  
  49. /* This routine tells the hash table that o_string is an symbol
  50.  */
  51.  
  52. void symbol_install(o_string, type_flag)
  53. char *o_string;
  54. symbol_type_t type_flag;
  55. {
  56. int index;
  57. struct symbol *bucket;
  58.  
  59. index = hash(o_string);
  60. bucket = Symbol_table[index];
  61. while(bucket != NULL && strncmp(o_string, bucket->original,SYMBOL_SIZE))
  62.      bucket = bucket->next;
  63. if(bucket == NULL){
  64.   bucket = (struct symbol *)malloc(sizeof(struct symbol));
  65.   bucket->original = (char *)malloc(strlen(o_string)+1);
  66.   strcpy(bucket->original, o_string);
  67.   bucket->next = Symbol_table[index];
  68.   bucket->type = type_flag;
  69.   Symbol_table[index] = bucket;
  70.   }
  71. }
  72.  
  73. /* this routine counts the number of symbols 
  74.    stored in the hash table
  75.  */
  76. count_t count_symbols()
  77. {
  78. int index;
  79. struct symbol *bucket;
  80. count_t total = 0;
  81.  
  82. for(index = 0; index < HASHSIZE; index++)
  83.    {
  84.    bucket = Symbol_table[index];
  85.    while(bucket != NULL)
  86.     {
  87.      bucket = bucket->next;
  88.     if(total == MAXTOTAL )
  89.       {
  90.       fatal("maximum number of symbols exceeded");
  91.           }
  92.      else
  93.          total++;
  94.         }
  95.    }
  96. return total;
  97. }
  98.  
  99. /* this routine is needed by qsort to compare symbols */
  100. compar(psymb1, psymb2)
  101. struct symbol **psymb1, **psymb2;
  102. {
  103. if((**psymb1).type != (**psymb2).type)
  104.   return((**psymb1).type - (**psymb2).type);
  105. else
  106. return(strcmp((*psymb1)->original, (*psymb2)->original));
  107. }
  108.  
  109. /* for display purposes */
  110. char *type_name(type)
  111. symbol_type_t type;
  112. {
  113. switch(type)
  114.     {
  115.     case TYPE_MACRO:
  116.         return("MACRO");
  117.     case TYPE_IDENTIFIER:
  118.         return("IDENTIFIER");
  119.     case TYPE_STRING:
  120.         return("STRING");
  121.     }
  122. return NULL;
  123. }
  124.  
  125. /* this routine makes a sorted table of 
  126.    the symbols and prints them
  127.  */
  128.  
  129. void sort_and_print()
  130. {
  131. struct symbol *bucket;
  132. int index;
  133. count_t total;
  134. struct symbol **ptable;
  135. struct symbol **table;
  136. symbol_type_t next_type;
  137.  
  138. total  = count_symbols();
  139. if(total == 0)
  140.   return;
  141. table = (struct symbol **)malloc(total*(sizeof(struct symbol *)));
  142. if(table == NULL)
  143.   {
  144.   fatal1("cant allocate %d ", total);
  145.   }
  146. else
  147. ptable = table;
  148. for(index = 0; index < HASHSIZE; index++)
  149.     {
  150.    bucket = Symbol_table[index];
  151.    while(bucket != NULL)
  152.     {
  153.     *ptable++ = bucket;
  154.      bucket = bucket->next;
  155.         }
  156.     }
  157. qsort(table, total, sizeof(struct symbol *), compar);
  158. ptable = table;
  159. printf("\n/* %sS */\n\n", type_name(table[0]->type));
  160.    
  161. while(total)
  162.      {
  163.      struct symbol *p_next_symbol;
  164.      total--;
  165.      if(total != 0){
  166.         p_next_symbol = *(ptable + 1);
  167.         next_type = p_next_symbol->type;
  168.         }
  169.      printf("%s\n",(*ptable)->original);
  170.      if( next_type != (*ptable)->type && total != 0)
  171.     printf("\n/* %sS */\n\n", type_name(next_type));
  172.      ptable++;
  173.      }
  174. }
  175. /* end of file */
  176.