C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why is istrstream deprecated?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Branimir Maksimovic
Guest





PostPosted: Mon Nov 22, 2004 9:59 pm    Post subject: Why is istrstream deprecated? Reply with quote



Greetings to all,

I found this class very usefull for parsing input buffers,
mmap-ing files and after that passing pointer and size to this
usefull class. What's wrong with it? I agree that ostrstream is not
that usefull, ostringstream has all advantages,but we really need
istrstream.
Can we un-deprecate istrstream?Any comments?

Bane.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Nov 23, 2004 10:40 am    Post subject: Re: Why is istrstream deprecated? Reply with quote



[email]bmaxa (AT) volomp (DOT) com[/email] (Branimir Maksimovic) wrote in message
news:<88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>...

Quote:
I found this class very usefull for parsing input buffers, mmap-ing
files and after that passing pointer and size to this usefull class.
What's wrong with it? I agree that ostrstream is not that usefull,
ostringstream has all advantages,but we really need istrstream.

Can we un-deprecate istrstream?Any comments?

I use both istrstream and ostrstream a lot for this sort of thing,
formatting and parsing fixed buffers. I find them extremely useful. On
the other hand, a streambuf which does just this is pretty trivial to
write: if you don't need to support seeking, a single call setg or setp
in the constructor suffices.

--
James Kanze GABI Software http://www.gabi-soft.fr
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
Maxim Yegorushkin
Guest





PostPosted: Tue Nov 23, 2004 10:45 am    Post subject: Re: Why is istrstream deprecated? Reply with quote



On 22 Nov 2004 16:59:16 -0500, Branimir Maksimovic <bmaxa (AT) volomp (DOT) com>
wrote:

Quote:
I found this class very usefull for parsing input buffers,
mmap-ing files and after that passing pointer and size to this
usefull class. What's wrong with it? I agree that ostrstream is not
that usefull, ostringstream has all advantages,but we really need
istrstream.

You can do that with std::istream as well, though little coding required:

class mem_input_stream : private std::streambuf, public std::istream
{
mem_input_stream(char* buf, size_t size)
: std::istream(static_cast<std::streambuf*>(this)) // cast for
clarity only
{
this->std::streambuf::setg(buf, buf, buf + size); //
qualification for clarity only
}
};

Quote:
Can we un-deprecate istrstream?Any comments?

Sorry, can't comment on that.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ardonik
Guest





PostPosted: Tue Nov 23, 2004 10:48 am    Post subject: Re: Why is istrstream deprecated? Reply with quote

[In reply to <88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>]

Quote:
Greetings to all,

I found this class very usefull for parsing input buffers,
mmap-ing files and after that passing pointer and size to this
usefull class. What's wrong with it? I agree that ostrstream is not
that usefull, ostringstream has all advantages,but we really need
istrstream.
Can we un-deprecate istrstream?Any comments?

The standard replacements for istrstream and ostrstream are
std::istringstream and std::ostringstream, respectively. You can find
them in the <sstream> header.

--Ardonik

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Tue Nov 23, 2004 10:54 am    Post subject: Re: Why is istrstream deprecated? Reply with quote

In article <88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>, Branimir
Maksimovic <bmaxa (AT) volomp (DOT) com> wrote:

Quote:
Greetings to all,

I found this class very usefull for parsing input buffers,
mmap-ing files and after that passing pointer and size to this
usefull class. What's wrong with it? I agree that ostrstream is not
that usefull, ostringstream has all advantages,but we really need
istrstream.
Can we un-deprecate istrstream?Any comments?


The short ansswer is that it is easy to write a stream buffer class
to do the input from the given contiguous memory area, to be read.

class charbuf:public std::streambuf
{
std::streampos seekpos(std::streampos,std::ios_base::opendir);
std::streampos seekoff(std::streampos,std::ios_base::seekdir,
std::ios_base::opendir);
public:
charbuf(char *p,std::ptrdiff_t size) {setg(p,p,p+size);}
};

class charstream:public std::istream
{
charbuf buf;
public:
charstream(char *p,std::ptrdiff_t size):buf(p,size),std::istream(0)
{
rdbuf(&buf);
}
};

implimentation:

