473,406 Members | 2,390 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,406 software developers and data experts.

my own streams

I would like to implement my own i/o streams for reading from my own
dataformat. Is it possible to derive one or more classes from
istream,ostream or iostream to do this? When member function would I then
have to implement?

Thanks!

--
Lasse

Jul 19 '05 #1
7 2589

"Lasse Skyum" <no spam> wrote in message
news:3f***********************@dread16.news.tele.d k...
I would like to implement my own i/o streams for reading from my own
dataformat.
I can't know for sure without more elaboration from you,
but I suspect what you're wanting to do doesn't need the
creation of a 'custom' stream, but only of custom stream
inserters/extractors.

Ask if you want an example.
Is it possible to derive one or more classes from
istream,ostream or iostream to do this?
Such 'custom' streams are usually implemented by inheriting
from stream buffer classes, not stream classes.
When member function would I then
have to implement?


Whichever ones you want to support.

Much good information about this is in this great book:
http://www.langer.camelot.de/iostreams.htm

-Mike
Jul 19 '05 #2

I can't know for sure without more elaboration from you,


Okay, it because I've made some functions that loads scripts, graphics and
geometry from streams. Currently I feed the functions fstream's but because
I havde decided to put all my files into a compressed collection-file I
can't do that anymore.

My idea was to uncompress it to a chunk of memory and make an istream that
read from it...

--
Lasse
Jul 19 '05 #3

"Lasse Skyum" <no spam> wrote in message
news:3f***********************@dread16.news.tele.d k...

I can't know for sure without more elaboration from you,
Okay, it because I've made some functions that loads scripts, graphics and
geometry from streams. Currently I feed the functions fstream's but

because I havde decided to put all my files into a compressed collection-file I
can't do that anymore.

My idea was to uncompress it to a chunk of memory and make an istream that
read from it...


Why not define a custom extractor which does the decompress,
and stores the data wherever you need it?

-Mike
Jul 19 '05 #4
Why not define a custom extractor which does the decompress,
and stores the data wherever you need it?


Hi Mike,

