[trunk] fgets takes an int as parameter
[openjpeg.git] / tests / compare_dump_files.c
1 /*
2  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 /*
28  * compare_dump_files.c
29  *
30  *  Created on: 25 juil. 2011
31  *      Author: mickael
32  * BASELINE MUST BE GENERATED BY UNIX PLATFORM REGARDING TO THE CRLF PROBLEM
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39
40 #include "opj_getopt.h"
41
42 typedef struct test_cmp_parameters
43 {
44   /**  */
45   char* base_filename;
46   /**  */
47   char* test_filename;
48 } test_cmp_parameters;
49
50 /*******************************************************************************
51  * Command line help function
52  *******************************************************************************/
53 static void compare_dump_files_help_display(void) {
54   fprintf(stdout,"\nList of parameters for the compare_dump_files function  \n");
55   fprintf(stdout,"\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");
58   fprintf(stdout,"\n");
59 }
60 /*******************************************************************************
61  * Parse command line
62  *******************************************************************************/
63 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
64 {
65   int sizemembasefile, sizememtestfile;
66   int index;
67   const char optlist[] = "b:t:";
68   int c;
69
70   /* Init parameters */
71   param->base_filename = NULL;
72   param->test_filename = NULL;
73
74   opj_opterr = 0;
75
76   while ((c = opj_getopt(argc, argv, optlist)) != -1)
77     switch (c)
78       {
79       case 'b':
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 );*/
86         break;
87       case 't':
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);*/
94        break;
95       case '?':
96         if ( (opj_optopt == 'b') || (opj_optopt == 't') )
97           fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
98         else
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);
101         return 1;
102       default:
103         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
104         break;
105       }
106
107   if (opj_optind != argc)
108     {
109     for (index = opj_optind; index < argc; index++)
110       fprintf(stderr,"Non-option argument %s\n", argv[index]);
111     return EXIT_FAILURE;
112     }
113
114   return EXIT_SUCCESS;
115 }
116 /*******************************************************************************
117  * MAIN
118  *******************************************************************************/
119 int main(int argc, char **argv)
120 {
121   test_cmp_parameters inParam;
122   FILE *fbase=NULL, *ftest=NULL;
123   int chbase, chtest;
124   int same = 1;
125   unsigned long l=1, pos;
126
127   if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
128     {
129     compare_dump_files_help_display();
130     if (!inParam.base_filename) free(inParam.base_filename);
131     if (!inParam.test_filename) free(inParam.test_filename);
132     return EXIT_FAILURE;
133     }
134
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");
141
142   /* open base file */
143   printf("Try to open: %s for reading ... ", inParam.base_filename);
144   if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
145     {
146     printf("Failed.\n");
147     free(inParam.base_filename);
148     free(inParam.test_filename);
149     return EXIT_FAILURE;
150     }
151   printf("Ok.\n");
152
153   /* open test file */
154   printf("Try to open: %s for reading ... ", inParam.test_filename);
155   if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
156     {
157     printf("Failed.\n");
158     fclose(fbase);
159     free(inParam.base_filename);
160     free(inParam.test_filename);
161     return EXIT_FAILURE;
162     }
163   printf("Ok.\n");
164
165   pos=ftell(fbase);
166
167   while(!feof(fbase))
168     {
169     chbase = fgetc(fbase);
170     if(ferror(fbase))
171       {
172       printf("Error reading base file.\n");
173       return EXIT_FAILURE;
174       }
175
176     chtest = fgetc(ftest);
177     if(ferror(ftest))
178       {
179       printf("Error reading test file.\n");
180       return EXIT_FAILURE;
181       }
182
183     /* CRLF problem (Baseline must be always generated by unix platform)*/
184     if (chbase == '\n' && chtest == '\r')
185       if (fgetc(ftest) == '\n')
186         chtest = '\n';
187
188     if(chbase != chtest)
189       {
190       int nbytes = 2048;
191       int CRLF_shift=1;
192       char *strbase, *strtest, *strbase_d, *strtest_d;
193
194       printf("Files differ at line %lu:\n", l);
195       fseek(fbase,pos,SEEK_SET);
196
197       /* Take into account CRLF characters when we write \n into
198       // dump file when we used WIN platform*/
199 #ifdef _WIN32
200       CRLF_shift = 2;
201       fseek(ftest,pos + l - 1,SEEK_SET);
202 #else
203       fseek(ftest,pos,SEEK_SET);
204 #endif
205
206       strbase = (char *) malloc(nbytes + 1);
207       strtest = (char *) malloc(nbytes + 1);
208
209         if (fgets(strbase, nbytes, fbase) == NULL)
210                 fprintf(stderr,"\nWARNING: fgets return a NULL value");
211         else
212         {
213                 if (fgets(strtest, nbytes, ftest) == NULL)
214                         fprintf(stderr,"\nWARNING: fgets return a NULL value");
215                 else
216                 {
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);
225                 }
226         }
227
228         free(strbase);free(strtest);
229         
230         same = 0;
231
232       break;
233       }
234     else
235       {
236       if (chbase == '\n')
237         {
238         l++;
239         pos = ftell(fbase);
240         }
241       }
242     }
243
244   /*Close File*/
245   fclose(fbase);
246   fclose(ftest);
247
248   /* Free memory*/
249   free(inParam.base_filename);
250   free(inParam.test_filename);
251
252   if(same)
253     {
254       printf("\n***** TEST SUCCEED: Files are the same. *****\n");
255       return EXIT_SUCCESS;
256     }
257   else return EXIT_FAILURE;
258 }