Skip to content

Commit 1258d42

Browse files
committed
Fixed style of example
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
1 parent 0f271ab commit 1258d42

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

crc_example.cpp

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,36 @@
1818
#include <iostream> // for std::cerr, std::cout
1919
#include <ostream> // for std::endl
2020

21-
2221
// Redefine this to change to processing buffer size
23-
#ifndef PRIVATE_BUFFER_SIZE
24-
#define PRIVATE_BUFFER_SIZE 1024
25-
#endif
26-
27-
// Global objects
28-
std::streamsize const buffer_size = PRIVATE_BUFFER_SIZE;
29-
22+
std::streamsize const buffer_size = 1024;
3023

3124
// Main program
32-
int
33-
main
34-
(
35-
int argc,
36-
char const * argv[]
37-
)
38-
try
39-
{
40-
boost::crc_32_type result;
41-
42-
for ( int i = 1 ; i < argc ; ++i )
43-
{
44-
std::ifstream ifs( argv[i], std::ios_base::binary );
45-
46-
if ( ifs )
47-
{
48-
do
49-
{
50-
char buffer[ buffer_size ];
51-
52-
ifs.read( buffer, buffer_size );
53-
result.process_bytes( buffer, ifs.gcount() );
54-
} while ( ifs );
55-
}
56-
else
57-
{
58-
std::cerr << "Failed to open file '" << argv[i] << "'."
59-
<< std::endl;
60-
}
25+
int main(int argc, char const *argv[]) {
26+
try {
27+
boost::crc_32_type result;
28+
29+
for (int i = 1; i < argc; ++i) {
30+
std::ifstream ifs(argv[i], std::ios_base::binary);
31+
32+
if (ifs) {
33+
do {
34+
char buffer[buffer_size];
35+
36+
ifs.read(buffer, buffer_size);
37+
result.process_bytes(buffer, ifs.gcount());
38+
} while (ifs);
39+
} else {
40+
std::cerr << "Failed to open file '" << argv[i] << "'." << std::endl;
41+
}
6142
}
6243

6344
std::cout << std::hex << std::uppercase << result.checksum() << std::endl;
6445
return EXIT_SUCCESS;
65-
}
66-
catch ( std::exception &e )
67-
{
46+
} catch (std::exception &e) {
6847
std::cerr << "Found an exception with '" << e.what() << "'." << std::endl;
6948
return EXIT_FAILURE;
70-
}
71-
catch ( ... )
72-
{
49+
} catch (...) {
7350
std::cerr << "Found an unknown exception." << std::endl;
7451
return EXIT_FAILURE;
52+
}
7553
}

0 commit comments

Comments
 (0)