fix assert_port_profile to show clearer error message
This commit is contained in:
parent
ec9305e9a5
commit
19a561132e
32
src/port.c
32
src/port.c
|
@ -352,13 +352,27 @@ pic_port_close_port(pic_state *pic)
|
||||||
return pic_none_value();
|
return pic_none_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define assert_port_profile(port, flgs, stat, caller) do { \
|
#define assert_port_profile(port, flgs, stat, caller) do { \
|
||||||
if ((port->flags & flgs) != flgs) { \
|
if ((port->flags & (flgs)) != (flgs)) { \
|
||||||
pic_error(pic, caller ": expected input port"); \
|
switch (flgs) { \
|
||||||
} \
|
case PIC_PORT_IN | PIC_PORT_TEXT: \
|
||||||
if (port->status != stat) { \
|
pic_error(pic, caller ": expected input/textual port"); \
|
||||||
pic_error(pic, caller ": expected open port"); \
|
case PIC_PORT_IN | PIC_PORT_BINARY: \
|
||||||
} \
|
pic_error(pic, caller ": expected input/binary port"); \
|
||||||
|
case PIC_PORT_OUT | PIC_PORT_TEXT: \
|
||||||
|
pic_error(pic, caller ": expected output/textual port"); \
|
||||||
|
case PIC_PORT_OUT | PIC_PORT_BINARY: \
|
||||||
|
pic_error(pic, caller ": expected output/binary port"); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
if (port->status != stat) { \
|
||||||
|
switch (stat) { \
|
||||||
|
case PIC_PORT_OPEN: \
|
||||||
|
pic_error(pic, caller ": expected open port"); \
|
||||||
|
case PIC_PORT_CLOSE: \
|
||||||
|
pic_error(pic, caller ": expected close port"); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static pic_value
|
static pic_value
|
||||||
|
@ -369,7 +383,7 @@ pic_port_read_char(pic_state *pic)
|
||||||
|
|
||||||
pic_get_args(pic, "|p", &port);
|
pic_get_args(pic, "|p", &port);
|
||||||
|
|
||||||
assert_port_profile(port, PIC_PORT_IN, PIC_PORT_OPEN, "read-char");
|
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char");
|
||||||
|
|
||||||
if ((c = fgetc(port->file)) == EOF) {
|
if ((c = fgetc(port->file)) == EOF) {
|
||||||
return pic_eof_object();
|
return pic_eof_object();
|
||||||
|
@ -387,7 +401,7 @@ pic_port_peek_char(pic_state *pic)
|
||||||
|
|
||||||
pic_get_args(pic, "|p", &port);
|
pic_get_args(pic, "|p", &port);
|
||||||
|
|
||||||
assert_port_profile(port, PIC_PORT_IN, PIC_PORT_OPEN, "peek-char");
|
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "peek-char");
|
||||||
|
|
||||||
if ((c = fgetc(port->file)) == EOF) {
|
if ((c = fgetc(port->file)) == EOF) {
|
||||||
return pic_eof_object();
|
return pic_eof_object();
|
||||||
|
|
Loading…
Reference in New Issue