text.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: text.c,v 1.2 2001/11/13 12:55:58 dick Exp $
  4. */
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include "debug.par"
  8. #include "sim.h"
  9. #include "token.h"
  10. #include "stream.h"
  11. #include "lex.h"
  12. #include "options.h"
  13. #include "error.h"
  14. #include "text.h"
  15. struct newline {
  16. unsigned char nl_tk_diff; /* token position difference */
  17. };
  18. #define NL_INCR 1000 /* increment of newline buffer size */
  19. static struct newline *nl_buff; /* to be filled by malloc */
  20. static unsigned int nl_size; /* size of nl_buff[] */
  21. static unsigned int nl_free; /* next free position in nl_buff[] */
  22. static unsigned int nl_next, nl_limit; /* nl_buff[] pointers during pass 2 */
  23. static void store_newline(void);
  24. static void init_nl_buff(void);
  25. /* TEXT INTERFACE */
  26. static unsigned int last_tk_cnt; /* token count at newline */
  27. static unsigned int last_nl_cnt; /* nl counter during pass 2 */
  28. void
  29. InitText(int nfiles) {
  30. /* allocate the array of text descriptors */
  31. NumberOfTexts = nfiles;
  32. Text = (struct text *)
  33. malloc((unsigned int)(NumberOfTexts*sizeof (struct text)));
  34. if (!Text) fatal("out of memory");
  35. init_nl_buff();
  36. }
  37. int
  38. OpenText(enum Pass pass, struct text *txt) {
  39. switch (pass) {
  40. case First:
  41. last_tk_cnt = 0;
  42. if (nl_buff) {
  43. txt->tx_nl_start = nl_free;
  44. }
  45. break;
  46. case Second:
  47. last_tk_cnt = 0;
  48. if (nl_buff) {
  49. nl_next = txt->tx_nl_start;
  50. nl_limit = txt->tx_nl_limit;
  51. last_nl_cnt = 1;
  52. lex_nl_cnt = 1;
  53. lex_tk_cnt = 0;
  54. return 1;
  55. }
  56. break;
  57. }
  58. return OpenStream(txt->tx_fname);
  59. }
  60. int
  61. NextTextTokenObtained(enum Pass pass) {
  62. register int ok = 0; /* gcc does not understand enum Pass */
  63. switch (pass) {
  64. case First:
  65. ok = NextStreamTokenObtained();
  66. if (TOKEN_EQ(lex_token, EOL)) {
  67. store_newline();
  68. last_tk_cnt = lex_tk_cnt;
  69. }
  70. break;
  71. case Second:
  72. /* get newline info from the buffer or from the file itself */
  73. if (nl_buff) {
  74. if (nl_next == nl_limit) {
  75. ok = 0;
  76. }
  77. else {
  78. struct newline *nl = &nl_buff[nl_next++];
  79. lex_nl_cnt = ++last_nl_cnt;
  80. lex_tk_cnt = (last_tk_cnt += nl->nl_tk_diff);
  81. lex_token = EOL;
  82. ok = 1;
  83. }
  84. }
  85. else {
  86. while ( (ok = NextStreamTokenObtained())
  87. && !TOKEN_EQ(lex_token, EOL)
  88. ) {
  89. /* skip */
  90. }
  91. }
  92. break;
  93. }
  94. return ok;
  95. }
  96. void
  97. CloseText(enum Pass pass, struct text *txt) {
  98. switch (pass) {
  99. case First:
  100. if (nl_buff) {
  101. if (last_tk_cnt != lex_tk_cnt) {
  102. /* there were tokens after the last newline */
  103. store_newline();
  104. }
  105. txt->tx_nl_limit = nl_free;
  106. }
  107. break;
  108. case Second:
  109. break;
  110. }
  111. CloseStream();
  112. }
  113. /* NEWLINE CACHING */
  114. /* To speed up pass2 which is interested in token positions at line ends,
  115. the newline buffer keeps this info from pass1. To reduce the size of
  116. the newline buffer, the info is kept as the differences of the values
  117. at consecutive line ends. This allows unsigned chars to be used rather
  118. than integers.
  119. The recording of token position differences at EOL is optional, and
  120. is switched off if
  121. - there is not room enough for the newline buffer.
  122. - a difference would not fit in the field in the struct.
  123. Switching off is done by freeing the buffer and setting nl_buff to 0.
  124. Anybody using nl_buff should therefore test for nl_buff being zero.
  125. */
  126. static void abandon_nl_buff(void);
  127. static void
  128. init_nl_buff(void) {
  129. /* Allocate the newline buffer, if possible */
  130. nl_size = 0 + NL_INCR;
  131. nl_buff = (option_set('x') ? 0 :
  132. (struct newline *)malloc(sizeof (struct newline) * nl_size)
  133. );
  134. }
  135. static void
  136. store_newline(void) {
  137. if (!nl_buff) return;
  138. if (nl_free == nl_size) {
  139. /* allocated array is full; try to increase its size */
  140. unsigned int new_size = nl_size + NL_INCR;
  141. struct newline *new_buff = (struct newline *)realloc(
  142. (char *)nl_buff,
  143. sizeof (struct newline) * new_size
  144. );
  145. if (!new_buff) {
  146. /* we failed */
  147. abandon_nl_buff();
  148. return;
  149. }
  150. nl_buff = new_buff, nl_size = new_size;
  151. }
  152. /* now we are sure there is room enough */
  153. {
  154. register struct newline *nl = &nl_buff[nl_free++];
  155. register unsigned int tk_diff = lex_tk_cnt - last_tk_cnt;
  156. nl->nl_tk_diff = tk_diff;
  157. if (nl->nl_tk_diff != tk_diff) {
  158. /* tk_diff does not fit in nl_tk_diff */
  159. abandon_nl_buff();
  160. }
  161. }
  162. }
  163. static void
  164. abandon_nl_buff(void) {
  165. if (nl_buff) {
  166. free((char *)nl_buff);
  167. nl_buff = 0;
  168. }
  169. }
  170. #ifdef DB_NL_BUFF
  171. void
  172. db_print_nl_buff(unsigned int start, unsigned int limit) {
  173. int i;
  174. fprintf(DebugFile, "\n**** DB_NL_BUFF ****\n");
  175. if (!nl_buff) {
  176. fprintf(DebugFile, ">>>> NO NL_BUFF\n\n");
  177. return;
  178. }
  179. if (start > nl_free) {
  180. fprintf(DebugFile, ">>>> start (%u) > nl_free (%u)\n\n",
  181. start, nl_free
  182. );
  183. return;
  184. }
  185. if (limit > nl_free) {
  186. fprintf(DebugFile, ">>>> limit (%u) > nl_free (%u)\n\n",
  187. limit, nl_free
  188. );
  189. return;
  190. }
  191. fprintf(DebugFile, "nl_buff: %u entries:\n", nl_free);
  192. for (i = start; i < limit; i++) {
  193. struct newline *nl = &nl_buff[i];
  194. fprintf(DebugFile, "nl_tk_diff = %d\n", nl->nl_tk_diff);
  195. }
  196. fprintf(DebugFile, "\n");
  197. }
  198. #endif /* DB_NL_BUFF */