[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) 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;
@ -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) 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); param->base_filename = (char*) malloc(sizemembasefile);
param->base_filename[0] = '\0'; strcpy(param->base_filename, opj_optarg);
strncpy(param->base_filename, opj_optarg, strlen(opj_optarg)); /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
param->base_filename[strlen(opj_optarg)] = '\0'; break;
/*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/ case 't':
break; sizememtestfile = strlen(opj_optarg) + 1;
case 't': param->test_filename = (char*) malloc(sizememtestfile);
sizememtestfile = (int) strlen(opj_optarg) + 1; strcpy(param->test_filename, opj_optarg);
param->test_filename = (char*) malloc((size_t)sizememtestfile); /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
param->test_filename[0] = '\0'; break;
strncpy(param->test_filename, opj_optarg, strlen(opj_optarg)); case '?':
param->test_filename[strlen(opj_optarg)] = '\0'; if ( (opj_optopt == 'b') || (opj_optopt == 't') )
/*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/ fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
break; else
case '?': if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
if ( (opj_optopt == 'b') || (opj_optopt == 't') ) else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt); return 1;
else default:
if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt); fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt); break;
return 1;
default:
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;
} }
/******************************************************************************* /*******************************************************************************
* MAIN * MAIN
*******************************************************************************/ *******************************************************************************/
@ -124,29 +121,24 @@ int main(int argc, char **argv)
int same = 1; int same = 1;
unsigned long l=1, pos; 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(); compare_dump_files_help_display();
if (!inParam.base_filename) free(inParam.base_filename); goto cleanup;
if (!inParam.test_filename) free(inParam.test_filename);
return EXIT_FAILURE;
} }
/* Display Parameters*/ /* Display Parameters*/
printf("******Parameters********* \n"); printf("******Parameters********* \n");
printf(" base_filename = %s\n" printf(" base_filename = %s\n"
" test_filename = %s\n", " test_filename = %s\n",
inParam.base_filename, inParam.test_filename); inParam.base_filename, inParam.test_filename);
printf("************************* \n"); printf("************************* \n");
/* open base file */ /* open base file */
printf("Try to open: %s for reading ... ", inParam.base_filename); printf("Try to open: %s for reading ... ", inParam.base_filename);
if((fbase = fopen(inParam.base_filename, "rb"))==NULL) if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
{ {
printf("Failed.\n"); goto cleanup;
free(inParam.base_filename);
free(inParam.test_filename);
return EXIT_FAILURE;
} }
printf("Ok.\n"); printf("Ok.\n");
@ -154,11 +146,7 @@ int main(int argc, char **argv)
printf("Try to open: %s for reading ... ", inParam.test_filename); printf("Try to open: %s for reading ... ", inParam.test_filename);
if((ftest = fopen(inParam.test_filename, "rb"))==NULL) if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
{ {
printf("Failed.\n"); goto cleanup;
fclose(fbase);
free(inParam.base_filename);
free(inParam.test_filename);
return EXIT_FAILURE;
} }
printf("Ok.\n"); printf("Ok.\n");
@ -170,14 +158,14 @@ int main(int argc, char **argv)
if(ferror(fbase)) if(ferror(fbase))
{ {
printf("Error reading base file.\n"); printf("Error reading base file.\n");
return EXIT_FAILURE; goto cleanup;
} }
chtest = fgetc(ftest); chtest = fgetc(ftest);
if(ferror(ftest)) if(ferror(ftest))
{ {
printf("Error reading test file.\n"); printf("Error reading test file.\n");
return EXIT_FAILURE; goto cleanup;
} }
/* CRLF problem (Baseline must be always generated by unix platform)*/ /* 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); strbase = (char *) malloc((size_t)nbytes + 1);
strtest = (char *) malloc((size_t)nbytes + 1); strtest = (char *) malloc((size_t)nbytes + 1);
if (fgets(strbase, nbytes, fbase) == NULL) if (fgets(strbase, nbytes, fbase) == NULL)
fprintf(stderr,"\nWARNING: fgets return a NULL value"); fprintf(stderr,"\nWARNING: fgets return a NULL value");
else else
{ {
if (fgets(strtest, nbytes, ftest) == NULL) if (fgets(strtest, nbytes, ftest) == NULL)
fprintf(stderr,"\nWARNING: fgets return a NULL value"); fprintf(stderr,"\nWARNING: fgets return a NULL value");
else else
{ {
strbase_d = (char *) malloc(strlen(strbase)+1); strbase_d = (char *) malloc(strlen(strbase)+1);
strtest_d = (char *) malloc(strlen(strtest)+1); strtest_d = (char *) malloc(strlen(strtest)+1);
strncpy(strbase_d, strbase, strlen(strbase)-1); strncpy(strbase_d, strbase, strlen(strbase)-1);
strncpy(strtest_d, strtest, strlen(strtest)-(size_t)CRLF_shift); strncpy(strtest_d, strtest, strlen(strtest)-(size_t)CRLF_shift);
strbase_d[strlen(strbase)-1] = '\0'; strbase_d[strlen(strbase)-1] = '\0';
strtest_d[strlen(strtest)-(size_t)CRLF_shift] = '\0'; strtest_d[strlen(strtest)-(size_t)CRLF_shift] = '\0';
printf("<%s> vs. <%s>\n", strbase_d, strtest_d); 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;
same = 0;
break; break;
} }
else else
@ -241,18 +230,15 @@ int main(int argc, char **argv)
} }
} }
printf("\n***** TEST SUCCEED: Files are the same. *****\n");
cleanup:
/*Close File*/ /*Close File*/
fclose(fbase); if(fbase) fclose(fbase);
fclose(ftest); if(ftell) fclose(ftest);
/* Free memory*/ /* Free memory*/
free(inParam.base_filename); free(inParam.base_filename);
free(inParam.test_filename); free(inParam.test_filename);
if(same) return same ? EXIT_SUCCESS : EXIT_FAILURE;
{
printf("\n***** TEST SUCCEED: Files are the same. *****\n");
return EXIT_SUCCESS;
}
else return EXIT_FAILURE;
} }