compare_dump_files.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * compare_dump_files.c
  28. *
  29. * Created on: 25 juil. 2011
  30. * Author: mickael
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <assert.h>
  37. #include "opj_getopt.h"
  38. typedef struct test_cmp_parameters
  39. {
  40. /** */
  41. char* base_filename;
  42. /** */
  43. char* test_filename;
  44. } test_cmp_parameters;
  45. /*******************************************************************************
  46. * Command line help function
  47. *******************************************************************************/
  48. static void compare_dump_files_help_display(void) {
  49. fprintf(stdout,"\nList of parameters for the compare_dump_files function \n");
  50. fprintf(stdout,"\n");
  51. fprintf(stdout," -b \t REQUIRED \t filename to the reference/baseline dump file \n");
  52. fprintf(stdout," -t \t REQUIRED \t filename to the test dump file image\n");
  53. fprintf(stdout,"\n");
  54. }
  55. /*******************************************************************************
  56. * Parse command line
  57. *******************************************************************************/
  58. static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
  59. {
  60. size_t sizemembasefile, sizememtestfile;
  61. int index;
  62. const char optlist[] = "b:t:";
  63. int c;
  64. /* Init parameters */
  65. param->base_filename = NULL;
  66. param->test_filename = NULL;
  67. opj_opterr = 0;
  68. while ((c = opj_getopt(argc, argv, optlist)) != -1)
  69. switch (c)
  70. {
  71. case 'b':
  72. sizemembasefile = strlen(opj_optarg) + 1;
  73. param->base_filename = (char*) malloc(sizemembasefile);
  74. strcpy(param->base_filename, opj_optarg);
  75. /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
  76. break;
  77. case 't':
  78. sizememtestfile = strlen(opj_optarg) + 1;
  79. param->test_filename = (char*) malloc(sizememtestfile);
  80. strcpy(param->test_filename, opj_optarg);
  81. /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
  82. break;
  83. case '?':
  84. if ( (opj_optopt == 'b') || (opj_optopt == 't') )
  85. fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
  86. else
  87. if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
  88. else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
  89. return 1;
  90. default:
  91. fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
  92. break;
  93. }
  94. if (opj_optind != argc)
  95. {
  96. for (index = opj_optind; index < argc; index++)
  97. fprintf(stderr,"Non-option argument %s\n", argv[index]);
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. /*******************************************************************************
  103. * MAIN
  104. *******************************************************************************/
  105. int main(int argc, char **argv)
  106. {
  107. test_cmp_parameters inParam;
  108. FILE *fbase=NULL, *ftest=NULL;
  109. int same = 0;
  110. char lbase[512];
  111. char strbase[512];
  112. char ltest[512];
  113. char strtest[512];
  114. if( parse_cmdline_cmp(argc, argv, &inParam) == 1 )
  115. {
  116. compare_dump_files_help_display();
  117. goto cleanup;
  118. }
  119. /* Display Parameters*/
  120. printf("******Parameters********* \n");
  121. printf(" base_filename = %s\n"
  122. " test_filename = %s\n",
  123. inParam.base_filename, inParam.test_filename);
  124. printf("************************* \n");
  125. /* open base file */
  126. printf("Try to open: %s for reading ... ", inParam.base_filename);
  127. if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
  128. {
  129. goto cleanup;
  130. }
  131. printf("Ok.\n");
  132. /* open test file */
  133. printf("Try to open: %s for reading ... ", inParam.test_filename);
  134. if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
  135. {
  136. goto cleanup;
  137. }
  138. printf("Ok.\n");
  139. while (fgets(lbase, sizeof(lbase), fbase) && fgets(ltest,sizeof(ltest),ftest))
  140. {
  141. int nbase = sscanf(lbase, "%511[^\r\n]", strbase);
  142. int ntest = sscanf(ltest, "%511[^\r\n]", strtest);
  143. assert( nbase != 511 && ntest != 511 );
  144. if( nbase != 1 || ntest != 1 )
  145. {
  146. fprintf(stderr, "could not parse line from files\n" );
  147. goto cleanup;
  148. }
  149. if( strcmp( strbase, strtest ) != 0 )
  150. {
  151. fprintf(stderr,"<%s> vs. <%s>\n", strbase, strtest);
  152. goto cleanup;
  153. }
  154. }
  155. same = 1;
  156. printf("\n***** TEST SUCCEED: Files are the same. *****\n");
  157. cleanup:
  158. /*Close File*/
  159. if(fbase) fclose(fbase);
  160. if(ftest) fclose(ftest);
  161. /* Free memory*/
  162. free(inParam.base_filename);
  163. free(inParam.test_filename);
  164. return same ? EXIT_SUCCESS : EXIT_FAILURE;
  165. }