std::streampos charbuf:seekpos(std::streampos pos
,std::ios_base::opendir)
{
char *p = eback() + pos;
if(p>=eback() && p <=egptr())
{
setg(eback(),p,egptr());
return pos;
}
else
return std::streampos(-1);
}

std::streampos charbuf::seekoff(std::streamoff off,
std::ios_base::seekdir dir, std::ios_base::opendir)
{
char *p=0;
switch(dir)
{
case std::ios_base::beg: p=eback() + off;break;
case std::ios_base::cur: p=gptr() +off;break;
case std::ios_base::end:p=egptr+off;break;
default:break;
}
if(p>=eback() && p <= egptr())
{
setg(eback(),p,egptr();
return std::streampos(p-egptr());
}
else
return std::streampos(-1);
}

now a stream class to use this buffer

class charstream:public std::istream
{
charbuf buf;
public:
charstream(char *p,std::ptrdiff_t size):buf(p,size),std::istream(0)
{
rdbuf(&buf);
}
};

charstream now should be able to read data and seek anywhere in the
contiguous memory area and fail to seek outside this area. It will not
be able to replace a char in the stream with a different char but it
should be able to putback a char read into the stream.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dietmar_kuehl@yahoo.com
Guest





PostPosted: Wed Nov 24, 2004 9:31 am    Post subject: Re: Why is istrstream deprecated? Reply with quote

Branimir Maksimovic wrote:
Quote:
Can we un-deprecate istrstream?Any comments?

The whole set of 'strstream' classes carries to much baggage: it
tries to do several things at once and effectively fails in all.
For your use, where you already have an array of characters which
you want to use with an input stream, you just need a very simple
stream buffer:

Quote:
struct membuf: std::streambuf {
membuf(char* beg, char* end) { setg(beg, beg, end); }
};

.... and possibly wrap construction of this stream buffer with a
suitable input stream class:

Quote:
struct imemstream: private virtual membuf, public std::istream {
imemstream(char* beg, char* end):
membuf(beg, end), std::istream(this)
{}
};

Given this trivial alternative to an essentially broken class, I
think it is appropriate to leave the whole strstream group of
classes as deprecated.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Dennett
Guest





PostPosted: Wed Nov 24, 2004 7:27 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote

Ardonik wrote:

Quote:
[In reply to <88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>]

Greetings to all,

I found this class very usefull for parsing input buffers,
mmap-ing files and after that passing pointer and size to this
usefull class. What's wrong with it? I agree that ostrstream is not
that usefull, ostringstream has all advantages,but we really need
istrstream.
Can we un-deprecate istrstream?Any comments?

The standard replacements for istrstream and ostrstream are
std::istringstream and std::ostringstream, respectively. You can find
them in the <sstream> header.

But they're not replacements; they don't allow using a pre-existing
block of memory as a stream.

-- James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Nov 24, 2004 11:57 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote

Carl Barron <cbarron413 (AT) adelphia (DOT) net> wrote

Quote:
In article <88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>, Branimir
Maksimovic <bmaxa (AT) volomp (DOT) com> wrote:

I found this class very usefull for parsing input buffers, mmap-ing
files and after that passing pointer and size to this usefull
class. What's wrong with it? I agree that ostrstream is not that
usefull, ostringstream has all advantages,but we really need
istrstream.
Can we un-deprecate istrstream?Any comments?

The short ansswer is that it is easy to write a stream buffer class
to do the input from the given contiguous memory area, to be read.

It's not that difficult, but given that it is a frequent problem, and is
easily supported on all platforms, should we have to?

Or put another way, do we really need hundreds of different memory
buffer streams, varying only in name and what they support.

--
James Kanze GABI Software http://www.gabi-soft.fr
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
Seungbeom Kim
Guest





PostPosted: Wed Nov 24, 2004 11:58 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote

[email]dietmar_kuehl (AT) yahoo (DOT) com[/email] wrote:
Quote:
For your use, where you already have an array of characters which
you want to use with an input stream, you just need a very simple
stream buffer:

| struct membuf: std::streambuf {
| membuf(char* beg, char* end) { setg(beg, beg, end); }
| };

... and possibly wrap construction of this stream buffer with a
suitable input stream class:

| struct imemstream: private virtual membuf, public std::istream {
| imemstream(char* beg, char* end):
| membuf(beg, end), std::istream(this)
| {}
| };

Given this trivial alternative to an essentially broken class, I
think it is appropriate to leave the whole strstream group of
classes as deprecated.

Wouldn't it have been better to make them part of the standard,
than to make everyone reinvent the wheel and understand it?
I see several people's answers in this thread, all of which
are a little bit different from the others. This is a burden
both for writers of the code and for readers.

Or is there a proposal for such a thing in progress?

--
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
Ardonik
Guest





PostPosted: Thu Nov 25, 2004 8:46 am    Post subject: Re: Why is istrstream deprecated? Reply with quote

[In reply to <TyVod.168886$hj.17966@fed1read07>]

Quote:
Ardonik wrote:

[In reply to <88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>]

Greetings to all,

I found this class very usefull for parsing input buffers, mmap-ing
files and after that passing pointer and size to this usefull class.
What's wrong with it? I agree that ostrstream is not that usefull,
ostringstream has all advantages,but we really need istrstream.
Can we un-deprecate istrstream?Any comments?

The standard replacements for istrstream and ostrstream are
std::istringstream and std::ostringstream, respectively. You can find
them in the <sstream> header.

But they're not replacements; they don't allow using a pre-existing
block of memory as a stream.

What's wrong with something like this?

char buf[MAX];
std::string s(buf, MAX);
std::istringstream in(s);

Yeah, the string's a copy of buf. If that's too much of a hardship, you
could always use placement new to put the string directly inside the
memory block. Easier than writing a custom streambuf class, in my
opinion, but there's obviously more than one way to skin a cat.

--Ardonik

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Thu Nov 25, 2004 8:51 am    Post subject: Re: Why is istrstream deprecated? Reply with quote

[email]dietmar_kuehl (AT) yahoo (DOT) com[/email] wrote:

Quote:
For your use, where you already have an array of characters which
you want to use with an input stream, you just need a very simple
stream buffer:

| struct membuf: std::streambuf {
| membuf(char* beg, char* end) { setg(beg, beg, end); }
| };

... and possibly wrap construction of this stream buffer with a
suitable input stream class:

| struct imemstream: private virtual membuf, public std::istream {
| imemstream(char* beg, char* end):
| membuf(beg, end), std::istream(this)
| {}

Would it be safe to rewrite this constructor as
imemstream(char const* beg, char const* end)
: membuf(const_cast<char*>(beg), const_cast<char*>(end)),
std::istream(this) {}

i.e. can I rely on stream_buf not trying to modify the passed buffer
when it is used only for reading, or will I have to copy the contents
of a 'const' buffer to be sure it works?

Quote:
| };

By the way, additional constructors as
imemstream(char const* beg, std::size_t len)
and
template<std::size_t Len> imemstream(char const (&array)[Len])
(with or without the 'const') could be handy, too.

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Fri Nov 26, 2004 1:11 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote


"Ardonik" <clcppm-poster (AT) this (DOT) is.invalid> schrieb im Newsbeitrag
news:pan.2004.11.25.06.35.12.813645 (AT) sbcglobal (DOT) net...
Quote:
[In reply to <TyVod.168886$hj.17966@fed1read07>]

Ardonik wrote:

[In reply to <88cdcbb3.0411220613.26dec52c (AT) posting (DOT) google.com>]

Greetings to all,

I found this class very usefull for parsing input buffers, mmap-ing
files and after that passing pointer and size to this usefull class.
What's wrong with it? I agree that ostrstream is not that usefull,
ostringstream has all advantages,but we really need istrstream.
Can we un-deprecate istrstream?Any comments?

The standard replacements for istrstream and ostrstream are
std::istringstream and std::ostringstream, respectively. You can find
them in the <sstream> header.

But they're not replacements; they don't allow using a pre-existing
block of memory as a stream.

What's wrong with something like this?

char buf[MAX];
std::string s(buf, MAX);
std::istringstream in(s);

Yeah, the string's a copy of buf. If that's too much of a hardship, you
could always use placement new to put the string directly inside the
memory block.

Could you please elaborate?


Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dietmar_kuehl@yahoo.com
Guest





PostPosted: Fri Nov 26, 2004 1:12 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote

Seungbeom Kim wrote:
Quote:
Wouldn't it have been better to make them part of the standard,
than to make everyone reinvent the wheel and understand it?

Maybe it would. Apparently nobody made a proposal for a simple
memory stream buffer. Personally, I would consider the ten lines of
code to be sufficiently trivial. Of course, I know how stream
buffers work which is not necessarily true for everybody who wants
to use streams.

Quote:
I see several people's answers in this thread, all of which
are a little bit different from the others. This is a burden
both for writers of the code and for readers.

I have seen three different implementations, two of them doing
essentially the same (Maxim's and mine, although Maxim's version
uses a neat shortcut and avoids one class) and one also allowing
seeks on the created stream buffer (Carl's version). This roughly
covers the design space for reading from a fixed memory area. The
only additional opportunity I see is supporting an initial putback
area although this already goes into rather special needs. If
reading and writing is to be supported for the same area, things
may be more complicated but, again, I would consider this a rather
special need where it is likely that any standard version does not
address them exactly anyway.

Personally, I don't think everything has to be in the standard
library: Things which can't be done otherwise or which are used
by a majority of the users should qualify. Arguably, not
everything in the current standard library or in the library TR
qualifies to these rules but I would still use them as a general
guideline. I think a fixed memory stream does not fit either of
these requirements and implementations are easily located on the
Internet. Actually, I think something like Perl's CPAN is a good
approach (although sometimes I get a little bit annoyed by mostly
trivial packages requiring loads of other even more trivial
packages). Boost goes somewhat into the direction.

Quote:
Or is there a proposal for such a thing in progress?

I'm not aware of any. You might write one if you think this is an
important component. However, I think we have more important
problems with the IOStream library (e.g. the lack of an error
reporting mechanism which provides a precise indication of what
went wrong rather than just an indication that something went
wrong). Also, stream buffers is a form of a special case sequence
abstraction originating from times when there was no common
sequence abstraction used by the standard library. Maybe it would
be more reasonable to think into this direction rather than
extending the special case stuff. Boost has a library simplifying
stream buffer creation. This may also be an approach.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nicola Musatti
Guest





PostPosted: Fri Nov 26, 2004 4:37 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote

Ardonik <clcppm-poster (AT) this (DOT) is.invalid> wrote

[...]
Quote:
What's wrong with something like this?

char buf[MAX];
std::string s(buf, MAX);
std::istringstream in(s);

Yeah, the string's a copy of buf. If that's too much of a hardship, you
could always use placement new to put the string directly inside the
memory block. Easier than writing a custom streambuf class, in my
opinion, but there's obviously more than one way to skin a cat.

That might get the string's internal buffer *pointer* into the memory
block; not the buffer itself, which is dynamically allocated.

Cheers,
Nicola Musatti

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dietmar_kuehl@yahoo.com
Guest





PostPosted: Fri Nov 26, 2004 4:38 pm    Post subject: Re: Why is istrstream deprecated? Reply with quote

Falk Tannhäuser wrote:
Quote:
Would it be safe to rewrite this constructor as
imemstream(char const* beg, char const* end)
: membuf(const_cast<char*>(beg), const_cast<char*>(end)),
std::istream(this) {}

I don't think that any of the functions operating on the get area
writes a character (not even 'sputback()' although it looks much
like it does: it merely checks that the characters match). However,
I don't see any restriction prohibiting an implementation to actually
write characters either. That is, it is unlikely to cause trouble but
on the other hand I don't think it is guaranteed to work. For example,
an optimized version of 'getline()' may be granted access to the
buffer (the standard library is free to bypass access restrictions
for its own classes and at least my implementation makes use of such
bypasses but only in a reading form) and it may change e.g. the
last character in the buffer to 'n' as a sentinel such that the
inner loop is not something like

for (char* it = gptr(), * end = egptr();
it != end && *it != 'n'; ++it)
....

but just

if (gptr() != egptr())
{
char save = egptr()[-1];
egptr()[-1] = 'n';
for (char* it = gptr(); *it != 'n'; ++it)
....
egptr()[-1] = save;
}

This scenario is not really that unrealistic that I would bet on it.

Quote:
By the way, additional constructors as
imemstream(char const* beg, std::size_t len)
and
template<std::size_t Len> imemstream(char const (&array)[Len])
(with or without the 'const') could be handy, too.

Of course, I just demonstrated how a simple version could look like.
In a real world I would probably implement a template in the first
place.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.