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 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 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
"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 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
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/
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?
...
|
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...
|
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...
|
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. ;-)
...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |