473,789 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5763
"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::serializ ation 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********@hot mail.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.tas k.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******@pegasu s.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***********@y ahoo.com (Dietmar Kuehl) wrote in message news:<5b******* *************** ****@posting.go ogle.com>...
ma******@pegasu s.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
12059
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 this. I have understood that there is a relation between streambuf and iostream, where one should be able to extend streambuf and override the underflow/overflow functions (and do send()/recv() for example).
12
3217
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 to stdout/stderr, I'd like it to write to stdout, to a file, and/or call a logging function. So my output function might look something like this: OutputFacility& OutputFacility::put(const std::string& s) { cout << s; // print to stdout
3
2187
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
1949
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... class TWFileBuf : public std::streambuf { private: bool Open( name ) {/* debug printf - does show up */} protected:
9
2720
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 looked at the C++ standard (ISO/IEC 14882) from the web to get a grip on the relationship between ostream methods and streambuf methods: flush(), operator<<(), sputc(),
2
2770
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 encryption, currently it is doing "nothing")
7
7029
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 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().
4
6415
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. My code looks as follows. #include <iostream>
3
4733
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
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.