streams: ecl_file_ops cleanup and some minor fixes

1. ecl_peek_char had outdated comment presumbly from before we've introduced
   stream dispatch tables - that comment has been removed.

2. fix erroneous specializations

   - of STREAM-UNREAD-CHAR

   By mistake we had two methods specialized to ANSI-STREAM, while one were
   clearly meant to specialize to T (in order to call BUG-OR-ERROR).

   - of winsock winsock_stream_output_ops

     stream peek char was set to ecl_generic_peek_char instead of
     ecl_not_input_read_char

3. change struct ecl_file_ops definition

a) ecl_file_ops structure change order of some operations to always feature READ
   before WRITE (for consistency)

b) we are more precise in dispatch function declaration and specify the return
   type to be ecl_character where applicable
This commit is contained in:
Daniel Kochmański 2025-07-22 11:38:30 +02:00
parent fad7073f10
commit 431132e4d1
8 changed files with 67 additions and 77 deletions

View file

@ -79,6 +79,18 @@ ecl_write_byte8(cl_object strm, unsigned char *c, cl_index n)
return ecl_stream_dispatch_table(strm)->write_byte8(strm, c, n);
}
cl_object
ecl_read_byte(cl_object strm)
{
return ecl_stream_dispatch_table(strm)->read_byte(strm);
}
void
ecl_write_byte(cl_object byte, cl_object strm)
{
ecl_stream_dispatch_table(strm)->write_byte(strm, byte);
}
ecl_character
ecl_read_char(cl_object strm)
{
@ -94,18 +106,6 @@ ecl_read_char_noeof(cl_object strm)
return c;
}
cl_object
ecl_read_byte(cl_object strm)
{
return ecl_stream_dispatch_table(strm)->read_byte(strm);
}
void
ecl_write_byte(cl_object byte, cl_object strm)
{
ecl_stream_dispatch_table(strm)->write_byte(strm, byte);
}
ecl_character
ecl_write_char(ecl_character c, cl_object strm)
{
@ -118,6 +118,12 @@ ecl_unread_char(ecl_character c, cl_object strm)
ecl_stream_dispatch_table(strm)->unread_char(strm, c);
}
ecl_character
ecl_peek_char(cl_object strm)
{
return ecl_stream_dispatch_table(strm)->peek_char(strm);
}
int
ecl_listen_stream(cl_object strm)
{
@ -214,21 +220,6 @@ ecl_stream_truename(cl_object strm)
return ecl_stream_dispatch_table(strm)->truename(strm);
}
/*
* ecl_read_char(s) tries to read a character from the stream S. It outputs
* either the code of the character read, or EOF. Whe compiled with
* CLOS-STREAMS and S is an instance object, STREAM-READ-CHAR is invoked
* to retrieve the character. Then STREAM-READ-CHAR should either
* output the character, or NIL, indicating EOF.
*
* INV: ecl_read_char(strm) checks the type of STRM.
*/
ecl_character
ecl_peek_char(cl_object strm)
{
return ecl_stream_dispatch_table(strm)->peek_char(strm);
}
/* -- Lisp interface -------------------------------------------------------- */
cl_object

View file

