| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* strm_clos.d - Gray Streams dispatch table |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* Copyright (c) 2025 Daniel Kochmanski |
| 11 |
* |
| 12 |
* See file 'LICENSE' for the copyright details. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <ecl/ecl.h> |
| 17 |
#include <ecl/internal.h> |
| 18 |
|
| 19 |
#ifdef ECL_CLOS_STREAMS |
| 20 |
static cl_index |
| 21 |
clos_stream_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 22 |
{ |
| 23 |
cl_index i; |
| 24 |
for (i = 0; i < n; i++) { |
| 25 |
cl_object byte = _ecl_funcall2(@'gray::stream-read-byte', strm); |
| 26 |
if (!ECL_FIXNUMP(byte)) |
| 27 |
break; |
| 28 |
c[i] = ecl_fixnum(byte); |
| 29 |
} |
| 30 |
return i; |
| 31 |
} |
| 32 |
|
| 33 |
static cl_index |
| 34 |
clos_stream_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 35 |
{ |
| 36 |
cl_index i; |
| 37 |
for (i = 0; i < n; i++) { |
| 38 |
cl_object byte = _ecl_funcall3(@'gray::stream-write-byte', strm, |
| 39 |
ecl_make_fixnum(c[i])); |
| 40 |
if (!ECL_FIXNUMP(byte)) |
| 41 |
break; |
| 42 |
} |
| 43 |
return i; |
| 44 |
} |
| 45 |
|
| 46 |
static cl_object |
| 47 |
clos_stream_read_byte(cl_object strm) |
| 48 |
{ |
| 49 |
cl_object out = _ecl_funcall2(@'gray::stream-read-byte', strm); |
| 50 |
if (out == @':eof') out = OBJNULL; |
| 51 |
return out; |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
clos_stream_write_byte(cl_object strm, cl_object byte) |
| 56 |
{ |
| 57 |
_ecl_funcall3(@'gray::stream-write-byte', strm, byte); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
clos_stream_unread_byte(cl_object strm, cl_object byte) |
| 62 |
{ |
| 63 |
_ecl_funcall3(@'gray::stream-unread-byte', strm, byte); |
| 64 |
} |
| 65 |
|
| 66 |
static cl_object |
| 67 |
clos_stream_peek_byte(cl_object strm) |
| 68 |
{ |
| 69 |
cl_object out = _ecl_funcall2(@'gray::stream-peek-byte', strm); |
| 70 |
if (out == @':eof') return OBJNULL; |
| 71 |
return out; |
| 72 |
} |
| 73 |
|
| 74 |
static ecl_character |
| 75 |
clos_stream_read_char(cl_object strm) |
| 76 |
{ |
| 77 |
cl_object output = _ecl_funcall2(@'gray::stream-read-char', strm); |
| 78 |
cl_fixnum value; |
| 79 |
if (ECL_CHARACTERP(output)) |
| 80 |
value = ECL_CHAR_CODE(output); |
| 81 |
else if (ECL_FIXNUMP(output)) |
| 82 |
value = ecl_fixnum(output); |
| 83 |
else if (output == ECL_NIL || output == @':eof') |
| 84 |
return EOF; |
| 85 |
else |
| 86 |
value = -1; |
| 87 |
unlikely_if (value < 0 || value > ECL_CHAR_CODE_LIMIT) |
| 88 |
FEerror("Unknown character ~A", 1, output); |
| 89 |
return value; |
| 90 |
} |
| 91 |
|
| 92 |
static ecl_character |
| 93 |
clos_stream_write_char(cl_object strm, ecl_character c) |
| 94 |
{ |
| 95 |
_ecl_funcall3(@'gray::stream-write-char', strm, ECL_CODE_CHAR(c)); |
| 96 |
return c; |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
clos_stream_unread_char(cl_object strm, ecl_character c) |
| 101 |
{ |
| 102 |
_ecl_funcall3(@'gray::stream-unread-char', strm, ECL_CODE_CHAR(c)); |
| 103 |
} |
| 104 |
|
| 105 |
static ecl_character |
| 106 |
clos_stream_peek_char(cl_object strm) |
| 107 |
{ |
| 108 |
cl_object out = _ecl_funcall2(@'gray::stream-peek-char', strm); |
| 109 |
if (out == @':eof') return EOF; |
| 110 |
return ecl_char_code(out); |
| 111 |
} |
| 112 |
|
| 113 |
static cl_index |
| 114 |
clos_stream_read_vector(cl_object strm, cl_object data, cl_index start, cl_index end) |
| 115 |
{ |
| 116 |
return fixnnint(_ecl_funcall5(@'gray::stream-read-sequence', strm, data, ecl_make_fixnum(start), ecl_make_fixnum(end))); |
| 117 |
} |
| 118 |
|
| 119 |
static cl_index |
| 120 |
clos_stream_write_vector(cl_object strm, cl_object data, cl_index start, cl_index end) |
| 121 |
{ |
| 122 |
_ecl_funcall5(@'gray::stream-write-sequence', strm, data, ecl_make_fixnum(start), ecl_make_fixnum(end)); |
| 123 |
if (start >= end) |
| 124 |
return start; |
| 125 |
return end; |
| 126 |
} |
| 127 |
|
| 128 |
static int |
| 129 |
clos_stream_listen(cl_object strm) |
| 130 |
{ |
| 131 |
return !Null(_ecl_funcall2(@'gray::stream-listen', strm)); |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
clos_stream_clear_input(cl_object strm) |
| 136 |
{ |
| 137 |
_ecl_funcall2(@'gray::stream-clear-input', strm); |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
clos_stream_clear_output(cl_object strm) |
| 142 |
{ |
| 143 |
_ecl_funcall2(@'gray::stream-clear-output', strm); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
clos_stream_force_output(cl_object strm) |
| 149 |
{ |
| 150 |
_ecl_funcall2(@'gray::stream-force-output', strm); |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
clos_stream_finish_output(cl_object strm) |
| 155 |
{ |
| 156 |
_ecl_funcall2(@'gray::stream-finish-output', strm); |
| 157 |
} |
| 158 |
|
| 159 |
static int |
| 160 |
clos_stream_input_p(cl_object strm) |
| 161 |
{ |
| 162 |
return !Null(_ecl_funcall2(@'gray::input-stream-p', strm)); |
| 163 |
} |
| 164 |
|
| 165 |
static int |
| 166 |
clos_stream_output_p(cl_object strm) |
| 167 |
{ |
| 168 |
return !Null(_ecl_funcall2(@'gray::output-stream-p', strm)); |
| 169 |
} |
| 170 |
|
| 171 |
static int |
| 172 |
clos_stream_interactive_p(cl_object strm) |
| 173 |
{ |
| 174 |
return !Null(_ecl_funcall2(@'gray::stream-interactive-p', strm)); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
static cl_object |
| 179 |
clos_stream_element_type(cl_object strm) |
| 180 |
{ |
| 181 |
return _ecl_funcall2(@'gray::stream-element-type', strm); |
| 182 |
} |
| 183 |
|
| 184 |
static cl_object |
| 185 |
clos_stream_length(cl_object strm) |
| 186 |
{ |
| 187 |
return _ecl_funcall2(@'gray::stream-file-length', strm); |
| 188 |
} |
| 189 |
|
| 190 |
static cl_object |
| 191 |
clos_stream_get_position(cl_object strm) |
| 192 |
{ |
| 193 |
return _ecl_funcall2(@'gray::stream-file-position', strm); |
| 194 |
} |
| 195 |
|
| 196 |
static cl_object |
| 197 |
clos_stream_set_position(cl_object strm, cl_object pos) |
| 198 |
{ |
| 199 |
return _ecl_funcall3(@'gray::stream-file-position', strm, pos); |
| 200 |
} |
| 201 |
|
| 202 |
static cl_object |
| 203 |
clos_stream_string_length(cl_object strm, cl_object string) |
| 204 |
{ |
| 205 |
return _ecl_funcall3(@'gray::stream-file-string-length', strm, string); |
| 206 |
} |
| 207 |
|
| 208 |
static int |
| 209 |
clos_stream_column(cl_object strm) |
| 210 |
{ |
| 211 |
cl_object col = _ecl_funcall2(@'gray::stream-line-column', strm); |
| 212 |
return Null(col)? -1 : ecl_to_size(ecl_floor1(col)); |
| 213 |
} |
| 214 |
|
| 215 |
static cl_object |
| 216 |
clos_stream_pathname(cl_object strm) |
| 217 |
{ |
| 218 |
return _ecl_funcall2(@'gray::pathname', strm); |
| 219 |
} |
| 220 |
|
| 221 |
static cl_object |
| 222 |
clos_stream_truename(cl_object strm) |
| 223 |
{ |
| 224 |
return _ecl_funcall2(@'gray::truename', strm); |
| 225 |
} |
| 226 |
|
| 227 |
static cl_object |
| 228 |
clos_stream_close(cl_object strm) |
| 229 |
{ |
| 230 |
return _ecl_funcall2(@'gray::close', strm); |
| 231 |
} |
| 232 |
|
| 233 |
const struct ecl_file_ops clos_stream_ops = { |
| 234 |
clos_stream_read_byte8, |
| 235 |
clos_stream_write_byte8, |
| 236 |
|
| 237 |
clos_stream_read_byte, |
| 238 |
clos_stream_write_byte, |
| 239 |
clos_stream_unread_byte, |
| 240 |
clos_stream_peek_byte, |
| 241 |
|
| 242 |
clos_stream_read_char, |
| 243 |
clos_stream_write_char, |
| 244 |
clos_stream_unread_char, |
| 245 |
clos_stream_peek_char, |
| 246 |
|
| 247 |
clos_stream_read_vector, |
| 248 |
clos_stream_write_vector, |
| 249 |
|
| 250 |
clos_stream_listen, |
| 251 |
clos_stream_clear_input, |
| 252 |
clos_stream_clear_output, |
| 253 |
clos_stream_finish_output, |
| 254 |
clos_stream_force_output, |
| 255 |
|
| 256 |
clos_stream_input_p, |
| 257 |
clos_stream_output_p, |
| 258 |
clos_stream_interactive_p, |
| 259 |
clos_stream_element_type, |
| 260 |
|
| 261 |
clos_stream_length, |
| 262 |
clos_stream_get_position, |
| 263 |
clos_stream_set_position, |
| 264 |
clos_stream_string_length, |
| 265 |
clos_stream_column, |
| 266 |
|
| 267 |
clos_stream_pathname, |
| 268 |
clos_stream_truename, |
| 269 |
|
| 270 |
clos_stream_close |
| 271 |
}; |
| 272 |
#endif /* ECL_CLOS_STREAMS */ |