I'm sure that would be a verry clever way to do it... unfortunatly I don't
understand what you mean by "custom extractor" ... is that a C++ STL thing?
(I'm verry new to STL)

My plan is now just to make my own CIStream, COStream, CIOStream with
similar functionality so I can easily switch over. Just thought I would
stick to standard C++ to make it more "clean" and reusable...

--
Lasse
Jul 19 '05 #5
Lasse Skyum wrote in
news:3f***********************@dread12.news.tele.d k:
Why not define a custom extractor which does the decompress,
and stores the data wherever you need it?
Hi Mike,

I'm sure that would be a verry clever way to do it... unfortunatly I
don't understand what you mean by "custom extractor" ... is that a C++
STL thing? (I'm verry new to STL)


Not really, STL isn't an offical term, when used sensibly is means
the standard containers, iterators and algorithms libraries, e.g.
std::vector< T >, std::set< T >, and std::sort().

Here is a class Test with its own extractor an inserter.

#include <iostream>
#include <ostream>
#include <iomanip>
#include <sstream>

class Test
{
int a, b;
public:
Test() {}
Test( int aa, int bb ) : a( aa ), b ( bb ) {}
friend std::istream &operator >> (
std::istream &is,
Test &rhs
);
friend std::ostream &operator << (
std::ostream &os,
Test const &rhs
)
{
return os << "(" << rhs.a << "," << rhs.b << ")";
}

};

std::istream &operator >> (
std::istream &is,
Test &rhs
)
{
char c;
if ( !(is >> c) ) return is;
if ( c != '(' )
{
is.setstate( std::ios_base::failbit );
return is;
}
if ( !(is >> rhs.a) ) return is;
if ( !(is >> c ) ) return is;
if ( c != ',' )
{
is.setstate( std::ios_base::failbit );
return is;
}
if ( !(is >> rhs.b) ) return is;
if ( !(is >> c ) ) return is;
if ( c != ')' )
{
is.setstate( std::ios_base::failbit );
return is;
}
return is;
}
int main()
{
using namespace std;

Test a( 1, 2), b( 3, 4 ), c;
stringstream ss;
ss << a << " " << b << " %";

if (ss >> c) cerr << "extracted c\n";
else cerr << "didn't extract c\n";

cerr << a << ", " << b << ", " << c << "\n";

if (ss >> c) cerr << "extracted c\n";
else cerr << "didn't extract c\n";

cerr << a << ", " << b << ", " << c << "\n";
if (ss >> c) cerr << "extracted c\n";
else cerr << "didn't extract c\n";

if ( ss.fail() ) cerr << "ss failbit set\n";
else cerr << "ss failbit clear\n";
}

My plan is now just to make my own CIStream, COStream, CIOStream with
similar functionality so I can easily switch over. Just thought I
would stick to standard C++ to make it more "clean" and reusable...


Providing your own stream inserters (operator <<) and extractors
(operator >>) is "clean" and reusable..
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #6
Thanks for the example Rob, I'll be trying to understand it now :-)
Why not define a custom extractor which does the decompress,
and stores the data wherever you need it?


Hi Mike,

I'm sure that would be a verry clever way to do it... unfortunatly I
don't understand what you mean by "custom extractor" ... is that a C++
STL thing? (I'm verry new to STL)


Not really, STL isn't an offical term, when used sensibly is means
the standard containers, iterators and algorithms libraries, e.g.
std::vector< T >, std::set< T >, and std::sort().

Here is a class Test with its own extractor an inserter.

#include <iostream>
#include <ostream>
#include <iomanip>
#include <sstream>

class Test
{
int a, b;
public:
Test() {}
Test( int aa, int bb ) : a( aa ), b ( bb ) {}
friend std::istream &operator >> (
std::istream &is,
Test &rhs
);
friend std::ostream &operator << (
std::ostream &os,
Test const &rhs
)
{
return os << "(" << rhs.a << "," << rhs.b << ")";
}

};

std::istream &operator >> (
std::istream &is,
Test &rhs
)
{
char c;
if ( !(is >> c) ) return is;
if ( c != '(' )
{
is.setstate( std::ios_base::failbit );
return is;
}
if ( !(is >> rhs.a) ) return is;
if ( !(is >> c ) ) return is;
if ( c != ',' )
{
is.setstate( std::ios_base::failbit );
return is;
}
if ( !(is >> rhs.b) ) return is;
if ( !(is >> c ) ) return is;
if ( c != ')' )
{
is.setstate( std::ios_base::failbit );
return is;
}
return is;
}
int main()
{
using namespace std;

Test a( 1, 2), b( 3, 4 ), c;
stringstream ss;
ss << a << " " << b << " %";

if (ss >> c) cerr << "extracted c\n";
else cerr << "didn't extract c\n";

cerr << a << ", " << b << ", " << c << "\n";

if (ss >> c) cerr << "extracted c\n";
else cerr << "didn't extract c\n";

cerr << a << ", " << b << ", " << c << "\n";
if (ss >> c) cerr << "extracted c\n";
else cerr << "didn't extract c\n";

if ( ss.fail() ) cerr << "ss failbit set\n";
else cerr << "ss failbit clear\n";
}

My plan is now just to make my own CIStream, COStream, CIOStream with
similar functionality so I can easily switch over. Just thought I
would stick to standard C++ to make it more "clean" and reusable...


Providing your own stream inserters (operator <<) and extractors
(operator >>) is "clean" and reusable..
Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 19 '05 #7
Rob Williscroft <rt*@freenet.REMOVE.co.uk> writes:
Lasse Skyum wrote in
news:3f***********************@dread12.news.tele.d k:
My plan is now just to make my own CIStream, COStream, CIOStream with
similar functionality so I can easily switch over. Just thought I
would stick to standard C++ to make it more "clean" and reusable...


Providing your own stream inserters (operator <<) and extractors
(operator >>) is "clean" and reusable..


Yes, but they only allow for "all-at-once" decompress-and-dumps,
right? For accesses such as compression, or transparent
encoding/decoding from some particular encoding format, I've
found that having custom streams is often handier, as (with text)
I can get text data one line at a time, or use the standard
inserters/extractors; or (with binary) I can grab data a chunk at
a time. I did this sort of thing for reading/writing Base64
transparently. Naturally, it's significantly more work than just
creating inserters and extractors, so you have to decide whether
the extra flexibility is worth the time and effort.

To OP: If you /do/ decide to create youre own streams, then the
main work is in deriving a new streambuf class. After this is
done, you don't really even need to create a stream class, as you
can pass your streambuf class to the constructor (or to the
rdbuf() member function) of the standard stream classes.

Good luck!

--
Micah J. Cowan
mi***@cowan.name
Jul 19 '05 #8

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

Similar topics

8
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? ...
10
by: Roy Smith | last post by:
I've been running some benchmarks to compare streams and stdio performance. I've always suspected stdio was faster, but was astonished to discover how much faster. I timed the following running...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
11
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
2
by: Peter Rilling | last post by:
One nice thing about collections and arrays is that they implement the IEnumerator and IEnumerable interfaces which allow for more then one iterator to walk the list of items without affecting the...
2
by: bonk | last post by:
Hello how do I connect streams in c# ? Imagine the followung scenario: I have a StreamWriter that writes Text to a Stream. How can I tell that Stream to pass that Data to another Stream...
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
2
by: Abhishek | last post by:
what are the STDUPDATE, STDERR, STDOUT and STDIN streams and how does one access these streams in C language. I am aware of the function fprintf(FILE *fp, char * format, char *s) which puts the...
4
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.