Custom streambuf class

  • Thread starter Marcin Kalicinski
  • Start date
M

Marcin Kalicinski

Hi,

I have a set of C-like functions for file access (like fopen, fwrite, fread,
fseek etc.). But I want to access the files using C++ stream, not these
functions. What I probably need to do is to create custom streambuf class
(am I right?).

Is there any free source code or tutorial available on how to do that? I
have googled for it, but I only found partial and untested solutions. Worse,
the solutions were pretty complicated, overriding lots of protected virtual
methods, and calling tons of acronym functions like sbumpc. I would spend
days trying to understand them and I don't have that much time.

The second question is, when I have the streambuf class how do I use it with
streams? Do I also need to create a custom iostream class that uses the
custom streambuf?

any help would be appreciated,
Marcin
 
I

Ivan Vecerina

Marcin Kalicinski said:
I have a set of C-like functions for file access (like fopen, fwrite,
fread,
fseek etc.). But I want to access the files using C++ stream, not these
functions. What I probably need to do is to create custom streambuf class
(am I right?).

Is there any free source code or tutorial available on how to do that? I
have googled for it, but I only found partial and untested solutions.
Worse,
the solutions were pretty complicated, overriding lots of protected
virtual
methods, and calling tons of acronym functions like sbumpc. I would spend
days trying to understand them and I don't have that much time.

I would recommend starting with the samples written by Dietmar Kuehl:
http://www.informatik.uni-konstanz.de/~kuehl/
Dietmar wrote the chapters about iostreams in Josuttis' excellent
"The C++ Standard Library - A Tutorial and Reference".
Reading the (relevant chapters) in the book can help too...

Unfortunately, C++ stream buffers are not simple, and awful acronyms such
as sbumpc are part of the requirements of the standard.
The second question is, when I have the streambuf class how do I use it
with
streams? Do I also need to create a custom iostream class that uses the
custom streambuf?
Yes, you would normally create 3 stream classes: i, o, and io (although it
is also possible to manually bind a streambuf to any iostream instance).
These classes are typically trivial, and you should be able to figure it out
starting from any implementation of i/o-stringstream or i/o-fstream.


I hope this helps,
Ivan
 
J

Jeff Flinn

Marcin Kalicinski said:
Hi,

I have a set of C-like functions for file access (like fopen, fwrite, fread,
fseek etc.). But I want to access the files using C++ stream, not these
functions. What I probably need to do is to create custom streambuf class
(am I right?).

There is a new IOStream library that was recently accepted into the boost
(www.boost.org) library, by Jonathan Turkanis. This significantly simplifies
creating new streams and streambufs.

You can download the pre-review version at
http://home.comcast.net/~jturkanis/iostreams/

Jeff F
 
T

Tom Widmer

Hi,

I have a set of C-like functions for file access (like fopen, fwrite, fread,
fseek etc.). But I want to access the files using C++ stream, not these
functions. What I probably need to do is to create custom streambuf class
(am I right?).

Is there any free source code or tutorial available on how to do that? I
have googled for it, but I only found partial and untested solutions. Worse,
the solutions were pretty complicated, overriding lots of protected virtual
methods, and calling tons of acronym functions like sbumpc. I would spend
days trying to understand them and I don't have that much time.

The second question is, when I have the streambuf class how do I use it with
streams? Do I also need to create a custom iostream class that uses the
custom streambuf?

In addition to Ivan's response, read this:
http://www.langer.camelot.de/IOStreams/Excerpt/excerpt.htm#Main
And then buy the book it is taken from!

The final step is to greatly simplify the whole process by using
Jonathan Turkanis' library:
http://home.comcast.net/~jturkanis/iostreams/
I haven't used it, but hopefully it will be in an official boost
(www.boost.org) release fairly soon.

Tom
 
M

Marcin Kalicinski

The final step is to greatly simplify the whole process by using
Jonathan Turkanis' library:
http://home.comcast.net/~jturkanis/iostreams/
I haven't used it, but hopefully it will be in an official boost
(www.boost.org) release fairly soon.

Thanks! I've looked into the docs and I think it might be exactly what I
needed. I only hope that it is not harder to use than reimplementing the
streambuf from scratch.

Best regards,
Marcin
 
J

Jeff Flinn

Marcin Kalicinski said:
Thanks! I've looked into the docs and I think it might be exactly what I
needed. I only hope that it is not harder to use than reimplementing the
streambuf from scratch.

It's not! I've used it to wrap Microsoft MFC CSharedFile for use with
boost::serialization to support drag/drop and clipboard data transfers. It
was significantly more straightforward than the streambuf derivation
approach.

Jeff F
 
M

ma740988

Marcin Kalicinski said:
Hi,

I have a set of C-like functions for file access (like fopen, fwrite, fread,
fseek etc.). But I want to access the files using C++ stream, not these
functions. What I probably need to do is to create custom streambuf class
(am I right?).

