[trunk] First pass to cleanup compare_dump_files

This commit is contained in:
Mathieu Malaterre 2014-03-10 09:56:22 +00:00
parent 24966ffbda
commit 7aee50de97
1 changed files with 61 additions and 75 deletions

View File

@ -62,7 +62,7 @@ static void compare_dump_files_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;
@ -77,19 +77,15 @@ 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;
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;
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 '?':
@ -108,11 +104,12 @@ static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
{
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;
}
/*******************************************************************************
* MAIN
*******************************************************************************/
@ -124,12 +121,10 @@ int main(int argc, char **argv)
int same = 1;
unsigned long l=1, pos;
if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
if( parse_cmdline_cmp(argc, argv, &inParam) == 1 )
{
compare_dump_files_help_display();
if (!inParam.base_filename) free(inParam.base_filename);
if (!inParam.test_filename) free(inParam.test_filename);
return EXIT_FAILURE;
goto cleanup;
}
/* Display Parameters*/
@ -143,10 +138,7 @@ int main(int argc, char **argv)
printf("Try to open: %s for reading ... ", inParam.base_filename);
if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
{
printf("Failed.\n");
free(inParam.base_filename);
free(inParam.test_filename);
return EXIT_FAILURE;
goto cleanup;
}
printf("Ok.\n");
@ -154,11 +146,7 @@ int main(int argc, char **argv)
printf("Try to open: %s for reading ... ", inParam.test_filename);
if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
{
printf("Failed.\n");
fclose(fbase);
free(inParam.base_filename);
free(inParam.test_filename);
return EXIT_FAILURE;
goto cleanup;
}
printf("Ok.\n");
@ -170,14 +158,14 @@ int main(int argc, char **argv)
if(ferror(fbase))
{
printf("Error reading base file.\n");
return EXIT_FAILURE;
goto cleanup;
}
chtest = fgetc(ftest);
if(ferror(ftest))
{
printf("Error reading test file.\n");
return EXIT_FAILURE;
goto cleanup;
}
/* CRLF problem (Baseline must be always generated by unix platform)*/
@ -221,14 +209,15 @@ int main(int argc, char **argv)
strbase_d[strlen(strbase)-1] = '\0';
strtest_d[strlen(strtest)-(size_t)CRLF_shift] = '\0';
printf("<%s> vs. <%s>\n", strbase_d, strtest_d);
free(strbase_d);free(strtest_d);
free(strbase_d);
free(strtest_d);
}
}
free(strbase);free(strtest);
free(strbase);
free(strtest);
same = 0;
break;
}
else
@ -241,18 +230,15 @@ int main(int argc, char **argv)
}
}
printf("\n***** TEST SUCCEED: Files are the same. *****\n");
cleanup:
/*Close File*/
fclose(fbase);
fclose(ftest);
if(fbase) fclose(fbase);
if(ftell) fclose(ftest);
/* Free memory*/
free(inParam.base_filename);
free(inParam.test_filename);
if(same)
{
printf("\n***** TEST SUCCEED: Files are the same. *****\n");
return EXIT_SUCCESS;
}
else return EXIT_FAILURE;
return same ? EXIT_SUCCESS : EXIT_FAILURE;
}