-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtxtcmp.c
More file actions
322 lines (277 loc) · 6.76 KB
/
txtcmp.c
File metadata and controls
322 lines (277 loc) · 6.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#define PROG "txtcmp"
#define VERSION "0.2.2"
#ifndef TXTCMP_LENGTH_GUESS
# define TXTCMP_LENGTH_GUESS 100
#endif
#if TXTCMP_LENGTH_GUESS < 1
# error "TXTCMP_LENGTH_GUESS should be >= 1"
#endif
#ifndef TXTCMP_REALLOC_FACTOR
# define TXTCMP_REALLOC_FACTOR 2
#endif
#if TXTCMP_REALLOC_FACTOR < 2
# error "TXTCMP_REALLOC_FACTOR should be >= 2"
#endif
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
typedef unsigned long hash_t;
// Options set by command line flags.
static char opt_normalize = 0;
static char opt_trim = 0;
static char opt_ignore_blanks = 0;
static char opt_ignore_whitespace = 0;
static void
xperror(const char *msg)
{
if(msg == NULL) {
perror(PROG);
}
else {
fprintf(stderr, PROG ": ");
perror(msg);
}
}
static void print_usage(FILE *fp, const char *arg0)
{
fprintf(fp,
"Usage: %s [options] file1 file2...\n"
"Options:\n"
" -b Ignore blank lines\n"
" -s Ignore whitespace altogether\n"
" -t Trim whitespace from ends of lines\n"
" -n Normalize LCS lengths\n"
" -h Print this message\n"
" -v Print the version\n"
, arg0);
}
static hash_t
hash_string(const char* str)
{
hash_t hash = 0;
int c;
// sdbm hash function
while((c = *str++)) {
hash = c + (hash << 6) + (hash << 16) - hash;
}
// We use a hash of zero to indicate EOF.
hash = (hash == 0) ? 1 : hash;
return hash;
}
static void
hash_file(const char *filename, FILE *fp, hash_t **buffer)
{
char *line = NULL, *linestart = NULL;
size_t linecap = 0, linecount = 0, lineguess = TXTCMP_LENGTH_GUESS;
ssize_t linelen;
if((*buffer = malloc(lineguess * sizeof(**buffer))) == NULL) {
xperror("malloc()");
exit(EXIT_FAILURE);
}
while((linelen = getline(&line, &linecap, fp)) > 0) {
++linecount;
linestart = line;
// Trim whitespace
if(opt_trim) {
while(isspace(*linestart)) {
++linestart;
--linelen;
}
while(isspace(linestart[linelen - 1])) {
--linelen;
}
linestart[linelen] = '\0';
}
// Strip all whitespace in-place.
if(opt_ignore_whitespace) {
size_t i, imax = linelen;
char *insert = linestart;
for(i = 0; i < imax; ++i) {
if(!isspace(linestart[i])) {
*insert = linestart[i];
++insert;
}
else {
--linelen;
}
}
linestart[linelen] = '\0';
}
// Ignore the line if it is blank
if(opt_ignore_blanks && (
(linestart[0] == '\r' && linestart[1] == '\n') ||
linestart[0] == '\n' ||
linestart[0] == '\0'
)
) {
--linecount;
continue;
}
// Rellocate memory if we have underestimated the file line count.
if(linecount > lineguess) {
lineguess *= TXTCMP_REALLOC_FACTOR;
if((*buffer = realloc(*buffer, lineguess * sizeof(**buffer))) == NULL) {
xperror("realloc()");
exit(EXIT_FAILURE);
}
}
(*buffer)[linecount-1] = hash_string(linestart);
}
if(ferror(fp)) {
xperror(filename);
exit(EXIT_FAILURE);
}
// Shrink the buffer down to the actual file length.
if((*buffer = realloc(*buffer, (linecount + 1) * sizeof(**buffer))) == NULL) {
xperror("realloc()");
exit(EXIT_FAILURE);
}
// "Terminate" the sequence of hashes with zero.
(*buffer)[linecount] = 0;
if(line != NULL) {
free(line);
}
}
static void
hash_files(const char *const* filenames, hash_t **hashes, size_t length)
{
size_t i;
FILE *fp = NULL;
for(i = 0; i < length; ++i) {
if((fp = fopen(filenames[i], "r")) == NULL) {
xperror(filenames[i]) ;
exit(EXIT_FAILURE);
}
hash_file(filenames[i], fp, &hashes[i]);
fclose(fp);
}
}
unsigned long
lcs_length(hash_t *buf1, size_t buflen1, hash_t *buf2, size_t buflen2)
{
size_t i, j;
unsigned long *swap, *this_row, *last_row, lookup1, lookup2, result;
hash_t h1, h2;
if(buflen1 == 0 || buflen2 == 0) {
return 0;
}
// Make sure buf2 is the smaller buffer (to allocate less memory).
if(buflen2 > buflen1) {
size_t tmplen;
hash_t *tmpbuf;
tmpbuf = buf1;
tmplen = buflen1;
buf1 = buf2;
buflen1 = buflen2;
buf2 = tmpbuf;
buflen2 = tmplen;
}
if((this_row = calloc(buflen2 + 1, sizeof(*this_row))) == NULL) {
xperror("calloc()");
exit(EXIT_FAILURE);
}
if((last_row = calloc(buflen2 + 1, sizeof(*last_row))) == NULL) {
xperror("calloc()");
exit(EXIT_FAILURE);
}
for(i = 1; i < buflen1 + 1; i++) {
h1 = buf1[i-1];
for(j = 1; j < buflen2 + 1; j++) {
h2 = buf2[j-1];
if(h1 == h2) {
this_row[j] = 1 + last_row[j-1];
}
else {
lookup1 = last_row[j];
lookup2 = this_row[j-1];
this_row[j] = (lookup1 > lookup2) ? lookup1 : lookup2;
}
}
swap = this_row;
this_row = last_row;
last_row = swap;
}
result = last_row[buflen2];
free(this_row);
free(last_row);
return result;
}
size_t hashlen(hash_t *hash) {
size_t i = 0;
while(hash[i] != 0) {
++i;
}
return i;
}
int main(int argc, char **argv)
{
size_t i, j, len1, len2;
unsigned long length;
int ch, count;
hash_t **hashes;
char **filenames;
while((ch = getopt(argc, argv, "bstnhv")) != -1) {
switch(ch) {
case 'v':
printf(PROG " " VERSION "\n");
exit(EXIT_SUCCESS);
case 'h':
print_usage(stdout, argv[0]);
exit(EXIT_SUCCESS);
case 'b':
opt_ignore_blanks = 1;
continue;
case 's':
opt_ignore_whitespace = 1;
continue;
case 'n':
opt_normalize = 1;
continue;
case 't':
opt_trim = 1;
continue;
case '?':
default:
print_usage(stderr, argv[0]);
exit(EXIT_FAILURE);
}
}
count = argc - optind;
if(count < 2) {
fprintf(stderr, "%s: need at least two filenames\n", argv[0]);
print_usage(stderr, argv[0]);
exit(EXIT_FAILURE);
}
if((hashes = malloc(count * sizeof(*hashes))) == NULL) {
xperror("malloc()");
exit(EXIT_FAILURE);
}
filenames = argv + optind;
hash_files((const char *const*) filenames, hashes, count);
for(i = 0; i < count; i++) {
len1 = hashlen(hashes[i]);
for(j = i + 1; j < count; j++) {
len2 = hashlen(hashes[j]);
length = lcs_length(hashes[i], len1, hashes[j], len2);
if(!opt_normalize) {
printf("%lu %s %s\n", length, filenames[i], filenames[j]);
}
else {
float rank = 0.f;
size_t minlen;
minlen = (len1 < len2) ? len1 : len2;
if(minlen > 0) {
rank = length / ((float) minlen);
}
printf("%.3f %s %s\n", rank, filenames[i], filenames[j]);
}
}
}
for(i = 0; i < count; i++) {
free(hashes[i]);
}
free(hashes);
exit(EXIT_SUCCESS);
}