M
mc
Hi all,
I've been trying to get it right for the last few days and wasn't 100%
successful. I trying to add functionality to the ifstream class creating an
le_ifstream derived class (see at the end of this message). I also want to
have some cast of casting from ifstream it le_ifstream. Here's what I'm
trying to do:
void foo(ifstream & ifs)
{
le_ifstream le_ifs(ifs); // build/cast the istream to an
le_ifstream
}
So far, I could only make it work if deriving from iostream (see below).
However, I see weird behavior (e.g. seek pointer get out of wack). I read
from a file 200 Kb in size and after 3 calls to readshort(), the
assert(good()) fails (gcount() returns 0), while this works with C calls or
if I read the data directly using the ifstream class.
Thank you in advance.
MC
------------------------------------------------------------------------------
class le_ifstream : public std::iostream
{
public :
inline le_ifstream(ifstream & _Istr) : iostream(_Istr.rdbuf())
{
}
le_ifstream & read(void * _Ptr, size_t _Count)
{
iostream::read((char *)_Ptr, _Count);
assert(gcount() == _Count);
return (*this);
}
inline le_ifstream & readfloat(void * _Ptr)
{
return (readint(_Ptr));
}
le_ifstream & readint(void * _Ptr)
{
char _Buf[4]; static int _One = 1;
iostream::read(_Buf, 4);
assert(good());
*((unsigned int *)_Ptr) = (*((char *)&_One) != 0) ? (*((unsigned int
*)&_Buf)) : ((_Buf[3] << 24) + (_Buf[2] << 16) + (_Buf[1] << 8) + _Buf[0]);
return (*this);
}
le_ifstream & readshort(void * _Ptr)
{
char _Buf[2]; static int _One = 1;
iostream::read(_Buf, 2);
assert(good());
*((unsigned short *)_Ptr) = (*((char *)&_One) != 0) ? (*((unsigned short
*)&_Buf)) : ((_Buf[1] << 8) + _Buf[0]);
return (*this);
}
};
I've been trying to get it right for the last few days and wasn't 100%
successful. I trying to add functionality to the ifstream class creating an
le_ifstream derived class (see at the end of this message). I also want to
have some cast of casting from ifstream it le_ifstream. Here's what I'm
trying to do:
void foo(ifstream & ifs)
{
le_ifstream le_ifs(ifs); // build/cast the istream to an
le_ifstream
}
So far, I could only make it work if deriving from iostream (see below).
However, I see weird behavior (e.g. seek pointer get out of wack). I read
from a file 200 Kb in size and after 3 calls to readshort(), the
assert(good()) fails (gcount() returns 0), while this works with C calls or
if I read the data directly using the ifstream class.
Thank you in advance.
MC
------------------------------------------------------------------------------
class le_ifstream : public std::iostream
{
public :
inline le_ifstream(ifstream & _Istr) : iostream(_Istr.rdbuf())
{
}
le_ifstream & read(void * _Ptr, size_t _Count)
{
iostream::read((char *)_Ptr, _Count);
assert(gcount() == _Count);
return (*this);
}
inline le_ifstream & readfloat(void * _Ptr)
{
return (readint(_Ptr));
}
le_ifstream & readint(void * _Ptr)
{
char _Buf[4]; static int _One = 1;
iostream::read(_Buf, 4);
assert(good());
*((unsigned int *)_Ptr) = (*((char *)&_One) != 0) ? (*((unsigned int
*)&_Buf)) : ((_Buf[3] << 24) + (_Buf[2] << 16) + (_Buf[1] << 8) + _Buf[0]);
return (*this);
}
le_ifstream & readshort(void * _Ptr)
{
char _Buf[2]; static int _One = 1;
iostream::read(_Buf, 2);
assert(good());
*((unsigned short *)_Ptr) = (*((char *)&_One) != 0) ? (*((unsigned short
*)&_Buf)) : ((_Buf[1] << 8) + _Buf[0]);
return (*this);
}
};