Boost logo

Boost :

Subject: Re: [boost] Interest in simple unformatted binary stream I/O?
From: Max Sobolev (macsmr_at_[hidden])
Date: 2011-04-27 07:28:42


On 24.04.2011 16:12, Beman Dawes wrote:
> There have been many requests on Boost and various C++ newsgroups for
> unformatted binary I/O. For example, in 2003 Neal Becker wrote:
>
> I wonder if anyone has code for implementing unformatted I/O?
> What I have in mind is
> for the simple case where the application that reads data knows
> the data types, so
> this is not as complicated as the general marshalling situation.
>
> This proposal provides a very simple solution that works with standard
> library input and output streams. The one caveat is that the stream
> must be opened with filemode std::ios_base::binary to avoid certain
> data values being treated as line endings.
>
> int main()
> {
> fstream f("binary_stream_example.dat",
> std::ios_base::trunc | std::ios_base::in | std::ios_base::out |
> std::ios_base::binary);
>
> int32_t x = 0x01020304;
> int32_t y = 0;
>
> f<< bin(x); // write 4 bytes
> f.seekg(0);
> f>> bin(y); // read 4 bytes
>
> BOOST_ASSERT(x == y);
>
> return 0;
> }
>
> For docs, header, and example code, see
> http://mysite.verizon.net/beman/binary_stream/binary_stream.html
> http://mysite.verizon.net/beman/binary_stream/binary_stream.hpp
> http://mysite.verizon.net/beman/binary_stream/binary_stream_example.cpp
>
> Is there interest in this for Boost?
>
> It seems way too small and simple to be a whole library itself. Are
> there any ideas where it should live and what namespace it should be
> in?
>
> --Beman

For such tasks i use very basic tool in the form of 2 overloaded
function templates named raw_cast<>() that converts from native
representation of a fundamental type (except void) to its raw-byte
representation, and vice versa.

raw_cast<>() implementation (raw_cast.hpp): http://codepad.org/x47pQiNA

code example: http://codepad.org/5y3RPj9r

[output (on a little-endian machine):
67305985 (raw: 1 2 3 4 [little endian]; 4 3 2 1 [big endian])

raw: 0 0 0 0 0 0 f0 3f 7f 0 0 0 58 ff ff ff ff 71 5 0
native: {1 : 127 : X : 0xffffffff : q : 5}
--end]

(Code works as is on linux. On other platforms there should be defined
BYTE_ORDER macro that equals to BIG_ENDIAN or something else; otherwise
machine's byte ordering will be interpreted as little endian.)

___________________
I think the following code (used in raw_cast<>() implementation) isn't
correct:

char const* const begin = reinterpret_cast<char const* const>(&value);
char const* const end = reinterpret_cast<char const* const>(&value) +
sizeof(T);

(Byte representation of any trivial copyable type isn't array actually.)

if so, boost::reverse_iterator<> should be replaced below (in code) to
an iterator without 'past-the-end' semantics

-- 
- Do you speak English? Мужик с глубоким вздохом: - Yes I do. А хули толку?

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk