compare_raw_files.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_raw_files.c
  28. *
  29. * Created on: 31 August 2011
  30. * Author: mickael
  31. *
  32. * This is equivalent to the UNIX `cmp` command
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38. #include "opj_getopt.h"
  39. typedef struct test_cmp_parameters
  40. {
  41. /** */
  42. char* base_filename;
  43. /** */
  44. char* test_filename;
  45. } test_cmp_parameters;
  46. /*******************************************************************************
  47. * Command line help function
  48. *******************************************************************************/
  49. static void compare_raw_files_help_display(void) {
  50. fprintf(stdout,"\nList of parameters for the compare_raw_files function \n");
  51. fprintf(stdout,"\n");
  52. fprintf(stdout," -b \t REQUIRED \t filename to the reference/baseline RAW image \n");
  53. fprintf(stdout," -t \t REQUIRED \t filename to the test RAW image\n");
  54. fprintf(stdout,"\n");
  55. }
  56. /*******************************************************************************
  57. * Parse command line
  58. *******************************************************************************/
  59. static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
  60. {
  61. size_t sizemembasefile, sizememtestfile;
  62. int index;
  63. const char optlist[] = "b:t:";
  64. int c;
  65. /* Init parameters*/
  66. param->base_filename = NULL;
  67. param->test_filename = NULL;
  68. opj_opterr = 0;
  69. while ((c = opj_getopt(argc, argv, optlist)) != -1)
  70. switch (c)
  71. {
  72. case 'b':
  73. sizemembasefile = strlen(opj_optarg)+1;
  74. free(param->base_filename); /* handle dup option */
  75. param->base_filename = (char*) malloc(sizemembasefile);
  76. strcpy(param->base_filename, opj_optarg);
  77. /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
  78. break;
  79. case 't':
  80. sizememtestfile = strlen(opj_optarg) + 1;
  81. free(param->test_filename); /* handle dup option */
  82. param->test_filename = (char*) malloc(sizememtestfile);
  83. strcpy(param->test_filename, opj_optarg);
  84. /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
  85. break;
  86. case '?':
  87. if ((opj_optopt == 'b') || (opj_optopt == 't'))
  88. fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
  89. else
  90. if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
  91. else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
  92. return 1;
  93. default:
  94. fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
  95. break;
  96. }
  97. if (opj_optind != argc) {
  98. for (index = opj_optind; index < argc; index++)
  99. fprintf(stderr,"Non-option argument %s\n", argv[index]);
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. /*******************************************************************************
  105. * MAIN
  106. *******************************************************************************/
  107. int main(int argc, char **argv)
  108. {
  109. int pos = 0;
  110. test_cmp_parameters inParam;
  111. FILE *file_test=NULL, *file_base=NULL;
  112. unsigned char equal = 0U; /* returns error by default */
  113. /* Get parameters from command line*/
  114. if (parse_cmdline_cmp(argc, argv, &inParam))
  115. {
  116. compare_raw_files_help_display();
  117. goto cleanup;
  118. }
  119. file_test = fopen(inParam.test_filename, "rb");
  120. if (!file_test) {
  121. fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
  122. goto cleanup;
  123. }
  124. file_base = fopen(inParam.base_filename, "rb");
  125. if (!file_base) {
  126. fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
  127. goto cleanup;
  128. }
  129. /* Read simultaneously the two files*/
  130. equal = 1U;
  131. while (equal)
  132. {
  133. unsigned char value_test = 0;
  134. unsigned char eof_test = 0;
  135. unsigned char value_base = 0;
  136. unsigned char eof_base = 0;
  137. /* Read one byte*/
  138. if (!fread(&value_test, 1, 1, file_test)) {
  139. eof_test = 1;
  140. }
  141. /* Read one byte*/
  142. if (!fread(&value_base, 1, 1, file_base)) {
  143. eof_base = 1;
  144. }
  145. /* End of file reached by the two files?*/
  146. if (eof_test && eof_base)
  147. break;
  148. /* End of file reached only by one file?*/
  149. if (eof_test || eof_base)
  150. {
  151. fprintf(stdout,"Files have different sizes.\n");
  152. equal = 0;
  153. }
  154. /* Binary values are equal?*/
  155. if (value_test != value_base)
  156. {
  157. fprintf(stdout,"Binary values read in the file are different %x vs %x at position %d.\n", value_test, value_base, pos);
  158. equal = 0;
  159. }
  160. pos++;
  161. }
  162. if(equal) fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
  163. cleanup:
  164. if(file_test) fclose(file_test);
  165. if(file_base) fclose(file_base);
  166. /* Free Memory */
  167. free(inParam.base_filename);
  168. free(inParam.test_filename);
  169. return equal ? EXIT_SUCCESS : EXIT_FAILURE;
  170. }