[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;
@ -76,43 +76,40 @@ static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
while ((c = opj_getopt(argc, argv, optlist)) != -1)
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';
/*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';
/*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
break;
case '?':
if ( (opj_optopt == 'b') || (opj_optopt == 't') )
fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
else
if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
return 1;
default:
fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
break;
case 'b':
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 = 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 '?':
if ( (opj_optopt == 'b') || (opj_optopt == 't') )
fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
else
if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
return 1;
default:
fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
break;
}
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;
}
/*******************************************************************************
* MAIN
*******************************************************************************/
@ -124,29 +121,24 @@ 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*/
printf("******Parameters********* \n");
printf(" base_filename = %s\n"
" test_filename = %s\n",
inParam.base_filename, inParam.test_filename);
" test_filename = %s\n",
inParam.base_filename, inParam.test_filename);
printf("************************* \n");
/* open base file */
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)*/
@ -206,29 +194,30 @@ int main(int argc, char **argv)
strbase = (char *) malloc((size_t)nbytes + 1);
strtest = (char *) malloc((size_t)nbytes + 1);
if (fgets(strbase, nbytes, fbase) == NULL)
fprintf(stderr,"\nWARNING: fgets return a NULL value");
else
{
if (fgets(strtest, nbytes, ftest) == NULL)
fprintf(stderr,"\nWARNING: fgets return a NULL value");
else
{
strbase_d = (char *) malloc(strlen(strbase)+1);
strtest_d = (char *) malloc(strlen(strtest)+1);
strncpy(strbase_d, strbase, strlen(strbase)-1);
strncpy(strtest_d, strtest, strlen(strtest)-(size_t)CRLF_shift);
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);
}
}
if (fgets(strbase, nbytes, fbase) == NULL)
fprintf(stderr,"\nWARNING: fgets return a NULL value");
else
{
if (fgets(strtest, nbytes, ftest) == NULL)
fprintf(stderr,"\nWARNING: fgets return a NULL value");
else
{
strbase_d = (char *) malloc(strlen(strbase)+1);
strtest_d = (char *) malloc(strlen(strtest)+1);
strncpy(strbase_d, strbase, strlen(strbase)-1);
strncpy(strtest_d, strtest, strlen(strtest)-(size_t)CRLF_shift);
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);free(strtest);
same = 0;
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;
}