fix assert_port_profile to show clearer error message
This commit is contained in:
parent
ec9305e9a5
commit
19a561132e
22
src/port.c
22
src/port.c
|
@ -353,11 +353,25 @@ pic_port_close_port(pic_state *pic)
|
|||
}
|
||||
|
||||
#define assert_port_profile(port, flgs, stat, caller) do { \
|
||||
if ((port->flags & flgs) != flgs) { \
|
||||
pic_error(pic, caller ": expected input port"); \
|
||||
if ((port->flags & (flgs)) != (flgs)) { \
|
||||
switch (flgs) { \
|
||||
case PIC_PORT_IN | PIC_PORT_TEXT: \
|
||||
pic_error(pic, caller ": expected input/textual 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)
|
||||
|
||||
|
@ -369,7 +383,7 @@ pic_port_read_char(pic_state *pic)
|
|||
|
||||
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) {
|
||||
return pic_eof_object();
|
||||
|
@ -387,7 +401,7 @@ pic_port_peek_char(pic_state *pic)
|
|||
|
||||
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) {
|
||||
return pic_eof_object();
|
||||
|
|
Loading…
Reference in New Issue