pass2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: pass2.c,v 2.10 2004/08/05 09:49:46 dick Exp $
  4. */
  5. #include <stdio.h>
  6. #include "debug.par"
  7. #include "sim.h"
  8. #include "text.h"
  9. #include "lex.h"
  10. #include "pass2.h"
  11. #ifdef DB_POS
  12. static void db_print_pos_list(const char *, const struct position *);
  13. static void db_print_lex(const char *);
  14. #endif
  15. static void pass2_txt(struct text *txt);
  16. static int next_eol_obtained(void);
  17. void
  18. Pass2(void) {
  19. int n;
  20. for (n = 0; n < NumberOfTexts; n++) {
  21. pass2_txt(&Text[n]);
  22. }
  23. }
  24. /* instantiate sort_pos_list() */
  25. #define SORT_STRUCT position
  26. #define SORT_NAME sort_pos_list
  27. #define SORT_BEFORE(p1,p2) ((p1)->ps_tk_cnt < (p2)->ps_tk_cnt)
  28. #define SORT_NEXT ps_next
  29. #include "sortlist.bdy"
  30. static void
  31. pass2_txt(struct text *txt) {
  32. register struct position *pos;
  33. register unsigned int old_nl_cnt;
  34. if (!txt->tx_pos) /* no need to scan the file */
  35. return;
  36. if (!OpenText(Second, txt)) {
  37. fprintf(stderr, ">>>> File %s disappeared <<<<\n",
  38. txt->tx_fname
  39. );
  40. }
  41. /* sets lex_nl_cnt and lex_tk_cnt */
  42. #ifdef DB_POS
  43. db_print_pos_list("before sorting", txt->tx_pos);
  44. #endif /* DB_POS */
  45. sort_pos_list(&txt->tx_pos);
  46. #ifdef DB_POS
  47. db_print_pos_list("after sorting", txt->tx_pos);
  48. #endif /* DB_POS */
  49. #ifdef DB_NL_BUFF
  50. db_print_nl_buff(txt->tx_nl_start, txt->tx_nl_limit);
  51. #endif /* DB_NL_BUFF */
  52. old_nl_cnt = 1;
  53. pos = txt->tx_pos;
  54. while (pos) {
  55. /* we scan the pos list and the file in parallel */
  56. /* find the corresponding line */
  57. while (pos->ps_tk_cnt >= lex_tk_cnt) {
  58. /* pos does not refer to this line, try the next */
  59. /* shift the administration */
  60. old_nl_cnt = lex_nl_cnt;
  61. /* and get the next eol position */
  62. if (!next_eol_obtained()) {
  63. /* ouch! not enough lines! */
  64. fprintf(stderr, ">>>> File %s modified <<<<\n",
  65. txt->tx_fname
  66. );
  67. break;
  68. }
  69. #ifdef DB_POS
  70. db_print_lex(txt->tx_fname);
  71. #endif /* DB_POS */
  72. }
  73. /* fill in the pos */
  74. switch (pos->ps_type) {
  75. case 0: /* first token of run */
  76. pos->ps_nl_cnt = old_nl_cnt;
  77. break;
  78. case 1: /* last token of run */
  79. pos->ps_nl_cnt = lex_nl_cnt;
  80. break;
  81. }
  82. /* and get the next pos */
  83. pos = pos->ps_next;
  84. }
  85. #ifdef DB_POS
  86. db_print_pos_list("after scanning", txt->tx_pos);
  87. #endif /* DB_POS */
  88. CloseText(Second, txt);
  89. }
  90. static int
  91. next_eol_obtained(void) {
  92. while (NextTextTokenObtained(Second)) {
  93. if (TOKEN_EQ(lex_token, EOL)) return 1;
  94. }
  95. return 0;
  96. }
  97. #ifdef DB_POS
  98. static void
  99. db_print_pos(const struct position *pos) {
  100. fprintf(DebugFile, "pos type: %s; token count: %u",
  101. (pos->ps_type == 0 ? "first" : " last"),
  102. pos->ps_tk_cnt
  103. );
  104. fprintf(DebugFile, ", line#: ");
  105. if (pos->ps_nl_cnt == -1) {
  106. fprintf(DebugFile, "<NOT SET>");
  107. }
  108. else {
  109. fprintf(DebugFile, "%u", pos->ps_nl_cnt);
  110. }
  111. fprintf(DebugFile, "\n");
  112. }
  113. static void
  114. db_print_pos_list(const char *msg, const struct position *pos) {
  115. fprintf(DebugFile, "\n**** DB_PRINT_POS_LIST, %s ****\n", msg);
  116. while (pos) {
  117. db_print_pos(pos);
  118. pos = pos->ps_next;
  119. }
  120. fprintf(DebugFile, "\n");
  121. }
  122. static void
  123. db_print_lex(const char *fn) {
  124. fprintf(DebugFile, "%s: lex_tk_cnt = %u, lex_nl_cnt = %u\n",
  125. fn, lex_tk_cnt, lex_nl_cnt);
  126. }
  127. #endif /* DB_POS */