crc_example.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Boost CRC example program file ------------------------------------------//
  2. // Copyright 2003 Daryle Walker. Use, modification, and distribution are
  3. // subject to the Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. // See <http://www.boost.org/libs/crc/> for the library's home page.
  6. // Revision History
  7. // 17 Jun 2003 Initial version (Daryle Walker)
  8. #include <boost/crc.hpp> // for boost::crc_32_type
  9. #include <cstdlib> // for EXIT_SUCCESS, EXIT_FAILURE
  10. #include <exception> // for std::exception
  11. #include <fstream> // for std::ifstream
  12. #include <ios> // for std::ios_base, etc.
  13. #include <iostream> // for std::cerr, std::cout
  14. #include <ostream> // for std::endl
  15. // Redefine this to change to processing buffer size
  16. #ifndef PRIVATE_BUFFER_SIZE
  17. #define PRIVATE_BUFFER_SIZE 1024
  18. #endif
  19. // Global objects
  20. std::streamsize const buffer_size = PRIVATE_BUFFER_SIZE;
  21. // Main program
  22. int
  23. main
  24. (
  25. int argc,
  26. char const * argv[]
  27. )
  28. try
  29. {
  30. boost::crc_32_type result;
  31. for ( int i = 1 ; i < argc ; ++i )
  32. {
  33. std::ifstream ifs( argv[i], std::ios_base::binary );
  34. if ( ifs )
  35. {
  36. do
  37. {
  38. char buffer[ buffer_size ];
  39. ifs.read( buffer, buffer_size );
  40. result.process_bytes( buffer, ifs.gcount() );
  41. } while ( ifs );
  42. }
  43. else
  44. {
  45. std::cerr << "Failed to open file '" << argv[i] << "'."
  46. << std::endl;
  47. }
  48. }
  49. std::cout << std::hex << std::uppercase << result.checksum() << std::endl;
  50. return EXIT_SUCCESS;
  51. }
  52. catch ( std::exception &e )
  53. {
  54. std::cerr << "Found an exception with '" << e.what() << "'." << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. catch ( ... )
  58. {
  59. std::cerr << "Found an unknown exception." << std::endl;
  60. return EXIT_FAILURE;
  61. }