| 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_common.d - common functions and helpers for streams |
| 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 |
/********************************************************************** |
| 20 |
* NOT A #<stream-type> STREAM |
| 21 |
*/ |
| 22 |
|
| 23 |
cl_object |
| 24 |
ecl_not_a_file_stream(cl_object strm) |
| 25 |
{ |
| 26 |
cl_error(9, @'simple-type-error', @':format-control', |
| 27 |
@"~A is not an file stream", |
| 28 |
@':format-arguments', cl_list(1, strm), |
| 29 |
@':expected-type', @'file-stream', |
| 30 |
@':datum', strm); |
| 31 |
} |
| 32 |
|
| 33 |
void |
| 34 |
ecl_not_an_input_stream(cl_object strm) |
| 35 |
{ |
| 36 |
cl_error(9, @'simple-type-error', @':format-control', |
| 37 |
@"~A is not an input stream", |
| 38 |
@':format-arguments', cl_list(1, strm), |
| 39 |
@':expected-type', |
| 40 |
cl_list(2, @'satisfies', @'input-stream-p'), |
| 41 |
@':datum', strm); |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
ecl_not_an_output_stream(cl_object strm) |
| 46 |
{ |
| 47 |
cl_error(9, @'simple-type-error', @':format-control', |
| 48 |
@"~A is not an output stream", |
| 49 |
@':format-arguments', cl_list(1, strm), |
| 50 |
@':expected-type', cl_list(2, @'satisfies', @'output-stream-p'), |
| 51 |
@':datum', strm); |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
not_a_character_stream(cl_object s) |
| 56 |
{ |
| 57 |
cl_error(9, @'simple-type-error', @':format-control', |
| 58 |
@"~A is not a character stream", |
| 59 |
@':format-arguments', cl_list(1, s), |
| 60 |
@':expected-type', @'character', |
| 61 |
@':datum', cl_stream_element_type(s)); |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
not_a_binary_stream(cl_object s) |
| 66 |
{ |
| 67 |
cl_error(9, @'simple-type-error', @':format-control', |
| 68 |
@"~A is not a binary stream", |
| 69 |
@':format-arguments', cl_list(1, s), |
| 70 |
@':expected-type', @'integer', |
| 71 |
@':datum', cl_stream_element_type(s)); |
| 72 |
} |
| 73 |
|
| 74 |
/********************************************************************** |
| 75 |
* NOT IMPLEMENTED or NOT APPLICABLE OPERATIONS |
| 76 |
*/ |
| 77 |
|
| 78 |
cl_index |
| 79 |
ecl_not_output_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 80 |
{ |
| 81 |
ecl_not_an_output_stream(strm); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
cl_index |
| 86 |
ecl_not_input_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 87 |
{ |
| 88 |
ecl_not_an_input_stream(strm); |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
cl_index |
| 93 |
ecl_not_binary_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 94 |
{ |
| 95 |
not_a_binary_stream(strm); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
ecl_not_output_write_byte(cl_object strm, cl_object byte) |
| 101 |
{ |
| 102 |
ecl_not_an_output_stream(strm); |
| 103 |
} |
| 104 |
|
| 105 |
cl_object |
| 106 |
ecl_not_input_read_byte(cl_object strm) |
| 107 |
{ |
| 108 |
ecl_not_an_input_stream(strm); |
| 109 |
return OBJNULL; |
| 110 |
} |
| 111 |
|
| 112 |
void |
| 113 |
ecl_not_binary_write_byte(cl_object strm, cl_object byte) |
| 114 |
{ |
| 115 |
not_a_binary_stream(strm); |
| 116 |
} |
| 117 |
|
| 118 |
cl_object |
| 119 |
ecl_not_binary_read_byte(cl_object strm) |
| 120 |
{ |
| 121 |
not_a_binary_stream(strm); |
| 122 |
return OBJNULL; |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
ecl_not_input_unread_byte(cl_object strm, cl_object byte) |
| 127 |
{ |
| 128 |
ecl_not_an_input_stream(strm); |
| 129 |
} |
| 130 |
|
| 131 |
ecl_character |
| 132 |
ecl_not_input_read_char(cl_object strm) |
| 133 |
{ |
| 134 |
ecl_not_an_input_stream(strm); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
ecl_character |
| 139 |
ecl_not_output_write_char(cl_object strm, ecl_character c) |
| 140 |
{ |
| 141 |
ecl_not_an_output_stream(strm); |
| 142 |
return c; |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
ecl_not_input_unread_char(cl_object strm, ecl_character c) |
| 147 |
{ |
| 148 |
ecl_not_an_input_stream(strm); |
| 149 |
} |
| 150 |
|
| 151 |
int |
| 152 |
ecl_not_input_listen(cl_object strm) |
| 153 |
{ |
| 154 |
ecl_not_an_input_stream(strm); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
|
| 158 |
ecl_character |
| 159 |
ecl_not_character_read_char(cl_object strm) |
| 160 |
{ |
| 161 |
not_a_character_stream(strm); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
ecl_character |
| 166 |
ecl_not_character_write_char(cl_object strm, ecl_character c) |
| 167 |
{ |
| 168 |
not_a_character_stream(strm); |
| 169 |
return c; |
| 170 |
} |
| 171 |
|
| 172 |
ecl_character |
| 173 |
ecl_not_character_decoder(cl_object stream, unsigned char **buffer, unsigned char *buffer_end) |
| 174 |
{ |
| 175 |
not_a_character_stream(stream); |
| 176 |
return EOF; |
| 177 |
} |
| 178 |
|
| 179 |
int |
| 180 |
ecl_not_character_encoder(cl_object stream, unsigned char *buffer, ecl_character c) |
| 181 |
{ |
| 182 |
not_a_character_stream(stream); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
ecl_not_input_clear_input(cl_object strm) |
| 188 |
{ |
| 189 |
ecl_not_an_input_stream(strm); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
void |
| 194 |
ecl_not_output_clear_output(cl_object strm) |
| 195 |
{ |
| 196 |
ecl_not_an_output_stream(strm); |
| 197 |
} |
| 198 |
|
| 199 |
void |
| 200 |
ecl_not_output_force_output(cl_object strm) |
| 201 |
{ |
| 202 |
ecl_not_an_output_stream(strm); |
| 203 |
} |
| 204 |
|
| 205 |
void |
| 206 |
ecl_not_output_finish_output(cl_object strm) |
| 207 |
{ |
| 208 |
ecl_not_an_output_stream(strm); |
| 209 |
} |
| 210 |
|
| 211 |
cl_object |
| 212 |
ecl_not_output_string_length(cl_object strm, cl_object string) |
| 213 |
{ |
| 214 |
ecl_not_an_output_stream(strm); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
cl_object |
| 219 |
ecl_not_file_string_length(cl_object strm, cl_object string) |
| 220 |
{ |
| 221 |
ecl_not_a_file_stream(strm); |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
int |
| 226 |
ecl_unknown_column(cl_object strm) |
| 227 |
{ |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
/********************************************************************** |
| 232 |
* CLOSED STREAM OPS |
| 233 |
*/ |
| 234 |
|
| 235 |
static cl_index |
| 236 |
closed_stream_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 237 |
{ |
| 238 |
FEclosed_stream(strm); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
static cl_index |
| 243 |
closed_stream_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 244 |
{ |
| 245 |
FEclosed_stream(strm); |
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
static cl_object |
| 250 |
closed_stream_read_byte(cl_object strm) |
| 251 |
{ |
| 252 |
FEclosed_stream(strm); |
| 253 |
return ECL_NIL; |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
closed_stream_write_byte(cl_object strm, cl_object byte) |
| 258 |
{ |
| 259 |
FEclosed_stream(strm); |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
closed_stream_unread_byte(cl_object strm, cl_object byte) |
| 264 |
{ |
| 265 |
FEclosed_stream(strm); |
| 266 |
} |
| 267 |
|
| 268 |
static ecl_character |
| 269 |
closed_stream_read_char(cl_object strm) |
| 270 |
{ |
| 271 |
FEclosed_stream(strm); |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
static ecl_character |
| 276 |
closed_stream_write_char(cl_object strm, ecl_character c) |
| 277 |
{ |
| 278 |
FEclosed_stream(strm); |
| 279 |
return c; |
| 280 |
} |
| 281 |
|
| 282 |
static void |
| 283 |
closed_stream_unread_char(cl_object strm, ecl_character c) |
| 284 |
{ |
| 285 |
FEclosed_stream(strm); |
| 286 |
} |
| 287 |
|
| 288 |
static int |
| 289 |
closed_stream_listen(cl_object strm) |
| 290 |
{ |
| 291 |
FEclosed_stream(strm); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
static void |
| 296 |
closed_stream_clear_input(cl_object strm) |
| 297 |
{ |
| 298 |
FEclosed_stream(strm); |
| 299 |
} |
| 300 |
|
| 301 |
#define closed_stream_clear_output closed_stream_clear_input |
| 302 |
#define closed_stream_force_output closed_stream_clear_input |
| 303 |
#define closed_stream_finish_output closed_stream_clear_input |
| 304 |
|
| 305 |
static cl_object |
| 306 |
closed_stream_length(cl_object strm) |
| 307 |
{ |
| 308 |
FEclosed_stream(strm); |
| 309 |
} |
| 310 |
|
| 311 |
#define closed_stream_get_position closed_stream_length |
| 312 |
|
| 313 |
static cl_object |
| 314 |
closed_stream_set_position(cl_object strm, cl_object position) |
| 315 |
{ |
| 316 |
FEclosed_stream(strm); |
| 317 |
} |
| 318 |
|
| 319 |
/********************************************************************** |
| 320 |
* GENERIC OPERATIONS |
| 321 |
* |
| 322 |
* Versions of the methods which are defined in terms of others |
| 323 |
*/ |
| 324 |
|
| 325 |
cl_object |
| 326 |
ecl_generic_peek_byte(cl_object strm) |
| 327 |
{ |
| 328 |
cl_object out = ecl_read_byte(strm); |
| 329 |
if (out != OBJNULL) ecl_unread_byte(out, strm); |
| 330 |
return out; |
| 331 |
} |
| 332 |
|
| 333 |
ecl_character |
| 334 |
ecl_generic_peek_char(cl_object strm) |
| 335 |
{ |
| 336 |
ecl_character out = ecl_read_char(strm); |
| 337 |
if (out != EOF) ecl_unread_char(out, strm); |
| 338 |
return out; |
| 339 |
} |
| 340 |
|
| 341 |
void |
| 342 |
ecl_generic_void(cl_object strm) |
| 343 |
{ |
| 344 |
} |
| 345 |
|
| 346 |
int |
| 347 |
ecl_generic_always_true(cl_object strm) |
| 348 |
{ |
| 349 |
return 1; |
| 350 |
} |
| 351 |
|
| 352 |
int |
| 353 |
ecl_generic_always_false(cl_object strm) |
| 354 |
{ |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
cl_object |
| 359 |
ecl_generic_always_nil(cl_object strm) |
| 360 |
{ |
| 361 |
return ECL_NIL; |
| 362 |
} |
| 363 |
|
| 364 |
int |
| 365 |
ecl_generic_column(cl_object strm) |
| 366 |
{ |
| 367 |
return strm->stream.column; |
| 368 |
} |
| 369 |
|
| 370 |
cl_object |
| 371 |
ecl_generic_set_position(cl_object strm, cl_object pos) |
| 372 |
{ |
| 373 |
return ECL_NIL; |
| 374 |
} |
| 375 |
|
| 376 |
cl_object |
| 377 |
ecl_generic_close(cl_object strm) |
| 378 |
{ |
| 379 |
struct ecl_file_ops *ops = strm->stream.ops; |
| 380 |
if (ecl_input_stream_p(strm)) { |
| 381 |
ops->read_byte8 = closed_stream_read_byte8; |
| 382 |
ops->read_byte = closed_stream_read_byte; |
| 383 |
ops->peek_byte = closed_stream_read_byte; |
| 384 |
ops->unread_byte = closed_stream_unread_byte; |
| 385 |
ops->read_char = closed_stream_read_char; |
| 386 |
ops->peek_char = closed_stream_read_char; |
| 387 |
ops->unread_char = closed_stream_unread_char; |
| 388 |
ops->listen = closed_stream_listen; |
| 389 |
ops->clear_input = closed_stream_clear_input; |
| 390 |
} |
| 391 |
if (ecl_output_stream_p(strm)) { |
| 392 |
ops->write_byte8 = closed_stream_write_byte8; |
| 393 |
ops->write_byte = closed_stream_write_byte; |
| 394 |
ops->write_char = closed_stream_write_char; |
| 395 |
ops->clear_output = closed_stream_clear_output; |
| 396 |
ops->force_output = closed_stream_force_output; |
| 397 |
ops->finish_output = closed_stream_finish_output; |
| 398 |
} |
| 399 |
ops->get_position = closed_stream_get_position; |
| 400 |
ops->set_position = closed_stream_set_position; |
| 401 |
ops->length = closed_stream_length; |
| 402 |
ops->close = ecl_generic_close; |
| 403 |
strm->stream.last_byte = OBJNULL; |
| 404 |
strm->stream.byte_buffer = NULL; |
| 405 |
strm->stream.closed = 1; |
| 406 |
return ECL_T; |
| 407 |
} |
| 408 |
|
| 409 |
cl_index |
| 410 |
ecl_generic_write_vector(cl_object strm, cl_object data, cl_index start, cl_index end) |
| 411 |
{ |
| 412 |
cl_elttype elttype; |
| 413 |
const struct ecl_file_ops *ops; |
| 414 |
if (start >= end) |
| 415 |
return start; |
| 416 |
ops = ecl_stream_dispatch_table(strm); |
| 417 |
elttype = ecl_array_elttype(data); |
| 418 |
if (elttype == ecl_aet_bc || |
| 419 |
#ifdef ECL_UNICODE |
| 420 |
elttype == ecl_aet_ch || |
| 421 |
#endif |
| 422 |
(elttype == ecl_aet_object && ECL_CHARACTERP(ecl_elt(data, 0)))) { |
| 423 |
ecl_character (*write_char)(cl_object, ecl_character) = ops->write_char; |
| 424 |
for (; start < end; start++) { |
| 425 |
write_char(strm, ecl_char_code(ecl_elt(data, start))); |
| 426 |
} |
| 427 |
} else { |
| 428 |
void (*write_byte)(cl_object, cl_object) = ops->write_byte; |
| 429 |
for (; start < end; start++) { |
| 430 |
write_byte(strm, ecl_elt(data, start)); |
| 431 |
} |
| 432 |
} |
| 433 |
return start; |
| 434 |
} |
| 435 |
|
| 436 |
cl_index |
| 437 |
ecl_generic_read_vector(cl_object strm, cl_object data, cl_index start, cl_index end) |
| 438 |
{ |
| 439 |
const struct ecl_file_ops *ops; |
| 440 |
cl_object expected_type; |
| 441 |
if (start >= end) |
| 442 |
return start; |
| 443 |
expected_type = ecl_stream_element_type(strm); |
| 444 |
ops = ecl_stream_dispatch_table(strm); |
| 445 |
if (expected_type == @'base-char' || expected_type == @'character') { |
| 446 |
ecl_character (*read_char)(cl_object) = ops->read_char; |
| 447 |
for (; start < end; start++) { |
| 448 |
ecl_character c = read_char(strm); |
| 449 |
if (c == EOF) break; |
| 450 |
ecl_elt_set(data, start, ECL_CODE_CHAR(c)); |
| 451 |
} |
| 452 |
} else { |
| 453 |
cl_object (*read_byte)(cl_object) = ops->read_byte; |
| 454 |
for (; start < end; start++) { |
| 455 |
cl_object x = read_byte(strm); |
| 456 |
if (x == OBJNULL) break; |
| 457 |
ecl_elt_set(data, start, x); |
| 458 |
} |
| 459 |
} |
| 460 |
return start; |
| 461 |
} |
| 462 |
|