473,388 Members | 1,382 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,388 software developers and data experts.

Custom streambuf class

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

Jul 22 '05 #1
9 5738
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:ck**********@korweta.task.gda.pl...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
On Wed, 6 Oct 2004 12:18:39 +0200, "Marcin Kalicinski"
<ka****@poczta.onet.pl> wrote:
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/IOStrea...cerpt.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
Jul 22 '05 #3

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:ck**********@korweta.task.gda.pl...
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
Jul 22 '05 #4
> 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
Jul 22 '05 #5

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:ck**********@korweta.task.gda.pl...
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.


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
Jul 22 '05 #6

"Tom Widmer" <to********@hotmail.com> wrote in message:

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 for the plug!

It's just been accepted into boost. Release 1.32 is expected soon; the iostreams
library should be in 1.33.

Jonathan

Jul 22 '05 #7
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message news:<ck**********@korweta.task.gda.pl>...
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'.
Jul 22 '05 #8
On 6 Oct 2004 12:52:14 -0700, ma******@pegasus.cc.ucf.edu (ma740988)
wrote:
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?&s...nnrp1.deja.com
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
Jul 22 '05 #9
di***********@yahoo.com (Dietmar Kuehl) wrote in message news:<5b**************************@posting.google. com>...
ma******@pegasus.cc.ucf.edu (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
Jul 22 '05 #10

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

Similar topics

3
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do...
12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
3
by: Christopher Benson-Manica | last post by:
This is starting to seem ridiculous to me :( #include <streambuf> #include <iostream> class TWFileStream : public std::streambuf { private: char cbuf;
5
by: Christopher Benson-Manica | last post by:
Yes, it's me again, with my dear, dear friends std::streambuf and std::ostream... On the bright side, the code compiles. On the gloomy side, I can't seem to get overflow() or xsputn() called... ...
9
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've...
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
7
by: smith4894 | last post by:
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...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
3
by: Raymond Martineau | last post by:
I have the following code segment for a class intended to split output between cout and a file: class SplitStream : public std::streambuf { std::streambuf *x; public: SplitStream() {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.