2 * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
28 * compare_dump_files.c
30 * Created on: 25 juil. 2011
32 * BASELINE MUST BE GENERATED BY UNIX PLATFORM REGARDING TO THE CRLF PROBLEM
40 #include "opj_getopt.h"
42 typedef struct test_cmp_parameters
48 } test_cmp_parameters;
50 /*******************************************************************************
51 * Command line help function
52 *******************************************************************************/
53 void compare_dump_files_help_display(void) {
54 fprintf(stdout,"\nList of parameters for the compare_dump_files function \n");
56 fprintf(stdout," -b \t REQUIRED \t filename to the reference/baseline dump file \n");
57 fprintf(stdout," -t \t REQUIRED \t filename to the test dump file image\n");
60 /*******************************************************************************
62 *******************************************************************************/
63 int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
65 int sizemembasefile, sizememtestfile;
67 const char optlist[] = "b:t:";
71 param->base_filename = NULL;
72 param->test_filename = NULL;
76 while ((c = opj_getopt(argc, argv, optlist)) != -1)
80 sizemembasefile = (int)strlen(opj_optarg)+1;
81 param->base_filename = (char*) malloc(sizemembasefile);
82 param->base_filename[0] = '\0';
83 strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
84 param->base_filename[strlen(opj_optarg)] = '\0';
85 //printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );
88 sizememtestfile = (int) strlen(opj_optarg) + 1;
89 param->test_filename = (char*) malloc(sizememtestfile);
90 param->test_filename[0] = '\0';
91 strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
92 param->test_filename[strlen(opj_optarg)] = '\0';
93 //printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);
96 if ( (opj_optopt == 'b') || (opj_optopt == 't') )
97 fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
99 if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
100 else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
103 fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
107 if (opj_optind != argc)
109 for (index = opj_optind; index < argc; index++)
110 fprintf(stderr,"Non-option argument %s\n", argv[index]);
116 /*******************************************************************************
118 *******************************************************************************/
119 int main(int argc, char **argv)
121 test_cmp_parameters inParam;
122 FILE *fbase=NULL, *ftest=NULL;
125 unsigned long l=1, pos;
127 if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
129 compare_dump_files_help_display();
130 if (!inParam.base_filename) free(inParam.base_filename);
131 if (!inParam.test_filename) free(inParam.test_filename);
135 // Display Parameters
136 printf("******Parameters********* \n");
137 printf(" base_filename = %s\n"
138 " test_filename = %s\n",
139 inParam.base_filename, inParam.test_filename);
140 printf("************************* \n");
143 printf("Try to open: %s for reading ... ", inParam.base_filename);
144 if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
147 free(inParam.base_filename);
148 free(inParam.test_filename);
154 printf("Try to open: %s for reading ... ", inParam.test_filename);
155 if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
159 free(inParam.base_filename);
160 free(inParam.test_filename);
169 chbase = fgetc(fbase);
172 printf("Error reading base file.\n");
176 chtest = fgetc(ftest);
179 printf("Error reading test file.\n");
183 // CRLF problem (Baseline must be always generated by unix platform)
184 if (chbase == '\n' && chtest == '\r')
185 if (fgetc(ftest) == '\n')
190 size_t nbytes = 2048;
192 char *strbase, *strtest, *strbase_d, *strtest_d;
194 printf("Files differ at line %lu:\n", l);
195 fseek(fbase,pos,SEEK_SET);
197 // Take into account CRLF characters when we write \n into
198 // dump file when we used WIN platform
201 fseek(ftest,pos + l - 1,SEEK_SET);
203 fseek(ftest,pos,SEEK_SET);
206 strbase = (char *) malloc(nbytes + 1);
207 strtest = (char *) malloc(nbytes + 1);
209 if (fgets(strbase, nbytes, fbase) == NULL)
210 fprintf(stderr,"\nWARNING: fgets return a NULL value");
213 if (fgets(strtest, nbytes, ftest) == NULL)
214 fprintf(stderr,"\nWARNING: fgets return a NULL value");
217 strbase_d = (char *) malloc(strlen(strbase)+1);
218 strtest_d = (char *) malloc(strlen(strtest)+1);
219 strncpy(strbase_d, strbase, strlen(strbase)-1);
220 strncpy(strtest_d, strtest, strlen(strtest)-CRLF_shift);
221 strbase_d[strlen(strbase)-1] = '\0';
222 strtest_d[strlen(strtest)-CRLF_shift] = '\0';
223 printf("<%s> vs. <%s>\n", strbase_d, strtest_d);
224 free(strbase_d);free(strtest_d);
228 free(strbase);free(strtest);
249 free(inParam.base_filename);
250 free(inParam.test_filename);
254 printf("\n***** TEST SUCCEED: Files are the same. *****\n");
257 else return EXIT_FAILURE;