-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.h
More file actions
513 lines (466 loc) · 14.3 KB
/
options.h
File metadata and controls
513 lines (466 loc) · 14.3 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include <ctype.h>
#define YDISPLAY 32 //lenght maximum of every line
/*
HERE WE HAVE ALL OPTION FUNCTIONS OF OUR PROGRAMM
*/
//int opt_s (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2);
char* str_to_lower(const char* chaine);
char* str_onespace (const char* chaine);
char* str_onetab (const char* chaine);
char* str_ignore_blank(char * chaine);
void opt_help ();
int opt_s (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2);
int opt_q (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2);
void opt_y (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2);
char* str_tabtospace (const char* chaine);
/********************************************************
FUNCTION str_to_lower OPTION -i
*********************************************************/
char* str_to_lower(const char* chaine)
{
char* string = NULL;
if (chaine != NULL)
{
//we allocate a string of same size of we had in chaine
string = malloc ((strlen (chaine) + 1) * sizeof (*string));
int i;
if (string != NULL)
{
//for each character, we lower it and rewrite it in our string
for (i=0;chaine[i];i++)
{
string[i] = tolower(chaine[i]);
}
string[i] = '\0';
}
}
return string;
}
/********************************************************
ENDOF: FUNCTION str_to_lower OPTION -i
*********************************************************/
/********************************************************
FUNCTION str_onespace OPTION -b
*********************************************************/
char* str_onespace (const char* chaine)
{
char* onespace = NULL;
if (chaine != NULL)
{
//allocation of the same size of memory of 'chaine'
onespace = malloc ((strlen (chaine) + 1) * sizeof (*onespace));
if (onespace != NULL)
{
int i;
int j;
int space = 0; //number of space
//for each character of our string
for (i=0,j=0;chaine[i];i++)
{
//we test if every char is a space, if it is a space, we test the
//value of 'space' which indicates if the previous char was a space too
//if it wasn't a space, we put 1 in 'space'
if (chaine[i] == ' ') // if the car is a space
{
if (space == 0)
{
onespace[j] = chaine[i];
space = 1;
j++;
//if space = 0, we copy this one and set space = 1
//so if space = 1, we just skip the copy of i in j
}
}
else //if the car is not a space, we jsute copy it
{
onespace[j] = chaine[i];
space = 0;
j++;
}
}
onespace[j] = '\0'; //last char of our string must be a \0
}
else //test our malloc
{
fprintf (stderr, "Allocation impossible\n");
exit (EXIT_FAILURE);
}
}
return onespace; //we return the string tweaked
}
/********************************************************
ENDOF: FUNCTION str_onespace OPTION -b
*********************************************************/
/********************************************************
FUNCTION str_onetab OPTION -E
*********************************************************/
char* str_onetab (const char* chaine)
{
char* onetab = NULL;
if (chaine != NULL)
{
onetab = malloc ((strlen (chaine) + 1) * sizeof (*onetab));
if (onetab != NULL)
{
int i;
int j;
int tabulation = 0;
for (i=0,j=0;chaine[i];i++)
{
if (chaine[i] == '\t')
{
if (tabulation == 0)
{
onetab[j] = chaine[i];
tabulation = 1;
j++;
}
}
else
{
onetab[j] = chaine[i];
tabulation = 0;
j++;
}
}
onetab[j] = '\0';
}
else //if our malloc is NULL, it's an error
{
fprintf (stderr, "Allocation impossible\n");
exit (EXIT_FAILURE);
}
}
return onetab;
}
/********************************************************
ENDOF: FUNCTION str_onetab OPTION -E
*********************************************************/
/********************************************************
FUNCTION str_ignore_blank OPTION -w
*********************************************************/
char* str_ignore_blank(char * chaine)
{
char * result = NULL;
if (chaine != NULL)
{
int length;
length = strlen(chaine);
result = malloc((length + 1) * sizeof(char));
if (result != NULL)
{
int i;
int j = 0;
for(i = 0; i != length; i++)
{
if (chaine[i] != ' ' && chaine[i] != '\t')
{
result[j] = chaine[i];
j++;
}
}
result[j] = '\0';
free(chaine);
}
}
return result;
}
/********************************************************
ENDOF: FUNCTION str_ignore_blank OPTION -w
*********************************************************/
/********************************************************
FUNCTION opt_help OPTION --help
*********************************************************/
void opt_help ()
{
printf("Options available:\n");
printf(" -b --ignore-space-change \tIgnore changes in the amount of white space.\n");
printf(" -i --ignore-case \t\tIgnore case differences in file contents.\n");
printf(" -E --ignore-tab-expansion \tIgnore changes due to tab expansion.\n");
printf(" -N --new-file \t\tTreat absent files as empty.\n");
printf(" -v --version \t\tOutput version info.\n");
printf(" -w --ignore-all-space \tIgnore all white space.\n");
printf(" -t --expand-tabs \t\tExpand tabs to spaces in output.\n");
printf(" -q --brief \t\t\tOutput only whether files differ.\n");
printf(" -y --side-by-side \t\tOutput in two columns.\n");
printf(" -s --report-identical-files \tReport when two files are the same.\n");
printf(" --help \t\t\tOutput this help.\n");
}
/********************************************************
ENDOF: FUNCTION opt_help OPTION --help
*********************************************************/
/********************************************************
FUNCTION opt_q OPTION -q
*********************************************************/
int opt_q (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2)
{
int briefresult = 0;
// 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++)
{
briefresult = 1;
}
}
else
{
//Only print the line from chaine_tab1, and rollback the
//index j because we'll have to read those lines again.
j = j-skipped;
briefresult = 1;
}
}
//Time to print all lines of chaine_tab2 if file2 have more
//line than file1: these are all new.
while (j < nb2)
{
j++;
briefresult = 1;
}
return briefresult;
}
/********************************************************
ENDOF: FUNCTION opt_q OPTION -q
*********************************************************/
/********************************************************
FUNCTION opt_s OPTION -s
*********************************************************/
int opt_s (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2)
{
int same = 0;
// 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++)
{
same = 1;
}
}
else
{
//Only print the line from chaine_tab1, and rollback the
//index j because we'll have to read those lines again.
j = j-skipped;
same = 1;
}
}
//Time to print all lines of chaine_tab2 if file2 have more
//line than file1: these are all new.
while (j < nb2)
{
j++;
same = 1;
}
return same;
}
/********************************************************
ENDOF: FUNCTION opt_s OPTION -s
*********************************************************/
/********************************************************
FUNCTION opt_y OPTION -y
*********************************************************/
void opt_y (char** chaine_tab1, int nb1, char** chaine_tab2, int nb2)
{
char * buffer = NULL;
buffer = (char*)malloc(YDISPLAY+1*sizeof(char));
if (buffer == NULL)
{
fprintf(stderr,"Allocation impossible\n");
exit(EXIT_FAILURE);
}
// 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++)
{
strncpy (buffer, chaine_tab2[a-1], YDISPLAY);
printf("\t\t\t\t> %s\n", buffer);
}
//Print the matched line
strncpy (buffer, chaine_tab1[i], YDISPLAY);
if (strlen(buffer) < YDISPLAY)
{
int b;
for (b=strlen(buffer);b < YDISPLAY;b++)
{
buffer[b] = ' ';
}
buffer[b]='\0';
}
printf("%s= %s\n", buffer, buffer);
}
else
{
//Only print the line from chaine_tab1, and rollback the
//index j because we'll have to read those lines again.
strncpy (buffer, chaine_tab1[i], YDISPLAY);
if (strlen(buffer) < YDISPLAY)
{
int b;
for (b=strlen(buffer);b < YDISPLAY;b++)
{
buffer[b] = ' ';
}
buffer[b]='\0';
}
printf("%s<\n", buffer);
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)
{
strncpy (buffer, chaine_tab2[j], YDISPLAY);
if (strlen(buffer) < YDISPLAY)
{
int b;
for (b=strlen(buffer);b < YDISPLAY;b++)
{
buffer[b] = ' ';
}
buffer[b]='\0';
}
printf("\t\t\t\t> %s\n", buffer);
j++;
}
free(buffer);
}
/********************************************************
ENDOF: FUNCTION opt_y OPTION -y
*********************************************************/
/********************************************************
FUNCTION str_tabtospace OPTION -t
*********************************************************/
char* str_tabtospace (const char* chaine)
{
int i;
int k; //number of tabulations
for (i=0,k=0;chaine[i];i++)
{
if (chaine[i] == '\t')
{
k++;
}
}
char* tabtospace = NULL;
if (chaine != NULL)
{
tabtospace = malloc ((strlen (chaine) + 1 + k*3) * sizeof (*tabtospace));
if (tabtospace != NULL)
{
int j;
for (i=0,j=0;chaine[i];i++)
{
if (chaine[i] == '\t')
{
tabtospace[j] = ' ';
tabtospace[j+1] = ' ';
tabtospace[j+2] = ' ';
tabtospace[j+3] = ' ';
j=j+4;
}
else
{
tabtospace[j] = chaine[i];
j++;
}
}
tabtospace[j] = '\0';
}
else //if our malloc is NULL, it's an error
{
fprintf (stderr, "Allocation impossible\n");
exit (EXIT_FAILURE);
}
}
return tabtospace;
}
/********************************************************
ENDOF: FUNCTION str_tabtospace OPTION -t
*********************************************************/