OpenJPH: /build/openjph/src/OpenJPH/tests/compare_files.cpp Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
compare_files.cpp
Go to the documentation of this file.
1
2#include <cstdio>
3#include <cstdlib>
4
6static inline
7 int getc_and_check(FILE *f, const char* filename)
8{
9 int c;
10 c = getc(f);
11 if (feof(f)) {
12 fprintf(stderr, "Error while reading a comment from %s\n", filename);
13 exit(-1);
14 }
15 return c;
16}
17
19static inline
20 void eat_comments(FILE *f, const char* filename, int& c) noexcept
21{
22 int length = 0;
23 length = getc_and_check(f, filename) << 8;
24 length |= getc_and_check(f, filename);
25 length -= 2;
26
27 for (int i = 0; i < length; ++i)
28 c = getc_and_check(f, filename);
29}
30
31
33 int main(int argc, char* argv[])
34{
35 if (argc < 3)
36 {
37 fprintf(stderr,
38 "compare_files expects two arguments <filename1, filename2>\n");
39 exit(-1);
40 }
41
42 FILE *f1 = fopen(argv[1], "rb");
43 if (f1 == NULL)
44 {
45 fprintf(stderr, "Unable to open file %s.\n", argv[1]);
46 return -1;
47 }
48
49 FILE *f2 = fopen(argv[2], "rb");
50 if (f2 == NULL)
51 {
52 fprintf(stderr, "Unable to open file %s.\n", argv[2]);
53 return -1;
54 }
55
56 bool tile_started = false;
57 int old_c1 = ' ';
58 while (1) // both files must end at the same time
59 {
60 int c1 = getc(f1);
61 int c2 = getc(f2);
62
63 bool eof1 = (feof(f1) != 0), eof2 = (feof(f2) != 0);
64
65 if (eof1 && eof2) // both reached end of file
66 {
67 fprintf(stdout, "Matching files.\n");
68 return 0;
69 }
70 else if (!eof1 && !eof2)
71 {
72 if (c1 != c2)
73 return -1;
74 if (!tile_started && old_c1 == 0xFF && c1 == 0x64) {
75 eat_comments(f1, argv[1], c1);
76 eat_comments(f2, argv[2], c2);
77 }
78 if (!tile_started && old_c1 == 0xFF && c1 == 0x90) {
79 // stop checking comments when a tile starts; we ignoring
80 // the case where tile can also have comments
81 tile_started = true;
82 }
83 old_c1 = c1;
84 }
85 else // only one of them reached end of file
86 {
87 fprintf(stderr, "One file finished before the other one.\n");
88 return -1;
89 }
90 }
91
92 fclose(f1);
93 fclose(f2);
94
95 return 0;
96}
getc_and_check
static int getc_and_check(FILE *f, const char *filename)
Definition compare_files.cpp:7
main
int main(int argc, char *argv[])
Definition compare_files.cpp:33
eat_comments
static void eat_comments(FILE *f, const char *filename, int &c) noexcept
Definition compare_files.cpp:20