Is there any free source code or tutorial available on how to do that? I
have googled for it, but I only found partial and untested solutions. Worse,
the solutions were pretty complicated, overriding lots of protected virtual
methods, and calling tons of acronym functions like sbumpc. I would spend
days trying to understand them and I don't have that much time.

The second question is, when I have the streambuf class how do I use it with
streams? Do I also need to create a custom iostream class that uses the
custom streambuf?

any help would be appreciated,
Marcin

I'd love to hear how you achieve this. Like you, I'm toiling with an
idea that was presented to me and involves streambuf.
I imagined deriving a new kind of streambuf that would be a bag of
streambufs. There would be one to log to a file and one to put on the
diagnostic output my logging information. I'll also have a logging
window that would be a destination. Then I'd just use my bag 'o
streambufs like std::cerr. Consideration will be given to a special
enumeration or type for controlling the behavior of the bag 'o
streambufs; to turn on and off logging to file, to control the
verbosity of the log, to turn on date-stamping, etc. Ideally, I could
register logging destinations in the bag o' streambufs.

A novel idea, except I'm now perusing texts on streams (purchased
Langer based on recommendation received here) and it appears the
learning curve is 'quite steep'.
 
T

Tom Widmer

I'd love to hear how you achieve this. Like you, I'm toiling with an
idea that was presented to me and involves streambuf.
I imagined deriving a new kind of streambuf that would be a bag of
streambufs. There would be one to log to a file and one to put on the
diagnostic output my logging information. I'll also have a logging
window that would be a destination. Then I'd just use my bag 'o
streambufs like std::cerr. Consideration will be given to a special
enumeration or type for controlling the behavior of the bag 'o
streambufs; to turn on and off logging to file, to control the
verbosity of the log, to turn on date-stamping, etc. Ideally, I could
register logging destinations in the bag o' streambufs.

This is a development of the teebuf. If you do a search for "streambuf
teebuf" you'll see a few implementations. e.g.
http://groups.google.co.uk/groups?&[email protected]
There are quite a few logging libraries floating around as well I
think.
A novel idea, except I'm now perusing texts on streams (purchased
Langer based on recommendation received here) and it appears the
learning curve is 'quite steep'.

Yup, there's quite a lot to learn. Try the boost iostreams library
mentioned in another part of this thread.

Tom
 
D

Dietmar Kuehl

(e-mail address removed) (ma740988) wrote:
[logging to various destinations with streambufs and controlling verbosity
for the respective destination somehow]
A novel idea,

Hardly... Actually, I posted all ingredients to achieve this (teebuf for
the multiple destinatinos; manipulators for controlling output) to most of
the C++ newsgroups several times. I'd guess it can be done with less then 100
lines of code, most of which can be copied from articles I wrote.
 
M

ma740988

(e-mail address removed) (ma740988) wrote:
[logging to various destinations with streambufs and controlling verbosity
for the respective destination somehow]
A novel idea,

Hardly... Actually, I posted all ingredients to achieve this (teebuf for
the multiple destinatinos; manipulators for controlling output) to most of
the C++ newsgroups several times. I'd guess it can be done with less then 100
lines of code, most of which can be copied from articles I wrote.


Interesting articles and posts. Perhaps you could shed light on one
of my primary dilemas.
Consider two classes where the initial 'cout' approach is follows.
First note, the function Print is 'wrapped' in an extern C and the
development environment allows us to 'type' Print - when necessary -
at the console to obtain real time debugging info.

class DFT
{
private:
double dftParamX1;
// lots more
public:
void Print () const { cout << dftParamX1 << endl; }
};
// extern c
extern "C" Print()
{
dft.Print();
}

class FFT
{
private:
double fftParamX1;
// lots more
public:
void Print() const { cout << fftParamX1 << endl; }
};

Now lets add teebuf to the mix.

I'll try to get clarification on a recommendation that involves object
registration which when viewed in this context is ambiguous to me.
For now here's what I understand.
When viewed from the context of teebuf. The 'preferred' approach
involves registering class objects (assume a dft and fft object) with
teebuf. The recommendation calls for "holding" a static array of
class pointer as a member in any private class, where the constructor
registers any new object and the destructor makes the deregistration.
I suppose you could register logging destinations in the bag o'
streambufs.

I'm unsure if I understand/see the value added in modifying a teebuf
to add 'object registration'? Sure in an ideal world I'll be able to
'plug' in a object and go but for some reason, I'm not envisioning
that.

Correct me if I'm wrong but it would appear to me that the ideal
approach in terms of teebuf involves - simply - an instantiation using
the composition approach.

class FFT
{
private:
teebuff dftTeebuff;
public:
FFT ( // initilize dftTeebuff in the constructor )
void DoubleSummation()
{
// use it
}
};

ditto for DFT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top