[trunk] Rework the code to simplify cleanup code

This commit is contained in:
Mathieu Malaterre 2014-03-07 16:15:59 +00:00
parent 440ef4873b
commit a0aa8dd851
1 changed files with 109 additions and 155 deletions

View File

@ -40,21 +40,21 @@
typedef struct test_cmp_parameters typedef struct test_cmp_parameters
{ {
/** */ /** */
char* base_filename; char* base_filename;
/** */ /** */
char* test_filename; char* test_filename;
} test_cmp_parameters; } test_cmp_parameters;
/******************************************************************************* /*******************************************************************************
* Command line help function * Command line help function
*******************************************************************************/ *******************************************************************************/
static void compareRAWimages_help_display(void) { static void compareRAWimages_help_display(void) {
fprintf(stdout,"\nList of parameters for the comparePGX function \n"); fprintf(stdout,"\nList of parameters for the comparePGX function \n");
fprintf(stdout,"\n"); fprintf(stdout,"\n");
fprintf(stdout," -b \t REQUIRED \t filename to the reference/baseline RAW image \n"); fprintf(stdout," -b \t REQUIRED \t filename to the reference/baseline RAW image \n");
fprintf(stdout," -t \t REQUIRED \t filename to the test RAW image\n"); fprintf(stdout," -t \t REQUIRED \t filename to the test RAW image\n");
fprintf(stdout,"\n"); fprintf(stdout,"\n");
} }
/******************************************************************************* /*******************************************************************************
@ -62,54 +62,52 @@ static void compareRAWimages_help_display(void) {
*******************************************************************************/ *******************************************************************************/
static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param) static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
{ {
int sizemembasefile, sizememtestfile; size_t sizemembasefile, sizememtestfile;
int index; int index;
const char optlist[] = "b:t:"; const char optlist[] = "b:t:";
int c; int c;
/* Init parameters*/ /* Init parameters*/
param->base_filename = NULL; param->base_filename = NULL;
param->test_filename = NULL; param->test_filename = NULL;
opj_opterr = 0; opj_opterr = 0;
while ((c = opj_getopt(argc, argv, optlist)) != -1) while ((c = opj_getopt(argc, argv, optlist)) != -1)
switch (c) switch (c)
{ {
case 'b': case 'b':
sizemembasefile = (int)strlen(opj_optarg)+1; sizemembasefile = strlen(opj_optarg)+1;
param->base_filename = (char*) malloc((size_t)sizemembasefile); free(param->base_filename); // handle dup option
param->base_filename[0] = '\0'; param->base_filename = (char*) malloc(sizemembasefile);
strncpy(param->base_filename, opj_optarg, strlen(opj_optarg)); strcpy(param->base_filename, opj_optarg);
param->base_filename[strlen(opj_optarg)] = '\0'; /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
/*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/ break;
break; case 't':
case 't': sizememtestfile = strlen(opj_optarg) + 1;
sizememtestfile = (int) strlen(opj_optarg) + 1; free(param->test_filename); // handle dup option
param->test_filename = (char*) malloc((size_t)sizememtestfile); param->test_filename = (char*) malloc(sizememtestfile);
param->test_filename[0] = '\0'; strcpy(param->test_filename, opj_optarg);
strncpy(param->test_filename, opj_optarg, strlen(opj_optarg)); /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
param->test_filename[strlen(opj_optarg)] = '\0'; break;
/*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/ case '?':
break; if ((opj_optopt == 'b') || (opj_optopt == 't'))
case '?': fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
if ((opj_optopt == 'b') || (opj_optopt == 't')) else
fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt); if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
else else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt); return 1;
else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt); default:
return 1; fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
default: break;
fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg); }
break;
}
if (opj_optind != argc) { if (opj_optind != argc) {
for (index = opj_optind; index < argc; index++) for (index = opj_optind; index < argc; index++)
fprintf(stderr,"Non-option argument %s\n", argv[index]); fprintf(stderr,"Non-option argument %s\n", argv[index]);
return EXIT_FAILURE; return 1;
} }
return EXIT_SUCCESS; return 0;
} }
/******************************************************************************* /*******************************************************************************
@ -118,119 +116,75 @@ static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int pos = 0; int pos = 0;
test_cmp_parameters inParam; test_cmp_parameters inParam;
FILE *file_test=NULL, *file_base=NULL; FILE *file_test=NULL, *file_base=NULL;
unsigned char equal = 1; unsigned char equal = 1;
/* Get parameters from command line*/ /* Get parameters from command line*/
if (parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE) if (parse_cmdline_cmp(argc, argv, &inParam))
{ {
compareRAWimages_help_display(); compareRAWimages_help_display();
goto cleanup;
}
/* Free Memory */ file_test = fopen(inParam.test_filename, "rb");
if (inParam.base_filename){ if (!file_test) {
free(inParam.base_filename); fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
inParam.base_filename = NULL; goto cleanup;
} }
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
return EXIT_FAILURE; file_base = fopen(inParam.base_filename, "rb");
} if (!file_base) {
fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
goto cleanup;
}
file_test = fopen(inParam.test_filename, "rb"); /* Read simultaneously the two files*/
if (!file_test) { while (equal)
fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename); {
unsigned char value_test = 0;
unsigned char eof_test = 0;
unsigned char value_base = 0;
unsigned char eof_base = 0;
/* Free Memory */ /* Read one byte*/
if (inParam.base_filename){ if (!fread(&value_test, 1, 1, file_test)) {
free(inParam.base_filename); eof_test = 1;
inParam.base_filename = NULL; }
}
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
return EXIT_FAILURE; /* Read one byte*/
} if (!fread(&value_base, 1, 1, file_base)) {
eof_base = 1;
}
file_base = fopen(inParam.base_filename, "rb"); /* End of file reached by the two files?*/
if (!file_base) { if (eof_test && eof_base)
fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename); break;
/* Free Memory */ /* End of file reached only by one file?*/
if (inParam.base_filename){ if (eof_test || eof_base)
free(inParam.base_filename); {
inParam.base_filename = NULL; fprintf(stdout,"Files have different sizes.\n");
} equal = 0;
if (inParam.test_filename){ }
free(inParam.test_filename);
inParam.test_filename = NULL;
}
fclose(file_test); /* Binary values are equal?*/
return EXIT_FAILURE; if (value_test != value_base)
} {
fprintf(stdout,"Binary values read in the file are different %x vs %x at position %d.\n", value_test, value_base, pos);
/* Read simultaneously the two files*/ equal = 0;
while (equal) }
{
unsigned char value_test = 0;
unsigned char eof_test = 0;
unsigned char value_base = 0;
unsigned char eof_base = 0;
/* Read one byte*/
if (!fread(&value_test, 1, 1, file_test)) {
eof_test = 1;
}
/* Read one byte*/
if (!fread(&value_base, 1, 1, file_base)) {
eof_base = 1;
}
/* End of file reached by the two files?*/
if (eof_test && eof_base)
break;
/* End of file reached only by one file?*/
if (eof_test || eof_base)
{
fprintf(stdout,"Files have different sizes.\n");
equal = 0;
}
/* Binary values are equal?*/
if (value_test != value_base)
{
fprintf(stdout,"Binary values read in the file are different %x vs %x at position %d.\n", value_test, value_base, pos);
equal = 0;
}
pos++; pos++;
} }
/* Free Memory */ if(equal) fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
if (inParam.base_filename){ cleanup:
free(inParam.base_filename); if(file_test) fclose(file_test);
inParam.base_filename = NULL; if(file_base) fclose(file_base);
}
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
fclose(file_test); /* Free Memory */
fclose(file_base); free(inParam.base_filename);
free(inParam.test_filename);
if (equal) return equal ? EXIT_SUCCESS : EXIT_FAILURE;
{
fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
} }