-
Notifications
You must be signed in to change notification settings - Fork 3
/
pass1.c
146 lines (126 loc) · 3.28 KB
/
pass1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: pass1.c,v 2.21 2012-11-28 20:49:52 Gebruiker Exp $
*/
#include <stdio.h>
#include <string.h>
#include "debug.par"
#include "sim.h"
#include "text.h"
#include "token.h"
#include "tokenarray.h"
#include "lang.h"
#include "error.h"
#include "options.h"
#include "pass1.h"
#ifdef DB_TEXT
static void db_print_text(const struct text *);
#endif
static void fprint_count(FILE *f, size_t cnt, const char *);
void
Read_Input_Files(int argc, const char *argv[], int round) {
int n;
Init_Text(argc);
Init_Token_Array();
/* Assume all texts to be new */
Number_Of_New_Texts = Number_Of_Texts;
/* Read the files */
for (n = 0; n < Number_Of_Texts; n++) {
const char *fname = argv[n];
struct text *txt = &Text[n];
if (round == 1 && !is_set_option('T')) {
fprintf(Output_File, "File %s: ", fname);
}
txt->tx_fname = fname;
txt->tx_pos = 0;
txt->tx_start =
txt->tx_limit = Text_Length();
if (is_new_old_separator(fname)) {
if (round == 1 && !is_set_option('T')) {
fprintf(Output_File, "separator\n");
}
Number_Of_New_Texts = n;
}
else {
if (!Open_Text(First, txt)) {
if (round == 1 && !is_set_option('T')) {
fprintf(Output_File,
">>>> cannot open <<<< ");
}
/* the file has still been opened
with a null file for uniformity
*/
}
while (Next_Text_Token_Obtained(First)) {
if (!Token_EQ(lex_token, End_Of_Line)) {
Store_Token(lex_token);
}
}
Close_Text(First, txt);
txt->tx_limit = Text_Length();
/* report */
if (round == 1 && !is_set_option('T')) {
fprint_count(Output_File,
txt->tx_limit - txt->tx_start,
token_name
);
fprintf(Output_File, ", ");
fprint_count(Output_File, lex_nl_cnt-1, "line");
if (lex_non_ascii_cnt) {
fprintf(Output_File, ", ");
fprint_count(Output_File,
lex_non_ascii_cnt,
"non-ASCII character"
);
}
fprintf(Output_File, "\n");
}
#ifdef DB_TEXT
db_print_text(txt);
#endif /* DB_TEXT */
}
fflush(Output_File);
}
/* report total */
if (round == 1 && !is_set_option('T')) {
fprintf(Output_File, "Total: ");
fprint_count(Output_File, Text_Length() - 1, token_name);
fprintf(Output_File, "\n\n");
fflush(Output_File);
}
}
static void
fprint_count(FILE *f, size_t cnt, const char *unit) {
/* Prints a grammatically correct string "%u %s[s]"
for units that form their plural by suffixing -s.
*/
fprintf(f, "%zu %s%s", cnt, unit, (cnt == 1 ? "" : "s"));
}
#ifdef DB_TEXT
static void
db_print_text(const struct text *txt) {
/* prints a text (in compressed form) */
size_t i;
fprintf(Debug_File, "\n\n**** DB_PRINT_TEXT ****\n");
fprintf(Debug_File, "File \"%s\", %zu %ss, ",
txt->tx_fname, txt->tx_limit - txt->tx_start, token_name
);
fprintf(Debug_File, "txt->tx_start = %zu, txt->tx_limit = %zu\n",
txt->tx_start, txt->tx_limit
);
int BoL = 1;
for (i = txt->tx_start; i < txt->tx_limit; i++) {
if (BoL) {
fprintf(Debug_File, "[%zd]:", i);
BoL = 0;
}
fprintf(Debug_File, " ");
fprint_token(Debug_File, Token_Array[i]);
if ((i - txt->tx_start + 1) % 10 == 0) {
fprintf(Debug_File, "\n");
BoL = 1;
}
}
fprintf(Debug_File, "\n");
}
#endif /* DB_TEXT */