use ansi c function fgets instead of GNU function getline to avoid build error with...
[openjpeg.git] / tests / compare_dump_files.c
1 /*
2  * compare_dump_files.c
3  *
4  *  Created on: 25 juil. 2011
5  *      Author: mickael
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12
13 #include "getopt.h"
14
15 typedef struct test_cmp_parameters
16 {
17   /**  */
18   char* base_filename;
19   /**  */
20   char* test_filename;
21 } test_cmp_parameters;
22
23 /*******************************************************************************
24  * Command line help function
25  *******************************************************************************/
26 void compare_dump_files_help_display() {
27   fprintf(stdout,"\nList of parameters for the compare_dump_files function  \n");
28   fprintf(stdout,"\n");
29   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline dump file \n");
30   fprintf(stdout,"  -t \t REQUIRED \t filename to the test dump file image\n");
31   fprintf(stdout,"\n");
32 }
33 /*******************************************************************************
34  * Parse command line
35  *******************************************************************************/
36 int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
37 {
38   int sizemembasefile, sizememtestfile;
39   int index;
40   const char optlist[] = "b:t:";
41   int c;
42
43   // Init parameters
44   param->base_filename = NULL;
45   param->test_filename = NULL;
46
47   opterr = 0;
48
49   while ((c = getopt(argc, argv, optlist)) != -1)
50     switch (c)
51       {
52       case 'b':
53         sizemembasefile = (int)strlen(optarg)+1;
54         param->base_filename = (char*) malloc(sizemembasefile);
55         param->base_filename[0] = '\0';
56         strncpy(param->base_filename, optarg, strlen(optarg));
57         param->base_filename[strlen(optarg)] = '\0';
58         //printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );
59         break;
60       case 't':
61         sizememtestfile = (int) strlen(optarg) + 1;
62         param->test_filename = (char*) malloc(sizememtestfile);
63         param->test_filename[0] = '\0';
64         strncpy(param->test_filename, optarg, strlen(optarg));
65         param->test_filename[strlen(optarg)] = '\0';
66         //printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);
67        break;
68       case '?':
69         if ( (optopt == 'b') || (optopt == 't') )
70           fprintf(stderr, "Option -%c requires an argument.\n", optopt);
71         else
72           if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt);
73           else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
74         return 1;
75       default:
76         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, optarg);
77         break;
78       }
79
80   if (optind != argc)
81     {
82     for (index = optind; index < argc; index++)
83       fprintf(stderr,"Non-option argument %s\n", argv[index]);
84     return EXIT_FAILURE;
85     }
86
87   return EXIT_SUCCESS;
88 }
89 /*******************************************************************************
90  * MAIN
91  *******************************************************************************/
92 int main(int argc, char **argv)
93 {
94   test_cmp_parameters inParam;
95   FILE *fbase=NULL, *ftest=NULL;
96   char chbase, chtest;
97   int same = 1;
98   unsigned long l=1, pos;
99
100   if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
101     {
102     compare_dump_files_help_display();
103     if (!inParam.base_filename) free(inParam.base_filename);
104     if (!inParam.test_filename) free(inParam.test_filename);
105     return EXIT_FAILURE;
106     }
107
108   // Display Parameters
109   printf("******Parameters********* \n");
110   printf(" base_filename = %s\n"
111           " test_filename = %s\n",
112           inParam.base_filename, inParam.test_filename);
113   printf("************************* \n");
114
115   /* open base file */
116   printf("Try to open: %s for reading ... ", inParam.base_filename);
117   if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
118     {
119     printf("Failed.\n");
120     free(inParam.base_filename);
121     free(inParam.test_filename);
122     return EXIT_FAILURE;
123     }
124   printf("Ok.\n");
125
126   /* open test file */
127   printf("Try to open: %s for reading ... ", inParam.test_filename);
128   if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
129     {
130     printf("Failed.\n");
131     fclose(fbase);
132     free(inParam.base_filename);
133     free(inParam.test_filename);
134     return EXIT_FAILURE;
135     }
136   printf("Ok.\n");
137
138   pos=ftell(fbase);
139
140   while(!feof(fbase))
141     {
142     chbase = fgetc(fbase);
143     if(ferror(fbase))
144       {
145       printf("Error reading base file.\n");
146       return EXIT_FAILURE;
147       }
148
149     chtest = fgetc(ftest);
150     if(ferror(ftest))
151       {
152       printf("Error reading test file.\n");
153       return EXIT_FAILURE;
154       }
155
156     if(chbase != chtest)
157       {
158       size_t nbytes = 2048;
159       char *strbase, *strtest, *strbase_d, *strtest_d;
160
161       printf("Files differ at line %lu:\n", l);
162       fseek(fbase,pos,SEEK_SET);
163       fseek(ftest,pos,SEEK_SET);
164
165       strbase = (char *) malloc(nbytes + 1);
166       strtest = (char *) malloc(nbytes + 1);
167       fgets(strbase, nbytes, fbase);
168       fgets(strtest, nbytes, ftest);
169       strbase_d = (char *) malloc(strlen(strbase));
170       strtest_d = (char *) malloc(strlen(strtest));
171       strncpy(strbase_d, strbase, strlen(strbase)-1);
172       strncpy(strtest_d, strtest, strlen(strtest)-1);
173       strbase_d[strlen(strbase)] = '\0';
174       strtest_d[strlen(strtest)] = '\0';
175       printf("<%s> vs. <%s>\n", strbase_d, strtest_d);
176
177       free(strbase);free(strtest);
178       free(strbase_d);free(strtest_d);
179       same = 0;
180       break;
181       }
182     else
183       {
184       if (chbase == '\n')
185         {
186         l++;
187         pos = ftell(fbase);
188         }
189       }
190     }
191
192   //Close File
193   fclose(fbase);
194   fclose(ftest);
195
196   // Free memory
197   free(inParam.base_filename);
198   free(inParam.test_filename);
199
200   if(same)
201     {
202       printf("\n***** TEST SUCCEED: Files are the same. *****\n");
203       return EXIT_SUCCESS;
204     }
205   else return EXIT_FAILURE;
206 }