473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ostringstream / ostrstream problem

Hello,

I am trying to write this simple code:

std::ostringstream s;
s << 1024;
std::cout << s.str() << std::endl;
s.str(""); // <- problem
s << 512;
std::cout << s.str() << std::endl;

The problem is that behind the scene I have a limited implementation of
ostringstream on system that does not support it:

class ostringstream : public ostrstream {
public:
string str() {
char *cstr = ostrstream::str();
freeze(0);
if (cstr == 0) return string();
return string(cstr,pcount());
}
};
I have two options:

- Enhance the limited implementation, which I am not very keen on
- Find a smarter way to do str("") that works for both ostringstream and
my limited ostringstream
Any advices ?
Thanks,
Mathieu
Ps: I tried s.seekp(0) instead of str("") but that leave garbage at the
end...
Jul 23 '05 #1
3 5573
Mathieu Malaterre wrote:
The problem is that behind the scene I have a limited implementation of ostringstream on system that does not support it:

class ostringstream : public ostrstream {
public:
string str() {
char *cstr = ostrstream::str();
freeze(0);
if (cstr == 0) return string();
return string(cstr,pcount());
}
};
Yuk! This code is not guaranteed to work: with the call to 'freeze(0)'
you state that you are done with the returned pointer and your are
not going to access it any further.You actually break this contract.
In addition, 'pcount()' is only supposed to return the number of
characters inserted by the last unformatted output operation (if I
remember correctly). If you insist in using 'std::ostrstream'
underneath, you should terminate the string properly (e.g. using the
manipulator 'std::ends') and then use an appropriate constructor of
'std::string'.
I have two options:

- Enhance the limited implementation, which I am not very keen on
- Find a smarter way to do str("") that works for both ostringstream and my limited ostringstream
I see at last two more options which should be considered:
- Ditch your current compiler and/or standard library and upgrade to a
reasonable one!
- Write your own stream class which just does the right thing: it just
takes a dozen of lines or so.

If all you need is a simple 'std::ostringstream' replacement which may
be
restricted to fixed size buffer but supports 'str(std::string const&)',
this just takes a few lines of code, roughly something like this:

struct stringbuf:
std::streambuf
{
enum { s_size = 1024 };
stringbuf() { setp(m_buf, m_buf + s_size); }
std::string str() const { return std::string(pbase(), pptr()); }
void str(std::string const& s) {
std::copy(s.begin(), s.end(), m_buf);
}
private:
char m_buf[s_size];
};

struct ostringstream:
private virtual stringbuf,
std::ostream
{
ostringstream(): std::ostream(static_cast<std::streambuf*>(this))
{}
};

Well, in a real implementation I would use a more solid approach than
just deriving from the stream buffer but I'm too lazy to show it once
again (I have posted many examples of this in the past).
Any advices ?


My preference would be:
- Use a reasonable standard library.
- Use a appropriate stream buffer like the one above (although you
probably would add error checking and possibly lift the size
restriction).
- I would *not* use either of your options.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2
Dietmar Kuehl wrote:
Mathieu Malaterre wrote:
The problem is that behind the scene I have a limited implementation


of
ostringstream on system that does not support it:

class ostringstream : public ostrstream {
public:
string str() {
char *cstr = ostrstream::str();
freeze(0);
if (cstr == 0) return string();
return string(cstr,pcount());
}
};

Yuk! This code is not guaranteed to work: with the call to 'freeze(0)'
you state that you are done with the returned pointer and your are
not going to access it any further.You actually break this contract.
In addition, 'pcount()' is only supposed to return the number of
characters inserted by the last unformatted output operation (if I
remember correctly). If you insist in using 'std::ostrstream'
underneath, you should terminate the string properly (e.g. using the
manipulator 'std::ends') and then use an appropriate constructor of
'std::string'.

I have two options:

- Enhance the limited implementation, which I am not very keen on
- Find a smarter way to do str("") that works for both ostringstream


and
my limited ostringstream

I see at last two more options which should be considered:
- Ditch your current compiler and/or standard library and upgrade to a
reasonable one!
- Write your own stream class which just does the right thing: it just
takes a dozen of lines or so.

If all you need is a simple 'std::ostringstream' replacement which may
be
restricted to fixed size buffer but supports 'str(std::string const&)',
this just takes a few lines of code, roughly something like this:

struct stringbuf:
std::streambuf
{
enum { s_size = 1024 };
stringbuf() { setp(m_buf, m_buf + s_size); }
std::string str() const { return std::string(pbase(), pptr()); }
void str(std::string const& s) {
std::copy(s.begin(), s.end(), m_buf);
}
private:
char m_buf[s_size];
};

struct ostringstream:
private virtual stringbuf,
std::ostream
{
ostringstream(): std::ostream(static_cast<std::streambuf*>(this))
{}
};

Well, in a real implementation I would use a more solid approach than
just deriving from the stream buffer but I'm too lazy to show it once
again (I have posted many examples of this in the past).

Any advices ?

My preference would be:
- Use a reasonable standard library.
- Use a appropriate stream buffer like the one above (although you
probably would add error checking and possibly lift the size
restriction).
- I would *not* use either of your options.
--


Dietmar,

Here is the patch I am using now:

http://public.kitware.com/cgi-bin/vi...&r2=1.6&r1=1.5

I'll see tomorow if this works on the broken plateforms ITK is building on.

Thanks for your help
Mathieu
Jul 23 '05 #3
Mathieu Malaterre wrote:
Here is the patch I am using now:

http://public.kitware.com/cgi-bin/vi...&r2=1.6&r1=1.5

This stuff uses some rather fishy techiques! I recommend
that you use the approach I have proposed: unless I have
made some blatant and obvious programming errors (I didn't
test the code prior to posting), it is the way stream
buffers are intended to be used. I don't have the standard
C++ library implementation I have done with me but I'm
pretty sure that the basics are the same. Of course, it is
your choice to ignore my advice on IOStreams but you might
want to briefly investigate my background before doing so
(e.g. have a look who translated/extended the IOStream
chapter of Nico's "The C++ Standard Library").
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Als | last post by:
What's the meaning of "freeze" as member of ostringstream class? Is it needed always to call freeze() at the disposal of an ostringstream object? Is it also needed before disposing an istringsteam...
9
by: Charles Prince | last post by:
How do I reuse a ostrstream? So far I have replaced all code that does this "delete <ostrstream>.str()" with "<ostrstream>.freeze(0)"
5
by: Simon Pryor | last post by:
I am having some strange problems using std::ostringstream. The simple stuff works okay, but trying to use: std::ostringstream::str(const std::string&) or:...
1
by: becte | last post by:
I encountered the following code similar to this // some header files static char* func(int i) { ostrstream out; if (i==1) out << "ABCDE"; else if (i==2) out << "123"; else cout << "";
2
by: b83503104 | last post by:
Hi, An old code is using stuff like #include <strstream.h> ostrstream *m_actionStream; and the compiler complained syntax error before `*' token I followed some previous posts and tried...
1
by: tcl | last post by:
Questions on ostrstream. #1) do I have a memory leak as the control exits the scope { ostrstream os; os << "hello world" << endl << ends; }
1
by: GeeBee | last post by:
1) I’m using Borland C++ Builder 1.0 (a very old (but still good) version). 2) I have an application EXE calling a DLL. 3) A function in the DLL receives a parameter defined as "ostream &" , and...
3
by: Generic Usenet Account | last post by:
With the deprecated ostrstream class, when the constructor is invoked without arguments, memory is dynamically allocated. In that case the onus on freeing the memory lies with the user. Typically...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.