underflow
The underflow function is called when characters are needed for
fetching and none are available in the get area.
Its general outline is similar to
overflow(),
but it deals with the get area rather than the put area.
int fctbuf::underflow() {
// Check that input is allowed
if ( !(mode&ios::in) ) return EOF ;
// Make sure there is a holding area.
if (allocate()==EOF) return EOF ;
// If there are characters waiting for output
// send them ;
if ( pptr() && pptr() > pbase() ) overflow(EOF) ;
// Reset put area
setp(0,0) ;
// Setup get area ;
if ( blen() > 1 ) setg(base(),base()+1,ebuf()) ;
else setg(base(),base(),ebuf()) ;
// Produce characters
int ok = (*fct)(base(),blen(),ios::in) ;
if ( ok ) {
return zapeof(*base()) ;
}
else {
setg(0,0,0) ;
return EOF ;
}
}
Some explanations:
-
EOF
is returned immediately if we aren't supposed to do input
or if a holding area cannot be allocated.
-
allocate()
is called to make sure that there is a holding area.
-
setg()
is used to establish the get area where
fct
will be asked to store characters.
Its first argument sets up a pointer,
eback,
that marks the limit to which
putback
can move
gptr.
The second argument becomes
gptr,
and the last becomes
egptr,
pointing at the char after the last
char
containing values stored by the producer.
-
blen()
returns the size of the holding area.
It may be as small as 1.
-
If the action function indicated success
underflow()
returns the first character.
It is left in the get area and may be extracted again.
zapeof()
is used to make sure that the returned result is not
EOF.
If
zapeof()
were omitted this might occur on a machine in which
chars
are signed and
EOF
is -1.
Next topic:
sync
Previous topic:
overflow
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005