-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass1.c
143 lines (125 loc) · 3.33 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
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: pass1.c,v 2.40 2017-12-11 14:12:36 dick 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 "options.h"
#include "pass1.h"
#ifdef DB_TEXT
#include "pass1_db.i"
#endif
static void
do_separator(const struct text *txt, const char *fname) {
if (!is_set_option('T')) {
fprintf(Output_File, "File %s: new/old separator\n", fname);
}
if (Number_of_New_Texts == Number_of_Texts) {
/* no new Number_of_New_Texts set yet */
Number_of_New_Texts = txt - &Text[0];
} else {
fatal("more than one new/old separator");
}
}
static int
read_text(const char *fname, struct text *txt) {
int file_opened = 0;
if (Open_Text(txt)) {
file_opened = 1;
} else {
/* print a warning */
fprintf(Output_File, "File %s: >>>> cannot open <<<<\n", fname);
/* the file has still been opened with a null file
for uniformity
*/
}
while (Next_Text_Token_Obtained()) {
if (!Token_EQ(lex_token, End_Of_Line)) {
Store_Token(lex_token);
}
}
Close_Text();
txt->tx_limit = Token_Array_Length();
txt->tx_EOL_terminated =
Token_EQ(lex_token, End_Of_Line);
#ifdef DB_TEXT
db_print_text(txt);
#endif /* DB_TEXT */
return file_opened;
}
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, "%s %s%s", size_t2string(cnt), unit, (cnt == 1 ? "" : "s"));
}
static void
report_file(const char *fname, struct text *txt) {
fprintf(Output_File, "File %s: ", fname);
fprint_count(Output_File, txt->tx_limit - txt->tx_start, Token_Name);
fprintf(Output_File, ", ");
fprint_count(Output_File,
lex_nl_cnt - 1 + (!txt->tx_EOL_terminated ? 1 : 0), "line"
);
if (!txt->tx_EOL_terminated) {
fprintf(Output_File, " (not NL-terminated)");
}
if (lex_non_ASCII_cnt) {
fprintf(Output_File, ", ");
fprint_count(Output_File,
lex_non_ASCII_cnt, "non-ASCII character"
);
}
fprintf(Output_File, "\n");
}
static void
read_file(const char *fname, struct text *txt) {
txt->tx_fname = fname;
txt->tx_pos = 0;
txt->tx_start = Token_Array_Length();
txt->tx_limit = Token_Array_Length();
if (is_new_old_separator(fname)) {
do_separator(txt, fname);
}
else { /* it is a real file */
int ok = read_text(fname, txt);
if (ok && !is_set_option('T')) {
report_file(fname, txt);
}
}
fflush(Output_File);
}
void
Read_Input_Files(int argc, const char *argv[]) {
int n;
Init_Text(argc);
Init_Token_Array();
/* Initially assume all texts to be new */
Number_of_New_Texts = Number_of_Texts;
/* Read the files */
for (n = 0; n < Number_of_Texts; n++) {
/* do one argument/file name */
read_file(argv[n],&Text[n]);
}
/* report total */
int sep_present = (Number_of_Texts != Number_of_New_Texts);
fprintf(Output_File, "Total input: ");
fprint_count(Output_File,
(!sep_present ? Number_of_Texts : Number_of_Texts - 1),
"file"
);
fprintf(Output_File, " (%d new, %d old), ",
Number_of_New_Texts,
(!sep_present ? 0 : Number_of_Texts - Number_of_New_Texts - 1)
);
fprint_count(Output_File, Token_Array_Length() - 1, Token_Name);
fprintf(Output_File, "\n\n");
fflush(Output_File);
}