From 1258d42a4b2f831f3f174ba24996d36befd2dd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20M=C3=BCller?= Date: Wed, 1 Nov 2017 13:14:11 +0100 Subject: [PATCH] Fixed style of example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While it's certainly nice that C++ allows you to not have {} to encapsulate a function body if that body only consists of a single statement (here: `try{…}`), it is not of any familiarity to C++ coders to omit them. Same goes for putting `main`'s argc/argv arguments on multiple lines. Also, defining a macro PRIVATE_SOMETHING just to use it once one line of code below is a bit of a strange thing to do. So, I got rid of the macro and applied the clang-format style from https://github.com/boost-experimental/vc --- crc_example.cpp | 64 ++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/crc_example.cpp b/crc_example.cpp index a40e61d1..dac973d1 100644 --- a/crc_example.cpp +++ b/crc_example.cpp @@ -18,58 +18,36 @@ #include // for std::cerr, std::cout #include // for std::endl - // Redefine this to change to processing buffer size -#ifndef PRIVATE_BUFFER_SIZE -#define PRIVATE_BUFFER_SIZE 1024 -#endif - -// Global objects -std::streamsize const buffer_size = PRIVATE_BUFFER_SIZE; - +std::streamsize const buffer_size = 1024; // Main program -int -main -( - int argc, - char const * argv[] -) -try -{ - boost::crc_32_type result; - - for ( int i = 1 ; i < argc ; ++i ) - { - std::ifstream ifs( argv[i], std::ios_base::binary ); - - if ( ifs ) - { - do - { - char buffer[ buffer_size ]; - - ifs.read( buffer, buffer_size ); - result.process_bytes( buffer, ifs.gcount() ); - } while ( ifs ); - } - else - { - std::cerr << "Failed to open file '" << argv[i] << "'." - << std::endl; - } +int main(int argc, char const *argv[]) { + try { + boost::crc_32_type result; + + for (int i = 1; i < argc; ++i) { + std::ifstream ifs(argv[i], std::ios_base::binary); + + if (ifs) { + do { + char buffer[buffer_size]; + + ifs.read(buffer, buffer_size); + result.process_bytes(buffer, ifs.gcount()); + } while (ifs); + } else { + std::cerr << "Failed to open file '" << argv[i] << "'." << std::endl; + } } std::cout << std::hex << std::uppercase << result.checksum() << std::endl; return EXIT_SUCCESS; -} -catch ( std::exception &e ) -{ + } catch (std::exception &e) { std::cerr << "Found an exception with '" << e.what() << "'." << std::endl; return EXIT_FAILURE; -} -catch ( ... ) -{ + } catch (...) { std::cerr << "Found an unknown exception." << std::endl; return EXIT_FAILURE; + } }