improve error handling in get_byte

This commit is contained in:
Vitor Santos Costa 2016-04-22 18:24:20 +01:00
parent c06d07985e
commit f4111997e7

View File

@ -44,9 +44,9 @@ static char SccsId[] = "%W% %G%";
*/ */
#include "Yap.h" #include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h" #include "YapHeap.h"
#include "YapText.h" #include "YapText.h"
#include "Yatom.h"
#include "yapio.h" #include "yapio.h"
#include <stdlib.h> #include <stdlib.h>
#if HAVE_UNISTD_H #if HAVE_UNISTD_H
@ -122,7 +122,7 @@ Int Yap_peek(int sno) {
olinecount = s->linecount; olinecount = s->linecount;
olinepos = s->linepos; olinepos = s->linepos;
ch = s->stream_wgetc(sno); ch = s->stream_wgetc(sno);
if (ch == EOFCHAR) { if (ch == EOFCHAR) {
s->stream_getc = EOFPeek; s->stream_getc = EOFPeek;
s->stream_wgetc = EOFWPeek; s->stream_wgetc = EOFWPeek;
s->status |= Push_Eof_Stream_f; s->status |= Push_Eof_Stream_f;
@ -209,21 +209,20 @@ static Int dopeek_byte(int sno) {
return ch; return ch;
} }
bool store_code(int ch, Term t USES_REGS) bool store_code(int ch, Term t USES_REGS) {
{
Term t2 = Deref(t); Term t2 = Deref(t);
bool rc = Yap_unify_constant(t2, MkIntegerTerm(ch)); bool rc = Yap_unify_constant(t2, MkIntegerTerm(ch));
if (!rc && !IsVarTerm(t2)) { if (!rc && !IsVarTerm(t2)) {
if (!IsIntegerTerm(t2)) { if (!IsIntegerTerm(t2)) {
Yap_Error( TYPE_ERROR_INTEGER, t, "in output argument"); Yap_Error(TYPE_ERROR_INTEGER, t, "in output argument");
} else if (IntegerOfTerm(t2) < 0){ } else if (IntegerOfTerm(t2) < 0) {
Yap_Error( REPRESENTATION_ERROR_IN_CHARACTER_CODE, t, "in output argument"); Yap_Error(REPRESENTATION_ERROR_IN_CHARACTER_CODE, t,
"in output argument");
} }
} }
return rc; return rc;
} }
/** @pred at_end_of_stream(+ _S_) is iso /** @pred at_end_of_stream(+ _S_) is iso
Succeed if the stream _S_ has stream position end-of-stream or Succeed if the stream _S_ has stream position end-of-stream or
@ -327,7 +326,7 @@ static Int get_char(USES_REGS1) { /* '$get'(Stream,-N) */
bool rc = Yap_unify_constant(t2, MkCharTerm(ch)); bool rc = Yap_unify_constant(t2, MkCharTerm(ch));
if (!rc) { if (!rc) {
if (!IsAtomTerm(t2)) { if (!IsAtomTerm(t2)) {
Yap_Error( TYPE_ERROR_IN_CHARACTER, ARG2, "in input argument"); Yap_Error(TYPE_ERROR_IN_CHARACTER, ARG2, "in input argument");
} }
} }
return rc; return rc;
@ -374,8 +373,9 @@ static Int get_1(USES_REGS1) { /* get_code1(Stream,-N) */
// status = GLOBAL_Stream[sno].status; // status = GLOBAL_Stream[sno].status;
if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) { if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, TermUserIn, "while getting code"); PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, TermUserIn,
return false; "while getting code");
return false;
} }
while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) <= 32 && ch >= 0) while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) <= 32 && ch >= 0)
; ;
@ -401,7 +401,8 @@ static Int getcode_1(USES_REGS1) { /* get0(Stream,-N) */
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) { if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, TermUserIn, "while getting code"); PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, TermUserIn,
"while getting code");
return false; return false;
} }
out = GLOBAL_Stream[sno].stream_wgetc(sno); out = GLOBAL_Stream[sno].stream_wgetc(sno);
@ -428,7 +429,8 @@ static Int getchar_1(USES_REGS1) { /* get0(Stream,-N) */
ch = GLOBAL_Stream[sno].stream_wgetc(sno); ch = GLOBAL_Stream[sno].stream_wgetc(sno);
if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) { if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, TermUserIn, "while getting code"); PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, TermUserIn,
"while getting code");
return false; return false;
} }
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
@ -436,7 +438,7 @@ static Int getchar_1(USES_REGS1) { /* get0(Stream,-N) */
if (!rc) { if (!rc) {
Term t2 = Deref(ARG1); Term t2 = Deref(ARG1);
if (!IsAtomTerm(t2)) { if (!IsAtomTerm(t2)) {
Yap_Error( TYPE_ERROR_IN_CHARACTER, ARG1, "in input argument"); Yap_Error(TYPE_ERROR_IN_CHARACTER, ARG1, "in input argument");
} }
} }
return rc; return rc;
@ -471,12 +473,10 @@ code with _C_.
*/ */
static Int get_byte(USES_REGS1) { /* '$get_byte'(Stream,-N) */ static Int get_byte(USES_REGS1) { /* '$get_byte'(Stream,-N) */
int sno = Yap_CheckBinaryStream(ARG1, Input_Stream_f, "get_byte/2"); int sno = Yap_CheckBinaryStream(ARG1, Input_Stream_f, "get_byte/2");
Int status;
Term out; Term out;
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
status = GLOBAL_Stream[sno].status;
out = MkIntTerm(GLOBAL_Stream[sno].stream_getc(sno)); out = MkIntTerm(GLOBAL_Stream[sno].stream_getc(sno));
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return Yap_unify_constant(ARG2, out); return Yap_unify_constant(ARG2, out);