peek support

This commit is contained in:
Vítor Santos Costa 2015-12-15 09:14:15 +00:00
parent e95e795e08
commit 5ceb98bdf9
5 changed files with 909 additions and 956 deletions

View File

@ -26,6 +26,7 @@ static char SccsId[] = "%W% %G%";
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "YapText.h"
#include "yapio.h"
#include <stdlib.h>
#if HAVE_UNISTD_H
@ -84,18 +85,66 @@ Int Yap_peek(int sno) {
Int ch;
s = GLOBAL_Stream + sno;
if ( s->status & Readline_Stream_f) {
ch = Yap_ReadlinePeekChar( sno );
if (ch == EOFCHAR) {
s->stream_getc = EOFPeek;
s->stream_wgetc = EOFWPeek;
s->status |= Push_Eof_Stream_f;
}
return ch;
}
ocharcount = s->charcount;
olinecount = s->linecount;
olinepos = s->linepos;
ch = s->stream_wgetc(sno);
s ->och = ch;
if (ch == EOFCHAR) {
s->stream_getc = EOFPeek;
s->stream_wgetc = EOFWPeek;
s->status |= Push_Eof_Stream_f;
return ch;
}
s->charcount = ocharcount;
s->linecount = olinecount;
s->linepos = olinepos;
/* buffer the character */
if (s->encoding == LOCAL_encoding) {
ungetwc(ch, s->file);
} else {
} else if (s->encoding == ENC_OCTET ||
s->encoding == ENC_ISO_LATIN1||
s->encoding == ENC_ISO_ASCII) {
ungetc(ch, s->file);
} else if (s->encoding == ENC_ISO_UTF8) {
unsigned char cs[8];
size_t n = put_utf8(cs, ch );
while (n--) {
ungetc(cs[n-1], s->file);
}
} else if (s->encoding == ENC_UTF16_BE) {
/* do the ungetc as if a write .. */
unsigned long int c = ch;
if (c >((1<<16)-1)) {
ungetc(c/1<<16, s->file);
c %= 1<< 16;
}
ungetc(c, s->file);
} else if (s->encoding == ENC_UTF16_BE) {
/* do the ungetc as if a write .. */
unsigned long int c = ch;
if (c > ((1<<16)-1)) {
ungetc(c/1<<16, s->file);
c %= 1<< 16;
}
} else if (s->encoding == ENC_UTF16_LE) {
/* do the ungetc as if a write .. */
unsigned long int c = ch;
if (c >(( 1<<16)-1)) {
ungetc(c%1<<16, s->file);
c /= 1<< 16;
}
ungetc(c, s->file);
} else {
int (*f)(int, int) = s->stream_putc;
s->stream_putc = plUnGetc;
put_wchar(sno, ch);

File diff suppressed because it is too large Load Diff

View File

@ -301,6 +301,7 @@ void Yap_InitWriteTPreds(void);
void Yap_InitReadTPreds(void);
void Yap_socketStream( StreamDesc *s );
void Yap_ReadlineFlush( int sno );
Int Yap_ReadlinePeekChar( int sno );
int Yap_ReadlineForSIGINT(void);
bool Yap_ReadlinePrompt( StreamDesc * s );
@ -327,6 +328,8 @@ int DefaultGets( int,UInt,char*);
int put_wchar(int sno, wchar_t ch);
Int GetStreamFd(int sno);
int ResetEOF(StreamDesc *s);
int EOFPeek(int sno);
int EOFWPeek(int sno);
void Yap_SetAlias (Atom arg, int sno);
bool Yap_AddAlias (Atom arg, int sno);

View File

@ -300,6 +300,42 @@ static int ReadlineGetc(int sno) {
return console_post_process_read_char(ch, s);
}
/**
@brief Yap_ReadlinePeekChar peeks the next char from the
readline buffer, but does not actually grab it.
The idea is to take advantage of the buffering. Special care must be taken with EOF, though.
*/
Int Yap_ReadlinePeekChar( int sno) {
StreamDesc *s = &GLOBAL_Stream[sno];
int ch;
if (s->u.irl.buf) {
const char *ttyptr = s->u.irl.ptr;
ch = *ttyptr;
if (ch == '\0') {
ch = '\n';
}
} if (getLine(sno, StdErrStream) ) {
CACHE_REGS
ch = s->u.irl.ptr[0];
if (ch == '\0') {
ch = '\n';
}
if (ch == '\n') {
LOCAL_newline = true;
} else {
LOCAL_newline = false;
}
} else {
return EOF;
}
return ch;
}
int Yap_ReadlineForSIGINT(void) {
CACHE_REGS
int ch;

View File

@ -140,6 +140,7 @@ int GetFreeStreamD(void) {
LOCK(GLOBAL_Stream[sno].streamlock);
UNLOCK(GLOBAL_StreamDescLock);
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
GLOBAL_Stream[sno].och = '\0';
return sno;
}