/
main.c
395 lines (347 loc) · 9.27 KB
/
main.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file_handling.h"
#include "options.h"
/*
Diff en C maison
*/
/***********************************************************
MAIN
***********************************************************/
int main(int argc, char** argv)
{
//check the number of argument, less than 2 is impossible
if (argc < 2)
{
printf("Not enough arguments.");
return 1;
}
/*****************************************************
Initialisation OPTIONS
******************************************************/
int option_i = 0;
int option_b = 0;
int option_E = 0;
int option_v = 0;
int option_h = 0;
int option_w = 0;
int option_N = 0;
int option_t = 0;
//these options must call its functions after string behavior options
int option_s = 0;
int option_q = 0;
int option_y = 0;
//How many options do we have?
int k;
int cpt = 0; //total number of options
for(k=1;k<argc;k++)
{
if(strcmp(argv[k], "-i") == 0 || strcmp(argv[k], "--ignore-case") == 0)
{
option_i = 1;
cpt++;
}
if(strcmp(argv[k], "-b") == 0 || strcmp(argv[k], "--ignore-space-change") == 0)
{
option_b = 1;
cpt++;
}
if(strcmp(argv[k], "-E") == 0 || strcmp(argv[k], "--ignore-tab-expansion") == 0)
{
option_E = 1;
cpt++;
}
if(strcmp(argv[k], "-v") == 0 || strcmp(argv[k], "--version") == 0)
{
option_v = 1; //useless because of return 0;
printf("%s: Version 2012.01.13.1727\n\n",argv[0]);
printf("Written by Antoine Lang-Cavelier\n");
return 0; //end of the programm now
}
if(strcmp(argv[k], "--help") == 0)
{
option_h = 1; //useless because of return 0;
printf("Syntax:\n");
printf("%s [OPTIONS] fichier1 fichier2\n\n",argv[0]);
opt_help(); //we have a function to display the help
return 0; //end of the programm now
}
if(strcmp(argv[k], "-w") == 0 || strcmp(argv[k], "--ignore-all-space") == 0)
{
option_w = 1;
cpt++;
}
if(strcmp(argv[k], "-N") == 0 || strcmp(argv[k], "--new-file") == 0)
{
option_N = 1;
cpt++;
if (argv[2] == NULL)
{
printf("%s: No differences between 2 empty files.\n",argv[0]);
return 0;
}
}
if(strcmp(argv[k], "-t") == 0 || strcmp(argv[k], "--expand-tabs") == 0)
{
option_t = 1;
cpt++;
}
if(strcmp(argv[k], "-s") == 0 || strcmp(argv[k], "--report-identical-files") == 0)
{
option_s = 1;
cpt++;
}
if(strcmp(argv[k], "-q") == 0 || strcmp(argv[k], "--brief") == 0)
{
option_q = 1;
cpt++;
}
if(strcmp(argv[k], "-y") == 0 || strcmp(argv[k], "--side-by-side") == 0)
{
option_y = 1;
cpt++;
}
}
//we increase our arguments by the number of different options selected
//because after that, we will use argv[1] and argv[2]
argv[1] = argv[cpt+1]; //first file
argv[2] = argv[cpt+2]; //second file
/*****************************************************
ENDOF: Initialisation OPTIONS
******************************************************/
if (option_N == 0 && argv[2] == NULL)
{
printf("%s: Missing operand after '%s'.\n",argv[0],argv[1]);
printf("%s: Try `%s --help' for more information.\n",argv[0],argv[0]);
return 0;
}
//we rename our two arguments to avoid type issues (char* vs char**)
char* src1 = argv[1];
char* src2 = argv[2];
/*****************************************************
ALGO DIFF;
******************************************************/
//call of our function nombre_ligne();
int nb1 = nombre_ligne(src1); //ask total lines of src1 and put it in nb1
int nb2 = nombre_ligne(src2); //ask total lines of src2 and put it in nb2
//beginning of the comparison loop in order to print the diff
//call of our function file_to_tab();
char** chaine_tab1 = file_to_tab(src1, nb1); //put our file1 in a tab of string
char** chaine_tab2 = file_to_tab(src2, nb2); //put our file2 in a tab of string
/*****************
Option -i
not case sensitive
*****************/
if (option_i == 1)
{
int i;
for (i=0; i<nb1; i++)
{
chaine_tab1[i] = str_to_lower(chaine_tab1[i]);
}
int j;
for (j=0; j<nb2; j++)
{
chaine_tab2[j] = str_to_lower(chaine_tab2[j]);
}
}
/*****************
ENDOF: Option -i
*****************/
/*****************
Option -b
no multi space
*****************/
if (option_b == 1)
{
int i;
for (i=0; i<nb1; i++)
{
chaine_tab1[i] = str_onespace(chaine_tab1[i]);
}
int j;
for (j=0; j<nb2; j++)
{
chaine_tab2[j] = str_onespace(chaine_tab2[j]);
}
}
/*****************
ENDOF: Option -b
*****************/
/*****************
Option -E
no multi tab
*****************/
if (option_E == 1)
{
int i;
for (i=0; i<nb1; i++)
{
chaine_tab1[i] = str_onetab(chaine_tab1[i]);
}
int j;
for (j=0; j<nb2; j++)
{
chaine_tab2[j] = str_onetab(chaine_tab2[j]);
}
}
/*****************
ENDOF: Option -E
*****************/
/*****************
Option -w
skip blank characters
*****************/
if (option_w == 1)
{
int i;
for (i=0; i<nb1; i++)
{
chaine_tab1[i] = str_ignore_blank(chaine_tab1[i]);
}
int j;
for (j=0; j<nb2; j++)
{
chaine_tab2[j] = str_ignore_blank(chaine_tab2[j]);
}
}
/*****************
ENDOF: Option -w
*****************/
/*****************
Option -t
change tab in space
*****************/
if (option_t == 1)
{
int i;
for (i=0; i<nb1; i++)
{
chaine_tab1[i] = str_tabtospace(chaine_tab1[i]);
}
int j;
for (j=0; j<nb2; j++)
{
chaine_tab2[j] = str_tabtospace(chaine_tab2[j]);
}
}
/*****************
ENDOF: Option -t
*****************/
/*****************
Option -s
report-identical-files
*****************/
if (option_s == 1)
{
if (opt_s(chaine_tab1, nb1, chaine_tab2, nb2) == 0)
{
printf("Files %s and %s are identical.",src1,src2);
free(chaine_tab1);
free(chaine_tab2);
return 0;
}
}
/*****************
ENDOF: Option -s
*****************/
/*****************
Option -q
brief diff
MUST BE AFTER STRING BEHAVIOR OPTIONS
*****************/
if (option_q == 1)
{
if (opt_q(chaine_tab1, nb1, chaine_tab2, nb2) != 0)
{
printf("Files %s and %s differ.", src1, src2);
}
free(chaine_tab1);
free(chaine_tab2);
return 0;
}
/*****************
ENDOF: Option -q
*****************/
/*****************
Option -y
side by side diff
MUST BE AFTER STRING BEHAVIOR OPTIONS
*****************/
if (option_y == 1)
{
printf("Side by side diff of %s with %s:\n\n",src1,src2);
opt_y(chaine_tab1, nb1, chaine_tab2, nb2);
free(chaine_tab1);
free(chaine_tab2);
return 0;
}
/*****************
ENDOF: Option -y
*****************/
printf("Diff of %s with %s:\n\n",src1,src2);
// i is the index of the line we're processing in chaine_tab1.
int i;
// j is the index of the line we're processing in chaine_tab2.
int j = 0;
//Process each line of chaine_tab1 and do our job
for (i=0; i<nb1; i++)
{
//Indicates if a matching line was found in chaine_tab2 (true = 1)
int found = 0;
//Indicates the number of lines we've skipped in chaine_tab2 before finding the matching line.
int skipped = 0;
//we process each line of chaine_tab2 (starting from the j-th one)
//until we either find a matching line or the end of file
while (j<nb2 && found == 0)
{
// Compare both lines
if(strcmp(chaine_tab1[i], chaine_tab2[j]) == 0)
{
found = 1;
}
else
{
skipped++;
}
j++;
}
//Now process the result:
if (found == 1)
{
//Print lines we skipped (from j-skipped to j)
int a;
for (a=j-skipped;a<j;a++)
{
printf("> %s\n", chaine_tab2[a-1]);
}
//Print the matched line
printf("= %s\n", chaine_tab1[i]);
}
else
{
//Only print the line from chaine_tab1, and rollback the
//index j because we'll have to read those lines again.
printf("< %s\n", chaine_tab1[i]);
j = j-skipped;
}
}
//Time to print all lines of chaine_tab2 if file2 have more
//line than file1: these are all new.
while (j < nb2)
{
printf("> %s\n", chaine_tab2[j]);
j++;
}
//deallocation of our mallocs
free(chaine_tab1);
free(chaine_tab2);
/*****************************************************
ENDOF: ALGO DIFF;
******************************************************/
return 0;
}
/***********************************************************
ENDOF: MAIN
***********************************************************/