[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

@ -62,7 +62,7 @@ static void compareRAWimages_help_display(void) {
*******************************************************************************/
static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
{
int sizemembasefile, sizememtestfile;
size_t sizemembasefile, sizememtestfile;
int index;
const char optlist[] = "b:t:";
int c;
@ -76,19 +76,17 @@ static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
switch (c)
{
case 'b':
sizemembasefile = (int)strlen(opj_optarg)+1;
param->base_filename = (char*) malloc((size_t)sizemembasefile);
param->base_filename[0] = '\0';
strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
param->base_filename[strlen(opj_optarg)] = '\0';
sizemembasefile = strlen(opj_optarg)+1;
free(param->base_filename); // handle dup option
param->base_filename = (char*) malloc(sizemembasefile);
strcpy(param->base_filename, opj_optarg);
/*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
break;
case 't':
sizememtestfile = (int) strlen(opj_optarg) + 1;
param->test_filename = (char*) malloc((size_t)sizememtestfile);
param->test_filename[0] = '\0';
strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
param->test_filename[strlen(opj_optarg)] = '\0';
sizememtestfile = strlen(opj_optarg) + 1;
free(param->test_filename); // handle dup option
param->test_filename = (char*) malloc(sizememtestfile);
strcpy(param->test_filename, opj_optarg);
/*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
break;
case '?':
@ -106,10 +104,10 @@ static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
if (opj_optind != argc) {
for (index = opj_optind; index < argc; index++)
fprintf(stderr,"Non-option argument %s\n", argv[index]);
return EXIT_FAILURE;
return 1;
}
return EXIT_SUCCESS;
return 0;
}
/*******************************************************************************
@ -123,56 +121,22 @@ int main(int argc, char **argv)
unsigned char equal = 1;
/* Get parameters from command line*/
if (parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE)
if (parse_cmdline_cmp(argc, argv, &inParam))
{
compareRAWimages_help_display();
/* Free Memory */
if (inParam.base_filename){
free(inParam.base_filename);
inParam.base_filename = NULL;
}
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
return EXIT_FAILURE;
goto cleanup;
}
file_test = fopen(inParam.test_filename, "rb");
if (!file_test) {
fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
/* Free Memory */
if (inParam.base_filename){
free(inParam.base_filename);
inParam.base_filename = NULL;
}
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
return EXIT_FAILURE;
goto cleanup;
}
file_base = fopen(inParam.base_filename, "rb");
if (!file_base) {
fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
/* Free Memory */
if (inParam.base_filename){
free(inParam.base_filename);
inParam.base_filename = NULL;
}
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
fclose(file_test);
return EXIT_FAILURE;
goto cleanup;
}
/* Read simultaneously the two files*/
@ -213,24 +177,14 @@ int main(int argc, char **argv)
pos++;
}
if(equal) fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
cleanup:
if(file_test) fclose(file_test);
if(file_base) fclose(file_base);
/* Free Memory */
if (inParam.base_filename){
free(inParam.base_filename);
inParam.base_filename = NULL;
}
if (inParam.test_filename){
free(inParam.test_filename);
inParam.test_filename = NULL;
}
fclose(file_test);
fclose(file_base);
if (equal)
{
fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
return equal ? EXIT_SUCCESS : EXIT_FAILURE;
}