-
Notifications
You must be signed in to change notification settings - Fork 97
/
inicomp.c
1058 lines (931 loc) · 42.9 KB
/
inicomp.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
* *
* Filename inicomp.c *
* *
* Description Compare two .INI files *
* *
* Notes In July 2010, I started a redesign of the program. *
* The goal was to gain performance, by replacing the old *
* linked lists with self-balancing binary trees. *
* The performance of linked lists was good when comparing *
* small .ini files, like the old Windows 9x win.ini and *
* system.ini. But it was becoming unacceptably slow when *
* comparing tens of MB-long exported registry trees. *
* The project stalled for several years, until I completed *
* it while traveling back from Christmas 2016 vacations. *
* *
* To do: *
* - Convert all file encodings to UTF8 data. *
* - Use "natural" sort for keys, not ASCII sort. *
* (natural = consider numbers as 1-character values, *
* no matter how many actual characters are present. *
* Ex: "12" < "75" < "128" ) *
* - Add support for multiple homonym sections. *
* (Non-standard, but used in some real cases.) *
* *
* History *
* 1993-09-28 JFL Created this program. *
* 1995-09-19 JFL Adapted for multiple OS target. *
* Changed version to 1.1, although the feature are the same.*
* 2010-06-08 JFL Added options -d and -v. *
* Added progress feedback while processing huge files. *
* 2010-07-09 JFL Started rewrite using dict.h. (Program does not build) *
* 2012-10-18 JFL Added my name in the help. *
* 2017-01-02 JFL Finished restructuration using dict.h. *
* Added option -V. *
* 2017-01-04 JFL Use case-independant NewIDict(). *
* Made this a UTF-8 app. supporting non-ASCII file names. *
* Added support for Linux. *
* 2017-01-05 JFL Added support for quoted value names, that may contain '='.
* Added support for \ continuation lines. *
* 2017-01-06 JFL Quoted value strings may also continue on multiple lines. *
* Use multimaps instead of dicts (aka. maps) to avoid *
* issues when encountering duplicate names. (This happens!) *
* 2017-01-07 JFL Count line ending with \r\r\n as two lines. *
* Version 2.0. *
* 2019-04-19 JFL Use the version strings from the new stversion.h. V.2.0.1.*
* 2019-06-12 JFL Added PROGRAM_DESCRIPTION definition. Version 2.0.2. *
* 2020-02-17 JFL Added option -f to allow free lines without an =value. *
* Removed the incorrect code handling homonym sections. The *
* standard is to merge multiple parts into 1 single section.*
* Version 2.1. *
* 2020-04-20 JFL Added support for MacOS. Version 2.2. *
* 2022-10-19 JFL Moved IsSwitch() to SysLib. Version 2.2.1. *
* *
* © Copyright 2016 Hewlett Packard Enterprise Development LP *
* Licensed under the Apache 2.0 license - www.apache.org/licenses/LICENSE-2.0 *
\*****************************************************************************/
#define PROGRAM_DESCRIPTION "Compare .ini files, section by section, and item by item"
#define PROGRAM_NAME "inicomp"
#define PROGRAM_VERSION "2.2.1"
#define PROGRAM_DATE "2022-10-19"
#define _CRT_SECURE_NO_WARNINGS /* Prevent warnings about using sprintf and sscanf */
#define _UTF8_SOURCE /* Enable MsvcLibX support for file names with Unicode characters */
#define _GNU_SOURCE /* Else Linux does not define strdup() in string.h */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <search.h>
#include <ctype.h>
/* SysToolsLib include files */
#include "debugm.h" /* SysToolsLib debug macros. Include first. */
#include "mainutil.h" /* SysLib helper routines for main() */
#include "stversion.h" /* SysToolsLib version strings. Include last. */
DEBUG_GLOBALS /* Define global variables used by debugging macros. (Necessary for Unix builds) */
#include "dict.h"
DICT_DEFINE_PROCS();
/************************* OS/2-specific definitions *************************/
#ifdef _OS2 /* To be defined on the command line for the OS/2 version */
#define PATHNAME_SIZE 260 /* FILENAME_MAX incorrect in stdio.h */
#endif
/************************ Win32-specific definitions *************************/
#ifdef _WIN32 /* Automatically defined when targeting a Win32 applic. */
#define PATHNAME_SIZE FILENAME_MAX
/* These are standard routines, but Microsoft thinks not */
#define strdup _strdup
/* These are non standard routines, but the leading _ is annoying */
#define strlwr _strlwr
#define stricmp _stricmp
#endif
/************************ MS-DOS-specific definitions ************************/
#ifdef _MSDOS /* Automatically defined when targeting an MS-DOS app. */
#define PATHNAME_SIZE FILENAME_MAX
#pragma warning(disable:4505) /* Ignore the "unreferenced local function has been removed" warning */
#endif
/************************* Unix-specific definitions *************************/
#if defined(__unix__) || defined(__MACH__) /* Automatically defined when targeting Unix or Mach apps. */
#define _UNIX
#define PATHNAME_SIZE FILENAME_MAX
#define stricmp strcasecmp
#endif /* __unix__ */
/******************************* Any other OS ********************************/
#ifndef PATHNAME_SIZE
#error "Unsupported OS"
#endif
/********************** End of OS-specific definitions ***********************/
typedef enum {EQUAL, FILE1, FILE2} outstate;
/* Global variables */
int verbose = FALSE;
int compBlanks = FALSE;
int ignoreCase = TRUE;
int allowNoValue = FALSE; /* TRUE = Allow non-standard data lines with a free string, without an =value */
/* Define multimap dictionaries */
int cmpvalue(void *p1, void *p2) {
return strcmp(p1, p2);
}
int cmpivalue(void *p1, void *p2) {
return _stricmp(p1, p2);
}
dict_t *NewIniDict(void) { /* Duplicate keys not allowed */
if (ignoreCase)
return NewIDict();
else
return NewDict();
}
dict_t *NewIniMMap(void) { /* Duplicate keys allowed */
if (ignoreCase)
return NewIMMap(cmpivalue);
else
return NewMMap(cmpvalue);
}
#define NewIniSectionDict() NewIniDict() /* Duplicate keys not allowed */
#define NewIniValueDict() NewIniMMap() /* Duplicate keys allowed */
/* Function prototypes */
char *processFile(char *argname, dict_t *sections);
char *trimLeft(char *s);
char *trimRight(char *s);
int compItem(dictnode *i1, dictnode *i2);
int compStringNB(const char *s1, const char *s2);
int compString(const char *s1, const char *s2);
int compare(char *name1, dict_t *tree1, char *name2, dict_t *tree2);
void newOutState(outstate *pold, outstate new, char *name1, char *name2);
void *printSectCB(char *pszName, void *pValue, void *pRef);
void *printSectNameCB(char *pszName, void *pValue, void *pRef);
void printSectName(char *name);
void *printItemCB(char *pszName, void *pValue, void *pRef);
void *printItem(dictnode *pi);
void outOfMem(void);
void usage(void);
/*----------------------------------------------------------------------------+
| |
| Function: main |
| |
| Description: Process the command line, and have the job done |
| |
| Input: int argc Number of command line arguments |
| char *argv[] Arguments |
| |
| Output: None |
| |
| Updates: |
| 1993-09-29 JFL Initial implementation |
| |
+----------------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
int i;
char *f1arg = NULL; /* File 1 name provided */
char *f2arg = NULL; /* File 2 name provided */
char *name1; /* File 1 name found */
char *name2; /* File 2 name found */
dict_t *dict1; /* File 1 sections dictionary */
dict_t *dict2; /* File 2 sections dictionary */
for (i=1; i<argc; i++) {
char *arg = argv[i];
if (IsSwitch(arg)) { /* It's a switch */
char *opt = arg + 1;
if ( streq(opt, "help")
|| streq(opt, "h")
|| streq(opt, "?")) {
usage();
}
if (streq(opt, "b")) {
compBlanks = TRUE;
continue;
}
if (streq(opt, "c")) {
ignoreCase = FALSE;
continue;
}
if (streq(opt, "C")) {
ignoreCase = TRUE;
continue;
}
DEBUG_CODE(
if ( streq(opt, "debug")
|| streq(opt, "d")) {
DEBUG_ON();
verbose = TRUE;
continue;
}
)
if (streq(opt, "f")) {
allowNoValue = TRUE;
continue;
}
if (streq(opt, "F")) {
allowNoValue = FALSE;
continue;
}
if ( streq(opt, "verbose")
|| streq(opt, "v")) {
verbose = TRUE;
continue;
}
if ( streq(opt, "version")
|| streq(opt, "V")) {
puts(DETAILED_VERSION);
exit(0);
}
printf("Unrecognized switch %s. Ignored.\n", arg);
continue;
}
if (!f1arg) {
f1arg = arg;
continue;
}
if (!f2arg) {
f2arg = arg;
continue;
}
printf("Unexpected argument: %s\nIgnored.\n", arg);
break; /* Ignore other arguments */
}
if (!f1arg) {
usage();
}
dict1 = NewIniSectionDict(); /* File 1 sections dictionary */
dict2 = NewIniSectionDict(); /* File 2 sections dictionary */
/* Sort files */
name1 = processFile(f1arg, dict1);
name2 = processFile(f2arg, dict2);
DEBUG_CODE(
/* Print all data gathered so far */
printf("***************************************************************\n");
ForeachDictValue(dict1, printSectCB, NULL);
printf("***************************************************************\n");
ForeachDictValue(dict2, printSectCB, NULL);
printf("***************************************************************\n");
)
/* Display differences */
compare(name1, dict1, name2, dict2);
return 0;
}
/*---------------------------------------------------------------------------*\
| |
| Function getLine |
| |
| Description Read a line, growing the buffer, if needed to get it all |
| |
| Input char **pBuf Address of output buffer pointer |
| size_t *pBufSize Address of output buffer size |
| FILE *f Input file handle |
| size_t offset Offset to write in the output buffer |
| long *pNLines Address of optional line counter |
| |
| Output Final address of the line read, or NULL if error. |
| |
| History |
| 2017-01-06 JFL Initial implementation |
| |
\*---------------------------------------------------------------------------*/
/* Read a line, growing the buffer if needed to get the full line */
char *getLine(char **pBuf, size_t *pBufSize, FILE *f, size_t offset, long *pNLines) {
char *line = *pBuf + offset;
size_t l;
long nl = 0;
if (!fgets(line, (int)(*pBufSize - offset), f)) return NULL;
l = strlen(line);
if (pNLines) {
nl = *pNLines += 1;
if ((verbose) && ((nl % 10000) == 0)) {
fprintf(stderr, "Line %ld: %s", nl, line);
if (l && (line[l-1] != '\n')) fprintf(stderr, "\n");
}
}
/* Check if the buffer is too small, and needs to be extended to get the full line */
while (((offset+l) == (*pBufSize-1)) && (line[l-1] != '\n')) {
DEBUG_PRINTF(("Line %ld overflowed. Expanding buffer to %ld bytes: %s\n", nl, (unsigned long)*pBufSize*2, line));
*pBuf = realloc(*pBuf, *pBufSize *= 2); /* Double its size, to avoid doing this too often */
if (!*pBuf) outOfMem();
line = *pBuf + offset;
if (!fgets(line+l, (int)(*pBufSize-(offset+l)), f)) break;
l += strlen(line+l);
}
/* When lines end with \r\r\n, most editors (but not Notepad) count that as extra lines */
if (pNLines) {
size_t i = 1;
if ((i <= l) && (line[l-i] == '\n')) i += 1; /* Skip the final \n */
if ((i <= l) && (line[l-i] == '\r')) i += 1; /* Skip the normal \r before that \n */
for ( ; (i <= l) && (line[l-i] == '\r'); i++) *pNLines += 1; /* If there are other \r, count extra lines */
}
return line;
}
/*----------------------------------------------------------------------------+
| |
| Function processFile |
| |
| Description Digest a .INI file. Build sorted lists of sections & items|
| |
| Input char *argname File name received from the command line |
| dict_t *sections Dictionary of all sections |
| |
| Output Pointer to the actual name of the .INI file analysed |
| |
| History |
| 1993-09-29 JFL Initial implementation |
| 2016-01-05 JFL Added support for quoted value names, that may contain '='.
| Added support for continuation lines. |
| |
+----------------------------------------------------------------------------*/
#define LINESIZE 256 /* Initial maximum line size. Will grow if needed */
/* Some registry paths are longer than 256 characters */
/* Must not be too large, else the small DOS version will fail to work */
char *processFile(char *argname, dict_t *sections) {
size_t lineSize = LINESIZE;
char *line;
long nl;
char *pc;
char *pc2;
FILE *f = NULL;
char *fname;
dict_t *items;
dict_t *items0;
size_t l;
int iRegEdit = 0; /* REGEDIT .reg file version */
char bom[4];
char *pszEncoding = "Windows";
line = malloc(lineSize);
if (!line) outOfMem();
/* Open file */
strcpy(line, argname);
if ( ((pc = strrchr(line, '.')) == NULL) || (strchr(pc, '\\') != NULL) ) {
/* If no dot found or there's a backslash afterwards */
strcat(line, ".ini");
}
fname = strdup(line);
f = fopen(fname, "rb");
if (!f) {
printf("Can't open file %s.\n", fname);
exit(1);
}
DEBUG_PRINTF(("\n\n"));
if (verbose) fprintf(stderr, "Reading %s\n", fname);
/* Check the encoding */
/* TO DO: Use that information to convert the input data to UTF8 */
l = fread(bom, 1, sizeof(bom), f);
if (!strncmp(bom, "\xEF\xBB\xBF", 3)) {
pszEncoding = "UTF8";
fseek(f, 3L, SEEK_SET);
} else if ( !strncmp(bom, "\xFF\xFE", 2)
|| ((l >= 4) && !bom[1] && !bom[3])) {
pszEncoding = "UTF16";
bad_encoding:
fprintf(stderr, "Error: File %s is encoded as %s. Please convert it to ANSI or UTF8 first.\n", fname, pszEncoding);
exit(1);
} else if ( !strncmp(bom, "\xFE\xFF", 2)
|| ((l >= 4) && !bom[0] && !bom[2])) {
pszEncoding = "UTF16BE";
goto bad_encoding;
} else { /* Assume this is in the default Windows encoding */
rewind(f);
}
/* Read it & classify lines */
nl = 0;
items0 = items = NewIniValueDict();
NewDictValue(sections, "", items); /* The initial unnamed section */
while (getLine(&line, &lineSize, f, 0, &nl)) {
l = strlen(line);
/* Check if the line ends with an \, showing that there's a continuation line */
check_if_continuation_line:
if (l && line[l-1] == '\n') line[--l] = '\0'; /* Trim the final \n */
while (l && line[l-1] == '\r') {line[--l] = '\0';} /* Trim all \r before that \n */
if (l && line[l-1] == '\\') { /* There is a continuation line */
size_t i;
line[--l] = '\0'; /* Trim the final \ */
getLine(&line, &lineSize, f, l, &nl);
i = l;
while (line[l] == ' ') {
for (i = l; (line[i] = line[i+1]) != '\0'; i += 1) ; /* Move the line ahead by 1 char */
}
l = i; /* Record the new line length */
goto check_if_continuation_line;
}
trimRight(line); /* Remove trailing blanks and \n */
pc = trimLeft(line); /* Skip blanks */
if (!*pc || (*pc == ';')) continue; /* Ignore blank lines & comments */
DEBUG_PRINTF(("Line %lu %s\n", nl, line));
if (*pc == '[') { /* If it's a section */
pc += 1; /* Skip the '[' */
pc = trimLeft(pc); /* Skip blanks */
pc2 = strrchr(pc, ']');
if (!pc2) {
fprintf(stderr, "Error in file %s line %ld: Missing end of section name:\n%s\n", fname, nl, line);
continue; /* If no ], try next line */
}
*(pc2) = '\0';
trimRight(pc);
items = NewIniValueDict();
NewDictValue(sections, pc, items);
continue;
}
/* Else it's an item line */
if (*pc == '"') { /* It's a quoted name */
pc2 = pc += 1; /* Skip the opening quote */
search_end_of_quoted_name:
for ( ; *pc2 && (*pc2 != '"'); pc2 += 1) { /* Search the closing quote */
if (*pc2 == '\\') pc2 += 1; /* Skip any escaped character. Ex: \" or \\ */
}
if (*pc2 == '"') {
*pc2 = '\0'; /* Remove the closing quote */
pc2 = strchr(pc2+1, '=');
} else { /* The quoted name continues on next line (rare, but happens) */
char *line0 = line;
*(pc2++) = '\\'; /* Record an escaped new line */
*(pc2++) = '\n';
*pc2 = '\0';
getLine(&line, &lineSize, f, pc2-line, &nl);
if (line != line0) {
pc += (line-line0);
pc2 += (line-line0);
}
goto search_end_of_quoted_name;
}
} else { /* Unquoted name */
pc2 = strchr(pc, '=');
if (pc2) *pc2 = '\0'; /* Remove the equal sign */
trimRight(pc);
}
if (!pc2) {
if (allowNoValue) { /* If we allow non-standard .ini files with value-less lines */
NewDictValue(items, pc, NULL); /* Enter them without a value */
continue;
}
if ( (items == items0)
&& ( sscanf(pc, "Windows Registry Editor Version %d", &iRegEdit)
|| sscanf(pc, "REGEDIT%d", &iRegEdit))
) { /* regedit .reg files header. Version varies. */
NewDictValue(items, pc, NULL); /* Enter it without a value */
continue;
}
fprintf(stderr, "Error in file %s line %ld: Unexpected (continuation?) line:\n%s\n", fname, nl, line);
continue;
}
pc2 = trimLeft(pc2+1);
if (iRegEdit && (*pc2 == '"')) { /* It's a quoted value */
char *pc3;
pc3 = pc2 += 1; /* Skip the opening quote */
search_end_of_quoted_value:
for ( ; *pc3 && (*pc3 != '"'); pc3 += 1) { /* Search the closing quote */
if (*pc3 == '\\') pc3 += 1; /* Skip any escaped character. Ex: \" or \\ */
}
if (*pc3 == '"') { /* We reached the end of string */
*pc3 = '\0'; /* Remove the closing quote */
} else { /* The string continues on next line */
char *line0 = line;
if (*(pc3-1) != '\n') { /* The first line had its \n trimmed above */
#if defined(_MSDOS) || defined(_WIN32)
*(pc3++) = '\r';
#endif
*(pc3++) = '\n';
*pc3 = '\0';
}
getLine(&line, &lineSize, f, pc3-line, &nl);
if (line != line0) {
pc += (line-line0);
pc2 += (line-line0);
pc3 += (line-line0);
}
goto search_end_of_quoted_value;
}
}
NewDictValue(items, pc, strdup(pc2));
}
/* Cleanup */
fclose(f);
free(line);
return fname;
}
/*----------------------------------------------------------------------------+
| |
| Function: trim |
| |
| Description: Remove spaces from the ends of a string |
| |
| Input: char *s String to trim |
| |
| Output: The string address, which may have shifted a bit. |
| |
| Notes: |
| |
| History: |
| 2010-07-10 JFL Initial implementation |
| 2017-01-02 JFL Fixed trimRight. |
| |
+----------------------------------------------------------------------------*/
char *trimLeft(char *s) {
while (isspace(*s)) s++;
return s;
}
char *trimRight(char *s) {
size_t l = strlen(s);
for ( ; (l--) && isspace(s[l]); ) {
s[l] = '\0';
}
return s;
}
/*----------------------------------------------------------------------------+
| |
| Function compItem |
| |
| Description Compare two item structures (actually pointers to ...) |
| |
| Input dictnode *i1 Structure 1 |
| dictnode *i2 Structure 2 |
| |
| Output 0 if *i1 = *i2 |
| <0 if *i1 < *i2 |
| >0 if *i1 > *i2 |
| |
| History |
| 1993-09-29 JFL Initial implementation |
| 2017-01-01 JFL Rewritten to handle dictnode items. |
| |
+----------------------------------------------------------------------------*/
int compItem(dictnode *i1, dictnode *i2)
{
int dif;
dif = compString(i1->pszKey, i2->pszKey); /* Compare names */
if (dif) return dif;
if (!i1->pData && !i2->pData) return 0;
if (!i1->pData && i2->pData) return -1;
if ( i1->pData && !i2->pData) return 1;
return compStringNB(i1->pData, i2->pData);
}
/*----------------------------------------------------------------------------+
| |
| Function compStringNB |
| |
| Description Compare two strings, possibly ignoring blanks and/or case |
| |
| Input char *s1 String 1 |
| char *s2 String 2 |
| |
| Output 0 if string1 = string2 |
| <0 if string1 < string2 |
| >0 if string1 > string2 |
| |
| History |
| 1993-09-29 JFL Initial implementation |
| 2017-01-05 JFL Removed the dependancy on the LINESIZE constant. |
| |
+----------------------------------------------------------------------------*/
int compStringNB(const char *s1, const char *s2)
{
const char *p1;
char *p2;
char c;
if (compBlanks)
{
return compString(s1, s2);
}
else
{
char *line1, *line2;
int dif;
line1 = malloc(strlen(s1) + 1);
if (!line1) outOfMem();
line2 = malloc(strlen(s2) + 1);
if (!line2) outOfMem();
for (p1=s1, p2=line1; (c=*p1) != 0; p1++) if (c != ' ') *(p2++) = c;
*p2 = '\0';
for (p1=s2, p2=line2; (c=*p1) != 0; p1++) if (c != ' ') *(p2++) = c;
*p2 = '\0';
dif = compString(line1, line2);
free(line1);
free(line2);
return dif;
}
}
/*----------------------------------------------------------------------------+
| |
| Function: compString |
| |
| Description: Compare two strings, possibly ignoring case |
| |
| Input: char *s1 String 1 |
| char *s2 String 2 |
| |
| Output: 0 if string1 = string2 |
| <0 if string1 < string2 |
| >0 if string1 > string2 |
| |
| Updates: |
| 1993-09-29 JFL Initial implementation |
| |
+----------------------------------------------------------------------------*/
int compString(const char *s1, const char *s2)
{
if (ignoreCase)
return stricmp(s1, s2);
else
return strcmp(s1, s2);
}
/*----------------------------------------------------------------------------+
| |
| Function compare |
| |
| Description Compare and display two sorted .INI files |
| |
| Input char *name1 File 1 name |
| dict_t *tree1 Tree section structure for file 1 |
| char *name2 File 2 name |
| dict_t *tree2 Tree section structure for file 2 |
| |
| Output none |
| |
| History |
| 1993-09-29 JFL Initial implementation |
| 2017-01-01 JFL Adapted to dict_t types. |
| |
+----------------------------------------------------------------------------*/
int compare(char *name1, dict_t *tree1, char *name2, dict_t *tree2)
{
dictnode *n1, *n2;
dict_t *s1, *s2; /* Section dictionaries */
dictnode *i1, *i2; /* Items */
dictnode *i01, *i02;
outstate os;
int sdone = FALSE;
unsigned long nc = 0; /* Number of comparisons done */
printf("Comparing .ini files %s and %s\n", name1, name2); /* Similar message to that of fc.exe */
fflush(stdout);
fflush(stderr);
os = EQUAL;
for (n1=FirstDictValue(tree1), n2=FirstDictValue(tree2); n1 || n2; )
{
int dif;
if (sdone) newOutState(&os, EQUAL, name1, name2); /* 2017-01-02 JFL Added to close EQUAL section */
s1 = s2 = NULL;
if (n1) s1 = n1->pData;
if (n2) s2 = n2->pData;
DEBUG_PRINTF(("// Comparing sections [%s] and [%s]\n", (n1 ? n1->pszKey : "(null)"), (n2 ? n2->pszKey : "(null)")));
if (!s1 && s2)
dif = 1;
else if (s1 && !s2)
dif = -1;
else
dif = compString(n1->pszKey, n2->pszKey);
if (dif < 0)
{
newOutState(&os, FILE1, name1, name2);
printSectName(n1->pszKey);
ForeachDictValue(s1, printItemCB, NULL);
n1 = NextDictValue(tree1, n1);
continue;
}
if (dif > 0)
{
newOutState(&os, FILE2, name1, name2);
printSectName(n2->pszKey);
ForeachDictValue(s2, printItemCB, NULL);
n2 = NextDictValue(tree2, n2);
continue;
}
/* Else section names match. Compare Items */
newOutState(&os, EQUAL, name1, name2);
sdone = FALSE;
i01 = i02 = NULL;
for (i1=FirstDictValue(s1), i2=FirstDictValue(s2); i1 || i2; )
{
nc += 1;
if ((verbose) && ((nc % 10000) == 0)) fprintf(stderr,
"Processing value %lu: %s\\%s\n", nc, n1->pszKey, (i1 ? i1->pszKey : i2->pszKey)
);
DEBUG_PRINTF(("// Comparing values \"%s\" and \"%s\"\n", (i1 ? i1->pszKey : "(null)"), (i2 ? i2->pszKey : "(null)")));
if (!i1 || !i2) /* We're sure at least one of them is not NULL */
{
if (!sdone)
{
printSectName(n1->pszKey);
sdone = TRUE;
}
if (!i01 && !i02) /* Remember the first difference */
{
i01 = i1;
i02 = i2;
}
if (i1)
i1 = NextDictValue(s1, i1);
else
i2 = NextDictValue(s2, i2);
continue;
}
dif = compItem(i1, i2);
if (dif)
{
if (!sdone)
{
printSectName(n1->pszKey);
sdone = TRUE;
}
if (!i01 && !i02) /* Remember the first difference */
{
i01 = i1;
i02 = i2;
}
if (dif < 0)
i1 = NextDictValue(s1, i1);
else
i2 = NextDictValue(s2, i2);
continue;
}
/* No difference */
if (i01 || i02) /* Display differing lines */
{
newOutState(&os, FILE1, name1, name2);
if (i01) for ( ; i01 != i1; i01 = NextDictValue(s1, i01)) printItem(i01);
newOutState(&os, FILE2, name1, name2);
if (i02) for ( ; i02 != i2; i02 = NextDictValue(s2, i02)) printItem(i02);
i01 = i02 = NULL;
}
i1 = NextDictValue(s1, i1);
i2 = NextDictValue(s2, i2);
}
if (i01 || i02) /* Display differing lines */
{
newOutState(&os, FILE1, name1, name2);
if (i01) for ( ; i01; i01 = NextDictValue(s1, i01)) printItem(i01);
newOutState(&os, FILE2, name1, name2);
if (i02) for ( ; i02; i02 = NextDictValue(s2, i02)) printItem(i02);
i01 = i02 = NULL;
}
n1 = NextDictValue(tree1, n1);
n2 = NextDictValue(tree2, n2);
}
newOutState(&os, EQUAL, name1, name2);
return 0;
}
/*----------------------------------------------------------------------------+
| |
| Function: newOutState |
| |
| Description: Change the output file state |
| |
| Input: outstate *pold Pointer to the state variable to change |
| outstate new New state value to reach |
| char *name1 File 1 name |
| char *name2 File 2 name |
| |
| Output: None |
| |
| Notes: The output state corresponds to the name of the file |
| from which lines are currently being displayed. |
| |
| Updates: |
| 1993-09-29 JFL Initial implementation |
| |
+----------------------------------------------------------------------------*/
void newOutState(outstate *pold, outstate new, char *name1, char *name2)
{
while (*pold != new)
{
switch (*pold)
{
case EQUAL:
printf("\n***** %s\n", name1);
*pold = FILE1;
break;
case FILE1:
printf("***** %s\n", name2);
*pold = FILE2;
break;
case FILE2:
printf("*****\n");
*pold = EQUAL;
break;
default:
break;
}
}
return;
}
/*----------------------------------------------------------------------------+
| |
| Function: printSectName |
| |
| Description: Display a section name |
| |
| Input: section *ps Pointer to the section to display |
| |
| Output: None |
| |
| Updates: |
| 1993-09-29 JFL Initial implementation |
| 2020-02-17 JFL No need to display the initial unnamed section [] name. |
| |
+----------------------------------------------------------------------------*/
#ifdef _MSC_VER
#pragma warning(disable:4100) /* Ignore the "unreferenced formal parameter" warning */
#endif
void *printSectNameCB(char *pszName, void *pValue, void *pRef)
{
if (pszName[0]) printf("\n[%s]\n", pszName);
return NULL;
}
void *printSectCB(char *pszName, void *pValue, void *pRef)
{
if (pszName[0]) printf("\n[%s]\n", pszName);
ForeachDictValue(pValue, printItemCB, pRef);
return NULL;
}
#ifdef _MSC_VER
#pragma warning(default:4100) /* Restore the "unreferenced formal parameter" warning */
#endif
void printSectName(char *name)
{
printSectNameCB(name, NULL, NULL);
}
/*----------------------------------------------------------------------------+
| |
| Function printItem |
| |
| Description Display a section item |
| |
| History |
| 1993-09-29 JFL Initial implementation |
| 2017-01-01 JFL Rewritten to be usable as a dict_t enumeration callback. |
| 2017-01-05 JFL Quote names containing spaces or =. |
| 2020-02-17 JFL Don't quote free-style lines without a value. |
| |
+----------------------------------------------------------------------------*/
#ifdef _MSC_VER
#pragma warning(disable:4100) /* Ignore the "unreferenced formal parameter" warning */
#endif
void *printItemCB(char *pszName, void *pValue, void *pRef)
{
char *pszValue = pValue;
if ( *pszName /* If the name is not empty */
&& ( (!pszName[strcspn(pszName, "= \t")]) /* and if there are no spaces or = in the name */
|| (!pszValue)) /* or we don't have a value */
) {
printf(" %s", pszName);
} else {
printf(" \"%s\"", pszName);
}
if (pszValue) {
size_t l = strlen(pszValue);
if ( !l /* If the value is empty */
|| pszValue[strcspn(pszValue, "\r\n")]/* or if it spans multiple lines */
|| isspace(pszValue[0]) /* or if it begins with a space */
|| (l && isspace(pszValue[l-1])) /* or if it ends with a space */
) {
printf(" = \"%s\"", pszValue);
} else {
printf(" = %s", pszValue);
}
}
printf("\n");
return NULL;
}
#ifdef _MSC_VER
#pragma warning(default:4100) /* Restore the "unreferenced formal parameter" warning */
#endif
void *printItem(dictnode *pi)
{
return printItemCB(pi->pszKey, pi->pData, NULL);
}
/*----------------------------------------------------------------------------+