3
/* Push back one byte to a given SF_READ stream
5
** Written by Kiem-Phong Vo.
8
static int _uexcept(reg Sfio_t* f, reg int type, Void_t* val, reg Sfdisc_t* disc)
10
static int _uexcept(f,type,val,disc)
19
/* hmm! This should never happen */
23
/* close the unget stream */
24
if(type != SF_CLOSING)
25
(void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
31
int sfungetc(reg Sfio_t* f, reg int c)
34
reg Sfio_t* f; /* push back one byte to this stream */
35
reg int c; /* the value to be pushed back */
42
if(c < 0 || (f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0))
46
/* fast handling of the typical unget */
47
if(f->next > f->data && f->next[-1] == (uchar)c)
52
/* make a string stream for unget characters */
53
if(f->disc != _Sfudisc)
54
{ if(!(uf = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND,
55
-1,SF_STRING|SF_READ)))
59
_Sfudisc->exceptf = _uexcept;
61
SFOPEN(f,0); (void)sfstack(f,uf); SFLOCK(f,0);
65
if(f->next == f->data)
69
if(!(data = (uchar*)malloc(f->size+16)))
73
f->flags |= SF_MALLOC;
75
memcpy((char*)(data+16),(char*)f->data,f->size);
79
f->endb = data+f->size;
82
*--f->next = (uchar)c;