 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Aug 23, 2006 7:11 am Post subject: streambuf in binary mode |
|
|
Hello all,
I'm working on writing my own streambuf classes (to use in my custom
ostream/isteam classes that will handle reading/writing data to a
mmap'd file).
When reading from the mmap file, I essentially have a char buffer in my
streambuf class, that I'm registering with setp(). on an overflow()
call, I simply copy the contents of the buffer into the mmap'd file via
memcpy().
If I want to use this to write binary data via the streambuf classes
ie, are there any special considerations I need to be aware of in my
streambuf classes? Do I need to set any special flags to indicate that
the data is in binary mode perhaps? Any special precautions I need to
take, in overflow(int_type) for example?
My setup seems to work with binary data *MOST* of the time, however
there are rare occasions when there is inconsistency with the data i'm
writing and reading...
Any advice, comments would be much appreciated
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Wed Aug 23, 2006 6:21 pm Post subject: Re: streambuf in binary mode |
|
|
smith4894 (AT) excite (DOT) com wrote:
| Quote: | I'm working on writing my own streambuf classes (to use in my custom
ostream/isteam classes that will handle reading/writing data to a
mmap'd file).
|
You are doing it with streambuf because you use iostream formatted
input/output (<<,>>), don't you?
If you don't, you could use a much simpler interface, in order not to
deal with all the complexity of implementing std::streambuf. Something
like that:
struct stream
{
virtual ssize_t read(void*, size_t) = 0;
virtual ssize_t write(void const*, size_t) = 0;
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Aug 23, 2006 7:25 pm Post subject: Re: streambuf in binary mode |
|
|
smith4894 (AT) excite (DOT) com wrote:
| Quote: | If I want to use this to write binary data via the streambuf classes
ie, are there any special considerations I need to be aware of in my
streambuf classes? Do I need to set any special flags to indicate that
the data is in binary mode perhaps? Any special precautions I need to
take, in overflow(int_type) for example?
You do know the difference between "binary" mode in an iostream |
and "formatted?".
All the binary flag does on the stream is turn off whatever line
end processing might be taking place (on Windows, \r\n -> \n
conversion or vice versa). Formatted refers to using he
functions like << and >> that convert the textual representation
to and from the operand types. Putting the stream in binary
momde doesn't change that. To "binary representations" of
these things, you use the unformatted I/O functions read
and write that just write an specified number of characters
to/from the stream.
All that being said, it is handled in the iostream base classes
and is totally transparent to the stream buffers. The stream
buffers just see a certain number of charT characters that
have already been formatted/new-line mapped.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Aug 23, 2006 11:37 pm Post subject: Re: streambuf in binary mode |
|
|
Ron Natalie wrote:
| Quote: | smith4894 (AT) excite (DOT) com wrote:
If I want to use this to write binary data via the streambuf
classes ie, are there any special considerations I need to
be aware of in my streambuf classes? Do I need to set any
special flags to indicate that the data is in binary mode
perhaps? Any special precautions I need to take, in
overflow(int_type) for example?
You do know the difference between "binary" mode in an
iostream and "formatted?".
All the binary flag does on the stream is turn off whatever
line end processing might be taking place (on Windows, \r\n -
\n conversion or vice versa). Formatted refers to using he
functions like << and >> that convert the textual
representation to and from the operand types. Putting the
stream in binary momde doesn't change that. To "binary
representations" of these things, you use the unformatted I/O
functions read and write that just write an specified number
of characters to/from the stream.
All that being said, it is handled in the iostream base
classes and is totally transparent to the stream buffers. The
stream buffers just see a certain number of charT characters
that have already been formatted/new-line mapped.
|
No. The iostream base classes are totally unaware of the
ios::binary flag, except that they declare it. In fact,
ios::binary is purely a streambuf issue; in the standard
library, the only class that uses it (other than to forward it)
is std::basic_filebuf.
Whether a user defined streambuf should use it or not depends on
what it does, but I suspect that cases where it should are very
rare. All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Thu Aug 24, 2006 6:55 pm Post subject: Re: streambuf in binary mode |
|
|
kanze wrote:
| Quote: | Whether a user defined streambuf should use it [ios::binary]
or not depends on what it does, but I suspect that cases
where it should are very rare.
Agreed. |
| Quote: | All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.
Surely whenever the streambuf needs to map between an internal format, |
and an external binary format?
Examples I can think of are:
- writing multiline text into a windows text box (where you need \n
-> \r\n conversion)
- writing text an HTTP GET request (which again I think needs \n >
CR, LF conversion)
- writing text to some network protocol which needs lines terminated
by ASCII LF (some Mac compilers use(d) '\n'==ASCII CR because that
allows ios::text to be a no-op for filebuf).
But, I agree it is rare.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Thu Aug 24, 2006 11:14 pm Post subject: Re: streambuf in binary mode |
|
|
Martin Bonner wrote:
| Quote: | kanze wrote:
All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.
Surely whenever the streambuf needs to map between an internal format,
and an external binary format?
Examples I can think of are:
- writing multiline text into a windows text box (where you need \n
-> \r\n conversion)
- writing text an HTTP GET request (which again I think needs \n
CR, LF conversion)
- writing text to some network protocol which needs lines terminated
by ASCII LF (some Mac compilers use(d) '\n'==ASCII CR because that
allows ios::text to be a no-op for filebuf).
But, I agree it is rare.
|
If all or most of the rare cases where a streambuf class other than
basic_filebuf is needed are concerned with which character sequence to
use for end-of-line, maybe we could make it a parameter for a common
class so that we didn't have to reinvent the wheel for each case.
Has there been any discussion or proposal in the past?
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|