📄 istream.cpp
字号:
//
// (c) Yuriy Gorvitovskiy
// for Openh323, www.Openh323.org
//
// Windows CE Port
//
// istream implementation
//
#include <iostream.h>
#include <tchar.h>
#include <stdlibx.h>
#define MAXLONGSIZ 16
#define MAXFLTSIZ 20
#define MAXDBLSIZ 28
#define MAXLDBLSIZ 32
/*******************************************************************************/
istream::istream()
{
x_flags |= ios::skipws;
x_gcount = 0;
_fGline = 0;
}
/*******************************************************************************/
istream::istream(streambuf* _inistbf)
{
init(_inistbf);
x_flags |= ios::skipws;
x_gcount = 0;
_fGline = 0;
}
/*******************************************************************************/
istream::istream(const istream& _istrm)
{
init(_istrm.rdbuf());
x_flags |= ios::skipws;
x_gcount = 0;
_fGline = 0;
}
/*******************************************************************************/
istream::~istream()
{
}
// used by ios::sync_with_stdio()
/*******************************************************************************/
istream& istream::operator=(streambuf * _sbuf)
{
if (delbuf() && rdbuf())
delete rdbuf();
bp = 0;
this->ios::operator=(ios()); // initialize ios members
delbuf(0); // important!
init(_sbuf); // set up bp
x_flags |= ios::skipws; // init istream members too
x_gcount = 0;
return *this;
}
/*******************************************************************************/
int istream::ipfx(int need)
{
lock();
if (need) // reset gcount if unformatted input
x_gcount = 0;
if (state) // return 0 iff error condition
{
state |= ios::failbit; // solves cin>>buf problem
unlock();
return 0;
}
if (x_tie && ((need==0) || (need > bp->in_avail())))
{
x_tie->flush();
}
lockbuf();
if ((need==0) && (x_flags & ios::skipws))
{
eatwhite();
if (state) // eof or error
{
state |= ios::failbit;
unlockbuf();
unlock();
return 0;
}
}
// leave locked ; isfx() will unlock
return 1; // return nz if okay
}
// formatted input functions
/*******************************************************************************/
istream& istream::operator>>(char * s)
{
int c;
unsigned int i, lim;
if (ipfx(0))
{
lim = (unsigned)(x_width-1);
x_width = 0;
if (!s)
{
state |= ios::failbit;
}
else
{
for (i=0; i< lim; i++)
{
c=bp->sgetc();
if (c==EOF)
{
state |= ios::eofbit;
if (!i)
state |= ios::failbit|ios::badbit;
break;
}
else if (_istspace(c))
{
break;
}
else
{
s[i] = (char)c;
bp->stossc(); // advance pointer
}
}
if (!i)
state |= ios::failbit;
else
s[i] = '\0';
}
isfx();
}
return *this;
}
/*******************************************************************************/
int istream::peek()
{
int retval;
if (ipfx(1))
{
retval = (bp->sgetc());
isfx();
}
else
retval = EOF;
return retval;
}
/*******************************************************************************/
istream& istream::putback(char c)
{
if (good())
{
lockbuf();
if (bp->sputbackc(c)==EOF)
{
clear(state | ios::failbit);
}
unlockbuf();
}
return *this;
}
/*******************************************************************************/
int istream::sync()
{
int retval;
lockbuf();
if ((retval=bp->sync())==EOF)
{
clear(state | (ios::failbit|ios::badbit));
}
unlockbuf();
return retval;
}
/*******************************************************************************/
void istream::eatwhite()
{
int c;
lockbuf();
c = bp->sgetc();
for ( ; ; )
{
if (c==EOF)
{
clear(state | ios::eofbit);
break;
}
if (isspace(c))
{
c = bp->snextc();
}
else
{
break;
}
}
unlockbuf();
}
/*******************************************************************************/
istream& istream::operator>>(streambuf* _sbuf)
{
int c;
if (ipfx(0))
{
while ((c=bp->sbumpc())!=EOF)
{
if (_sbuf->sputc(c)==EOF)
{
state |= ios::failbit;
}
}
isfx();
}
return *this;
}
// unformatted input functions
/*******************************************************************************/
istream& istream::get( streambuf& sbuf, char delim)
{
int c;
if (ipfx(1)) // resets x_gcount
{
while ((c = bp->sgetc())!=delim)
{
if (c==EOF) // stop if EOF encountered
{
state |= ios::eofbit;
break;
}
bp->stossc(); // advance get pointer
x_gcount++; // and increment count
if (sbuf.sputc(c)==EOF)
state |= ios::failbit;
}
isfx();
}
return *this;
}
/*******************************************************************************/
istream& istream::seekg(streampos _strmp)
{
lockbuf();
if (bp->seekpos(_strmp, ios::in)==EOF)
{
clear(state | failbit);
}
unlockbuf();
return(*this);
}
/*******************************************************************************/
istream& istream::seekg(streamoff _strmf, seek_dir _sd)
{
lockbuf();
if (bp->seekoff(_strmf, _sd, ios::in)==EOF)
clear(state | failbit);
unlockbuf();
return(*this);
}
/*******************************************************************************/
streampos istream::tellg()
{
streampos retval;
lockbuf();
if ((retval=bp->seekoff(streamoff(0), ios::cur, ios::in))==EOF)
clear(state | failbit);
unlockbuf();
return(retval);
}
/*******************************************************************************/
int istream::get()
{
int c;
if (ipfx(1)) // resets x_gcount
{
if ((c=bp->sbumpc())==EOF)
state |= ios::eofbit;
else
x_gcount++;
isfx();
return c;
}
return EOF;
}
/*******************************************************************************/
// signed and unsigned char make inline calls to this:
istream& istream::get( char& c)
{
int temp;
if (ipfx(1)) // resets x_gcount
{
if ((temp=bp->sbumpc())==EOF)
state |= (ios::failbit|ios::eofbit);
else
x_gcount++;
c = (char) temp;
isfx();
}
return *this;
}
/*******************************************************************************/
// called by signed and unsigned char versions
istream& istream::read(char * ptr, int n)
{
if (ipfx(1)) // resets x_gcount
{
x_gcount = bp->sgetn(ptr, n);
if ((unsigned)x_gcount < (unsigned)n)
state |= (ios::failbit|ios::eofbit);
isfx();
}
return *this;
}
/*******************************************************************************/
int istream::getint(char * buffer) // returns length
{
int base, i;
int c;
int fDigit = 0;
int bindex = 1;
if (x_flags & ios::dec)
base = 10;
else if (x_flags & ios::hex)
base = 16;
else if (x_flags & ios::oct)
base = 8;
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -