|
Boost Users : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2003-06-12 02:05:00
On Tuesday, June 10, 2003, at 9:24 PM, Daryle Walker wrote:
[SNIP]
> Note in the "if" statement, I had the construction in it. However,
> neither one of my compilers took it! Moving the constructor call
> worked, but I thought my original method should be accepted.
>
> One compiler finished after the above 'fix'. The other (a variant of
> GCC 3.1) complained about missing symbols at link-time; it could not
> create the global objects hidden in Boost.CRC. (I haven't worked on
> the code in a long time; there could be quirks on compilers written
> after Boost.CRC.)
>
> When I ran the program under the compiler that worked, the "readsome"
> function always returned zero! There were no suitable substitutes;
> the "read" function doesn't tell you how many characters it read (bad
> if the last block isn't full) and the multi-character "get" functions
> use delimiters. So this program is stuck at single-character reads at
> the moment. Maybe I should switch to the stream-buffer method you
> used.
>
> Can you try this out to make sure the problems aren't just mine?
Note to everyone: don't write code when you're half-asleep.
1. The "if" statement problem: object definitions inside the
if-statement's parentheses is OK only for assignment initialization.
if ( Type Object = Expression ) // is allowed
if ( Type Object( Constructor Args ) ) // is banned!
2. Don't know why GCC is having link problems. Help! (BTW, I'm using
an August 2002 eMac with Mac OS X 10.2.6 running Project Builder 2.0.1
with Apple-modified GCC 3.1)
3. While reading a C++ IOStreams book (for the heck of it), I
encountered the "gcount" method. Now the code uses that and the "read"
method.
The updated sample is attached. Would this code be OK to include as an
additional example in the Boost archive?
Daryle
----------
// Boost CRC example program file ------------------------------------------//
// (C) Copyright Daryle Walker 2003. Permission to copy, use, modify, sell
// and distribute this software is granted provided this copyright notice
// appears in all copies. This software is provided "as is" without express or
// implied warranty, and with no claim as to its suitability for any purpose.
// Revision History
// 12 Jun 2003 Initial version (Daryle Walker)
#include <boost/crc.hpp> // for boost::crc_32_type
#include <cstdlib> // for EXIT_SUCCESS, EXIT_FAILURE
#include <exception> // for std::exception
#include <fstream> // for std::ifstream
#include <ios> // for std::ios_base, etc.
#include <iostream> // for std::cerr, std::cout
#include <ostream> // 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;
// 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 )
{
char buffer[ buffer_size ];
std::streamsize len;
do
{
ifs.read( buffer, buffer_size );
len = ifs.gcount();
result.process_bytes( buffer, len );
} while ( ifs && (buffer_size == len) );
}
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 )
{
std::cerr << "Found an exception with '" << e.what() << "'." << std::endl;
return EXIT_FAILURE;
}
catch ( ... )
{
std::cerr << "Found an unknown exception." << std::endl;
return EXIT_FAILURE;
}
[Non-text portions of this message have been removed]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net