@ -88,7 +88,7 @@ clos_stream_unread_char(cl_object strm, ecl_character c)
_ecl_funcall3(@'gray::stream-unread-char', strm, ECL_CODE_CHAR(c));
}
static int
static ecl_character
clos_stream_peek_char(cl_object strm)
{
cl_object out = _ecl_funcall2(@'gray::stream-peek-char', strm);
@ -217,11 +217,11 @@ clos_stream_close(cl_object strm)
}
const struct ecl_file_ops clos_stream_ops = {
clos_stream_write_byte8,
clos_stream_read_byte8,
clos_stream_write_byte8,
clos_stream_write_byte,
clos_stream_read_byte,
clos_stream_write_byte,
clos_stream_read_char,
clos_stream_write_char,

View file

@ -143,11 +143,11 @@ two_way_close(cl_object strm)
}
const struct ecl_file_ops two_way_ops = {
two_way_write_byte8,
two_way_read_byte8,
two_way_write_byte8,
two_way_write_byte,
two_way_read_byte,
two_way_write_byte,
two_way_read_char,
two_way_write_char,
@ -341,11 +341,11 @@ broadcast_close(cl_object strm)
}
const struct ecl_file_ops broadcast_ops = {
broadcast_write_byte8,
ecl_not_input_read_byte8,
broadcast_write_byte8,
broadcast_write_byte,
ecl_not_input_read_byte,
broadcast_write_byte,
ecl_not_input_read_char,
broadcast_write_char,
@ -531,11 +531,11 @@ echo_close(cl_object strm)
}
const struct ecl_file_ops echo_ops = {
echo_write_byte8,
echo_read_byte8,
echo_write_byte8,
echo_write_byte,
echo_read_byte,
echo_write_byte,
echo_read_char,
echo_write_char,
@ -683,11 +683,11 @@ concatenated_close(cl_object strm)
}
const struct ecl_file_ops concatenated_ops = {
ecl_not_output_write_byte8,
concatenated_read_byte8,
ecl_not_output_write_byte8,
ecl_not_output_write_byte,
concatenated_read_byte,
ecl_not_output_write_byte,
concatenated_read_char,
ecl_not_output_write_char,
@ -915,11 +915,11 @@ synonym_truename(cl_object strm)
}
const struct ecl_file_ops synonym_ops = {
synonym_write_byte8,
synonym_read_byte8,
synonym_write_byte8,
synonym_write_byte,
synonym_read_byte,
synonym_write_byte,
synonym_read_char,
synonym_write_char,

View file

@ -558,11 +558,11 @@ io_file_truename(cl_object strm)
}
const struct ecl_file_ops io_file_ops = {
io_file_write_byte8,
io_file_read_byte8,
io_file_write_byte8,
ecl_generic_write_byte,
ecl_generic_read_byte,
ecl_generic_write_byte,
ecl_eformat_read_char,
ecl_eformat_write_char,
@ -596,11 +596,11 @@ const struct ecl_file_ops io_file_ops = {
};
const struct ecl_file_ops output_file_ops = {
output_file_write_byte8,
ecl_not_input_read_byte8,
output_file_write_byte8,
ecl_generic_write_byte,
ecl_not_input_read_byte,
ecl_generic_write_byte,
ecl_not_input_read_char,
ecl_eformat_write_char,
@ -634,11 +634,11 @@ const struct ecl_file_ops output_file_ops = {
};
const struct ecl_file_ops input_file_ops = {
ecl_not_output_write_byte8,
io_file_read_byte8,
ecl_not_output_write_byte8,
ecl_not_output_write_byte,
ecl_generic_read_byte,
ecl_not_output_write_byte,
ecl_eformat_read_char,
ecl_not_output_write_char,
@ -940,11 +940,11 @@ io_stream_close(cl_object strm)
#define io_stream_write_vector io_file_write_vector
const struct ecl_file_ops io_stream_ops = {
io_stream_write_byte8,
io_stream_read_byte8,
io_stream_write_byte8,
ecl_generic_write_byte,
ecl_generic_read_byte,
ecl_generic_write_byte,
ecl_eformat_read_char,
ecl_eformat_write_char,
@ -978,11 +978,11 @@ const struct ecl_file_ops io_stream_ops = {
};
const struct ecl_file_ops output_stream_ops = {
output_stream_write_byte8,
ecl_not_input_read_byte8,
output_stream_write_byte8,
ecl_generic_write_byte,
ecl_not_input_read_byte,
ecl_generic_write_byte,
ecl_not_input_read_char,
ecl_eformat_write_char,
@ -1016,11 +1016,11 @@ const struct ecl_file_ops output_stream_ops = {
};
const struct ecl_file_ops input_stream_ops = {
ecl_not_output_write_byte8,
input_stream_read_byte8,
ecl_not_output_write_byte8,
ecl_not_output_write_byte,
ecl_generic_read_byte,
ecl_not_output_write_byte,
ecl_eformat_read_char,
ecl_not_output_write_char,
@ -1165,11 +1165,11 @@ winsock_stream_close(cl_object strm)
}
const struct ecl_file_ops winsock_stream_io_ops = {
winsock_stream_write_byte8,
winsock_stream_read_byte8,
winsock_stream_write_byte8,
ecl_generic_write_byte,
ecl_generic_read_byte,
ecl_generic_write_byte,
ecl_eformat_read_char,
ecl_eformat_write_char,
@ -1203,16 +1203,16 @@ const struct ecl_file_ops winsock_stream_io_ops = {
};
const struct ecl_file_ops winsock_stream_output_ops = {
winsock_stream_write_byte8,
ecl_not_input_read_byte8,
winsock_stream_write_byte8,
ecl_generic_write_byte,
ecl_not_input_read_byte,
ecl_generic_write_byte,
ecl_not_input_read_char,
ecl_eformat_write_char,
ecl_not_input_unread_char,
ecl_generic_peek_char,
ecl_not_input_read_char,
ecl_generic_read_vector,
ecl_generic_write_vector,
@ -1241,11 +1241,11 @@ const struct ecl_file_ops winsock_stream_output_ops = {
};
const struct ecl_file_ops winsock_stream_input_ops = {
ecl_not_output_write_byte8,
winsock_stream_read_byte8,
ecl_not_output_write_byte8,
ecl_not_output_write_byte,
ecl_generic_read_byte,
ecl_not_output_write_byte,
ecl_eformat_read_char,
ecl_not_output_write_char,
@ -1350,11 +1350,11 @@ wcon_stream_force_output(cl_object strm)
}
const struct ecl_file_ops wcon_stream_io_ops = {
wcon_stream_write_byte8,
wcon_stream_read_byte8,
wcon_stream_write_byte8,
ecl_generic_write_byte,
ecl_generic_read_byte,
ecl_generic_write_byte,
ecl_eformat_read_char,
ecl_eformat_write_char,

View file

@ -155,11 +155,11 @@ seq_file_element_type(cl_object strm)
}
const struct ecl_file_ops seq_in_ops = {
ecl_not_output_write_byte8,
seq_in_read_byte8,
ecl_not_output_write_byte8,
ecl_not_output_write_byte,
ecl_generic_read_byte,
ecl_not_output_write_byte,
ecl_eformat_read_char,
ecl_not_output_write_char,
@ -365,11 +365,11 @@ seq_out_set_position(cl_object strm, cl_object pos)
}
const struct ecl_file_ops seq_out_ops = {
seq_out_write_byte8,
ecl_not_input_read_byte8,
seq_out_write_byte8,
ecl_generic_write_byte,
ecl_not_input_read_byte,
ecl_generic_write_byte,
ecl_not_input_read_char,
ecl_eformat_write_char,

View file

@ -84,11 +84,11 @@ str_out_set_position(cl_object strm, cl_object pos)
}
const struct ecl_file_ops str_out_ops = {
ecl_not_output_write_byte8,
ecl_not_binary_read_byte8,
ecl_not_output_write_byte8,
ecl_not_binary_write_byte,
ecl_not_input_read_byte,
ecl_not_binary_write_byte,
ecl_not_input_read_char,
str_out_write_char,
@ -277,11 +277,11 @@ str_in_set_position(cl_object strm, cl_object pos)
}
const struct ecl_file_ops str_in_ops = {
ecl_not_output_write_byte8,
ecl_not_binary_read_byte8,
ecl_not_output_write_byte8,
ecl_not_output_write_byte,
ecl_not_binary_read_byte,
ecl_not_output_write_byte,
str_in_read_char,
ecl_not_output_write_char,

View file

@ -95,7 +95,6 @@
(:documentation "Return the stream line length or NIL."))
(defgeneric stream-listen (stream)
#+sb-doc
(:documentation
"This is used by LISTEN. It returns true or false. The default method uses
STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
@ -516,7 +515,7 @@
(defmethod stream-unread-char ((stream ansi-stream) character)
(cl:unread-char character stream))
(defmethod stream-unread-char ((stream ansi-stream) character)
(defmethod stream-unread-char ((stream t) character)
(declare (ignore character))
(bug-or-error stream 'stream-unread-char))

View file

@ -587,16 +587,16 @@ enum ecl_smmode { /* stream mode */
};
struct ecl_file_ops {
cl_index (*write_byte8)(cl_object strm, unsigned char *c, cl_index n);
cl_index (*read_byte8)(cl_object strm, unsigned char *c, cl_index n);
cl_index (*write_byte8)(cl_object strm, unsigned char *c, cl_index n);
void (*write_byte)(cl_object strm, cl_object byte);
cl_object (*read_byte)(cl_object strm);
void (*write_byte)(cl_object strm, cl_object byte);
int (*read_char)(cl_object strm);
int (*write_char)(cl_object strm, int c);
void (*unread_char)(cl_object strm, int c);
int (*peek_char)(cl_object strm);
ecl_character (*read_char)(cl_object strm);
ecl_character (*write_char)(cl_object strm, ecl_character c);
void (*unread_char)(cl_object strm, ecl_character c);
ecl_character (*peek_char)(cl_object strm);
cl_index (*read_vector)(cl_object strm, cl_object data, cl_index start, cl_index end);
cl_index (*write_vector)(cl_object strm, cl_object data, cl_index start, cl_index end);