Debian Codegraph @

src/c/streams/strm_sequence.d

raw
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_sequence.d - Sequence Stream dispatch tables
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/ecl-inl.h>
18 #define ECL_DEFINE_AET_SIZE
19 #include <ecl/internal.h>
20
21 /**********************************************************************
22 * Direct vector operations (no conversion, arbitrary element type).
23 */
24
25 static cl_object
26 seq_read_object(cl_object strm)
27 {
28 cl_object vec = SEQ_STREAM_VECTOR(strm);
29 cl_fixnum pos = SEQ_STREAM_POSITION(strm);
30 if (pos < SEQ_INPUT_LIMIT(strm)) {
31 SEQ_STREAM_POSITION(strm)++;
32 return ecl_aref_unsafe(vec, pos);
33 }
34 return OBJNULL;
35 }
36
37 static cl_object
38 seq_peek_object(cl_object strm)
39 {
40 cl_object vec = SEQ_STREAM_VECTOR(strm);
41 cl_fixnum pos = SEQ_STREAM_POSITION(strm);
42 if (pos < SEQ_INPUT_LIMIT(strm)) {
43 return ecl_aref_unsafe(vec, pos);
44 }
45 return OBJNULL;
46 }
47
48 static void
49 seq_write_object(cl_object strm, cl_object object)
50 {
51 cl_object vec = SEQ_STREAM_VECTOR(strm);
52 cl_fixnum pos = SEQ_STREAM_POSITION(strm);
53 cl_fixnum dim = vec->vector.dim;
54 AGAIN:
55 if (pos >= dim) {
56 cl_object size = ecl_ash(ecl_make_fixnum(dim), 1);
57 SEQ_STREAM_VECTOR(strm) = vec = si_adjust_vector(vec, size);
58 goto AGAIN;
59 }
60 ecl_aset(vec, pos++, object);
61 if (vec->vector.fillp < pos)
62 vec->vector.fillp = pos;
63 SEQ_STREAM_POSITION(strm) = pos;
64 }
65
66 static void
67 seq_unread_object(cl_object strm, cl_object object)
68 {
69 unlikely_if (SEQ_STREAM_POSITION(strm) <= 0) {
70 ecl_unread_error(strm);
71 }
72 SEQ_STREAM_POSITION(strm)--;
73 }
74
75 /* -- Direct byte <- byte ------------------------------------------- */
76 static cl_object
77 byte_byte(cl_object byte)
78 {
79 unlikely_if (byte != OBJNULL && Null(cl_integerp(byte))) {
80 FEwrong_type_argument(@[byte], byte);
81 }
82 return byte;
83 }
84
85 static cl_object
86 seq_byte_read_byte(cl_object strm)
87 { return byte_byte(seq_read_object(strm)); }
88
89 static cl_object
90 seq_byte_peek_byte(cl_object strm)
91 { return byte_byte(seq_peek_object(strm)); }
92
93 static void
94 seq_byte_write_byte(cl_object strm, cl_object byte)
95 { seq_write_object(strm, byte_byte(byte)); }
96
97 static void
98 seq_byte_unread_byte(cl_object strm, cl_object byte)
99 { seq_unread_object(strm, byte_byte(byte)); }
100
101 /* -- Direct char <- char ------------------------------------------- */
102 static ecl_character
103 char_char(cl_object byte)
104 {
105 unlikely_if (byte != OBJNULL && !ECL_CHARACTERP(byte)) {
106 FEwrong_type_argument(@[char], byte);
107 }
108 return byte == OBJNULL ? EOF : ECL_CHAR_CODE(byte);
109 }
110
111 static ecl_character
112 seq_char_read_char(cl_object strm)
113 { return char_char(seq_read_object(strm)); }
114
115 static ecl_character
116 seq_char_peek_char(cl_object strm)
117 { return char_char(seq_peek_object(strm)); }
118
119 static ecl_character
120 seq_char_write_char(cl_object strm, ecl_character c)
121 { seq_write_object(strm, ECL_CODE_CHAR(c)); return c; }
122
123 static void
124 seq_char_unread_char(cl_object strm, ecl_character c)
125 { seq_unread_object(strm, ECL_CODE_CHAR(c)); }
126
127 /* -- Direct byte <- char ------------------------------------------- */
128 static cl_object
129 byte_char(cl_object byte)
130 {
131 unlikely_if (byte != OBJNULL && !ECL_CHARACTERP(byte)) {
132 FEwrong_type_argument(@[char], byte);
133 }
134 return byte == OBJNULL ? OBJNULL : cl_char_code(byte);
135 }
136
137 static cl_object
138 seq_char_read_byte(cl_object strm)
139 { return byte_char(seq_read_object(strm)); }
140
141 static cl_object
142 seq_char_peek_byte(cl_object strm)
143 { return byte_char(seq_peek_object(strm)); }
144
145 static void
146 seq_char_write_byte(cl_object strm, cl_object byte)
147 { seq_write_object(strm, cl_code_char(byte)); }
148
149 static void
150 seq_char_unread_byte(cl_object strm, cl_object byte)
151 { seq_unread_object(strm, cl_code_char(byte)); }
152
153 /* -- Direct char <- byte ------------------------------------------- */
154 static ecl_character
155 char_byte(cl_object byte)
156 {
157 unlikely_if (byte != OBJNULL && Null(cl_integerp(byte))) {
158 FEwrong_type_argument(@[byte], byte);
159 }
160 /* INV cl_code_char can return NIL for bytes outside of the char range.
161 ecl_char_code will signal a condition then. */
162 return byte == OBJNULL ? EOF : ecl_char_code(cl_code_char(byte));
163 }
164
165 static ecl_character
166 seq_byte_read_char(cl_object strm)
167 { return char_byte(seq_read_object(strm)); }
168
169 static ecl_character
170 seq_byte_peek_char(cl_object strm)
171 { return char_byte(seq_peek_object(strm)); }
172
173 static ecl_character
174 seq_byte_write_char(cl_object strm, ecl_character c)
175 { seq_write_object(strm, cl_char_code(ECL_CODE_CHAR(c))); return c; }
176
177 static void
178 seq_byte_unread_char(cl_object strm, ecl_character c)
179 { seq_unread_object(strm, cl_char_code(ECL_CODE_CHAR(c))); }
180
181 /**********************************************************************
182 * SEQUENCE INPUT STREAMS
183 */
184
185 /* Keep in mind that this function assumes that the element type is the octet
186 (we'd need to factor the byte size to the change position and test limit),
187 and that for :ucs-2 and :ucs-4 .byte_buffer is NULL. -- jd 2025-07-29 */
188 static cl_index
189 seq_in_read_byte8(cl_object strm, unsigned char *c, cl_index n)
190 {
191 cl_fixnum curr_pos = SEQ_STREAM_POSITION(strm);
192 cl_fixnum last = SEQ_INPUT_LIMIT(strm);
193 cl_fixnum delta = last - curr_pos;
194 if (delta > 0) {
195 cl_object vector = SEQ_STREAM_VECTOR(strm);
196 if (delta > n) delta = n;
197 ecl_copy(c, vector->vector.self.bc + curr_pos, delta);
198 SEQ_STREAM_POSITION(strm) += delta;
199 return delta;
200 }
201 return 0;
202 }
203
204 static void
205 seq_in_unread_char(cl_object strm, ecl_character c)
206 {
207 int flags = strm->stream.flags;
208 ecl_eformat_unread_char(strm, c);
209 if (c == ECL_CHAR_CODE_NEWLINE
210 && (flags & ECL_STREAM_CR)
211 && (flags & ECL_STREAM_LF))
212 SEQ_STREAM_POSITION(strm) -= 2;
213 else
214 SEQ_STREAM_POSITION(strm) -= 1;
215 }
216
217 static void
218 seq_in_unread_byte(cl_object strm, cl_object byte)
219 {
220 unlikely_if(SEQ_STREAM_POSITION(strm) <= 0) {
221 ecl_unread_error(strm);
222 }
223 SEQ_STREAM_POSITION(strm) -= 1;
224 }
225
226 #ifdef ecl_uint16_t
227 static ecl_character
228 seq_in_ucs2_read_char(cl_object strm)
229 {
230 cl_fixnum curr_pos = SEQ_STREAM_POSITION(strm);
231 cl_fixnum last = SEQ_INPUT_LIMIT(strm);
232 if (curr_pos >= last) {
233 return EOF;
234 }
235 cl_object vector = SEQ_STREAM_VECTOR(strm);
236 ecl_character c = vector->vector.self.b16[curr_pos++];
237 cl_object err;
238 if (c >= 0xD800 && c <= 0xDBFF) {
239 if (curr_pos >= last) {
240 err = ecl_list1(ecl_make_fixnum(c));
241 goto DECODING_ERROR;
242 }
243 ecl_character aux = vector->vector.self.b16[curr_pos++];
244 if (aux < 0xDC00 || aux > 0xDFFF) {
245 err = cl_list(2, ecl_make_fixnum(c), ecl_make_fixnum(aux));
246 goto DECODING_ERROR;
247 }
248 c = ((c & 0x3FF) << 10) + (aux & 0x3FF) + 0x10000;
249 }
250 SEQ_STREAM_POSITION(strm) = curr_pos;
251 return c;
252 cl_object code;
253 DECODING_ERROR:
254 code = _ecl_funcall4(@'ext::decoding-error', strm,
255 cl_stream_external_format(strm),
256 err);
257 if (Null(code)) {
258 /* Go for next character */
259 return seq_in_ucs2_read_char(strm);
260 } else {
261 /* Return supplied character */
262 return ecl_char_code(code);
263 }
264 }
265
266 static void
267 seq_in_ucs2_unread_char(cl_object strm, ecl_character c)
268 {
269 if (c >= 0x10000) {
270 SEQ_STREAM_POSITION(strm) -= 2;
271 } else {
272 SEQ_STREAM_POSITION(strm) -= 1;
273 }
274 }
275 #endif
276
277 #ifdef ecl_uint32_t
278 static ecl_character
279 seq_in_ucs4_read_char(cl_object strm)
280 {
281 cl_fixnum curr_pos = SEQ_STREAM_POSITION(strm);
282 if (curr_pos >= SEQ_INPUT_LIMIT(strm)) {
283 return EOF;
284 }
285 cl_object vector = SEQ_STREAM_VECTOR(strm);
286 SEQ_STREAM_POSITION(strm) += 1;
287 return vector->vector.self.b32[curr_pos];
288 }
289
290 static void
291 seq_in_ucs4_unread_char(cl_object strm, ecl_character c)
292 {
293 SEQ_STREAM_POSITION(strm) -= 1;
294 }
295 #endif
296
297 static int
298 seq_in_listen(cl_object strm)
299 {
300 if (SEQ_STREAM_POSITION(strm) < SEQ_INPUT_LIMIT(strm))
301 return ECL_LISTEN_AVAILABLE;
302 else
303 return ECL_LISTEN_EOF;
304 }
305
306 static cl_object
307 seq_in_get_position(cl_object strm)
308 {
309 return ecl_make_unsigned_integer(SEQ_STREAM_POSITION(strm));
310 }
311
312 static cl_object
313 seq_in_set_position(cl_object strm, cl_object pos)
314 {
315 cl_fixnum disp;
316 if (Null(pos)) {
317 disp = SEQ_INPUT_LIMIT(strm);
318 } else {
319 disp = ecl_to_size(pos);
320 if (disp >= SEQ_INPUT_LIMIT(strm)) {
321 disp = SEQ_INPUT_LIMIT(strm);
322 }
323 }
324 SEQ_STREAM_POSITION(strm) = disp;
325 return ECL_T;
326 }
327
328 static cl_object
329 seq_file_element_type(cl_object strm)
330 {
331 return IO_FILE_ELT_TYPE(strm);
332 }
333
334 const struct ecl_file_ops seq_in_ops = {
335 seq_in_read_byte8,
336 ecl_not_output_write_byte8,
337
338 ecl_binary_read_byte,
339 ecl_not_output_write_byte,
340 seq_in_unread_byte,
341 ecl_generic_peek_byte,
342
343 ecl_eformat_read_char,
344 ecl_not_output_write_char,
345 seq_in_unread_char,
346 ecl_generic_peek_char,
347
348 ecl_generic_read_vector,
349 ecl_generic_write_vector,
350
351 seq_in_listen,
352 ecl_generic_void, /* clear-input */
353 ecl_not_output_clear_output,
354 ecl_not_output_finish_output,
355 ecl_not_output_force_output,
356
357 ecl_generic_always_true, /* input_p */
358 ecl_generic_always_false, /* output_p */
359 ecl_generic_always_false,
360 seq_file_element_type,
361
362 ecl_not_a_file_stream, /* length */
363 seq_in_get_position,
364 seq_in_set_position,
365 ecl_not_output_string_length,
366 ecl_unknown_column,
367
368 ecl_not_a_file_stream,
369 ecl_not_a_file_stream,
370
371 ecl_generic_close
372 };
373
374 static cl_object
375 make_sequence_input_stream(cl_object vector, cl_index istart, cl_index iend,
376 cl_object external_format)
377 {
378 cl_object strm;
379 cl_elttype type;
380 cl_object type_name;
381 int byte_size;
382 int flags = 0;
383 if (!ECL_VECTORP(vector)) {
384 FEwrong_type_nth_arg(@[ext::make-sequence-input-stream], 1, vector, @[vector]);
385 }
386 type = ecl_array_elttype(vector);
387 type_name = ecl_elttype_to_symbol(type);
388 /* ecl_normalize_stream_element_type errors on illegal element type. */
389 byte_size = ecl_normalize_stream_element_type(type_name);
390 /* Character streams always get some external format. For binary
391 * sequences it has to be explicitly mentioned. */
392 strm = ecl_alloc_stream();
393 strm->stream.ops = ecl_duplicate_dispatch_table(&seq_in_ops);
394 strm->stream.mode = (short)ecl_smm_sequence_input;
395 if (!byte_size && Null(external_format)) {
396 external_format = @':default';
397 }
398 if (ecl_aet_size[type] == 1 && !Null(external_format)) {
399 ecl_set_stream_elt_type(strm, byte_size, flags, external_format);
400 /* Override byte size */
401 if (byte_size) strm->stream.byte_size = 8;
402 }
403 #ifdef ecl_uint16_t
404 else if (ecl_aet_size[type] == 2 && external_format == @':ucs-2') {
405 SEQ_STREAM_ELT_TYPE(strm) = @'character';
406 strm->stream.format = @':ucs-2';
407 strm->stream.byte_size = 2*8;
408 /* decoding */
409 strm->stream.ops->read_char = seq_in_ucs2_read_char;
410 strm->stream.ops->unread_char = seq_in_ucs2_unread_char;
411 /* identity */
412 strm->stream.ops->read_byte = seq_byte_read_byte;
413 strm->stream.ops->peek_byte = seq_byte_peek_byte;
414 strm->stream.ops->unread_byte = seq_byte_unread_byte;
415 }
416 #endif
417 #ifdef ecl_uint32_t
418 else if (ecl_aet_size[type] == 4 && external_format == @':ucs-4') {
419 SEQ_STREAM_ELT_TYPE(strm) = @'character';
420 strm->stream.format = @':ucs-4';
421 strm->stream.byte_size = 4*8;
422 /* decoding */
423 strm->stream.ops->read_char = seq_in_ucs4_read_char;
424 strm->stream.ops->unread_char = seq_in_ucs4_unread_char;
425 /* identity */
426 strm->stream.ops->read_byte = seq_byte_read_byte;
427 strm->stream.ops->peek_byte = seq_byte_peek_byte;
428 strm->stream.ops->unread_byte = seq_byte_unread_byte;
429 }
430 #endif
431 else if(!byte_size && external_format == @':default') {
432 /* char vector -> native bivalent stream */
433 SEQ_STREAM_ELT_TYPE(strm) = @'character';
434 /* identity */
435 strm->stream.ops->read_char = seq_char_read_char;
436 strm->stream.ops->peek_char = seq_char_peek_char;
437 strm->stream.ops->unread_char = seq_char_unread_char;
438 /* char-code */
439 strm->stream.ops->read_byte = seq_char_read_byte;
440 strm->stream.ops->peek_byte = seq_char_peek_byte;
441 strm->stream.ops->unread_byte = seq_char_unread_byte;
442 }
443 else if(Null(external_format)) {
444 /* byte vector -> native bivalent stream */
445 ecl_set_stream_elt_type(strm, byte_size, flags, external_format);
446 /* code-char */
447 strm->stream.ops->read_char = seq_byte_read_char;
448 strm->stream.ops->peek_char = seq_byte_peek_char;
449 strm->stream.ops->unread_char = seq_byte_unread_char;
450 /* identity */
451 strm->stream.ops->read_byte = seq_byte_read_byte;
452 strm->stream.ops->peek_byte = seq_byte_peek_byte;
453 strm->stream.ops->unread_byte = seq_byte_unread_byte;
454 }
455 else {
456 FEerror("Illegal combination of external-format ~A and input vector ~A "
457 "for MAKE-SEQUENCE-INPUT-STREAM.~%", 2, external_format, vector);
458 }
459 SEQ_STREAM_VECTOR(strm) = vector;
460 SEQ_STREAM_POSITION(strm) = istart;
461 SEQ_INPUT_VECTOR_END(strm) = iend;
462 return strm;
463 }
464
465 @(defun ext::make_sequence_input_stream (vector &key
466 (start ecl_make_fixnum(0))
467 (end ECL_NIL)
468 (external_format ECL_NIL))
469 cl_index_pair p;
470 cl_object strm;
471 @
472 p = ecl_vector_start_end(@[ext::make-sequence-input-stream],
473 vector, start, end);
474 strm = make_sequence_input_stream(vector, p.start, p.end,
475 external_format);
476 if (Null(end))
477 strm->stream.flags |= ECL_STREAM_USE_VECTOR_FILLP;
478 @(return strm)
479 @)
480
481 /**********************************************************************
482 * SEQUENCE OUTPUT STREAMS
483 */
484
485 static void
486 seq_out_enlarge_vector(cl_object strm)
487 {
488 cl_object vector = SEQ_STREAM_VECTOR(strm);
489 si_adjust_vector(vector, ecl_ash(ecl_make_fixnum(vector->vector.dim), 1));
490 SEQ_STREAM_VECTOR(strm) = vector;
491 }
492
493 /* Keep in mind that this function assumes that the element type is the octet
494 (we'd need to factor the byte size to the change position and test limit),
495 and that for :ucs-2 and :ucs-4 .byte_buffer is NULL. -- jd 2025-07-29 */
496 static cl_index
497 seq_out_write_byte8(cl_object strm, unsigned char *c, cl_index n)
498 {
499 AGAIN:
500 {
501 cl_object vector = SEQ_STREAM_VECTOR(strm);
502 cl_fixnum curr_pos = SEQ_STREAM_POSITION(strm);
503 cl_fixnum last = vector->vector.dim;
504 cl_fixnum delta = last - curr_pos;
505 if (delta < n) {
506 seq_out_enlarge_vector(strm);
507 goto AGAIN;
508 }
509 ecl_copy(vector->vector.self.bc + curr_pos, c, n);
510 SEQ_STREAM_POSITION(strm) = curr_pos += n;
511 if (vector->vector.fillp < curr_pos)
512 vector->vector.fillp = curr_pos;
513 }
514 return n;
515 }
516
517 #ifdef ecl_uint16_t
518 static ecl_character
519 seq_out_ucs2_write_char(cl_object strm, ecl_character c)
520 {
521 AGAIN:
522 {
523 cl_object vector = SEQ_STREAM_VECTOR(strm);
524 cl_fixnum curr_pos = SEQ_STREAM_POSITION(strm);
525 cl_fixnum n = (c >= 0x10000) ? 2 : 1;
526 if (vector->vector.dim - curr_pos < n) {
527 seq_out_enlarge_vector(strm);
528 goto AGAIN;
529 }
530 if (c >= 0x10000) {
531 c -= 0x10000;
532 vector->vector.self.b16[curr_pos++] = (c >> 10) | 0xD800;
533 vector->vector.self.b16[curr_pos++] = (c & 0x3FF) | 0xDC00;
534 } else {
535 vector->vector.self.b16[curr_pos++] = c;
536 }
537 SEQ_STREAM_POSITION(strm) = curr_pos;
538 if (vector->vector.fillp < curr_pos)
539 vector->vector.fillp = curr_pos;
540 }
541 return c;
542 }
543 #endif
544
545 #ifdef ecl_uint32_t
546 static ecl_character
547 seq_out_ucs4_write_char(cl_object strm, ecl_character c)
548 {
549 AGAIN:
550 {
551 cl_object vector = SEQ_STREAM_VECTOR(strm);
552 cl_fixnum curr_pos = SEQ_STREAM_POSITION(strm);
553 if (vector->vector.dim - curr_pos < 1) {
554 seq_out_enlarge_vector(strm);
555 goto AGAIN;
556 }
557 vector->vector.self.b32[curr_pos++] = c;
558 SEQ_STREAM_POSITION(strm) = curr_pos;
559 if (vector->vector.fillp < curr_pos)
560 vector->vector.fillp = curr_pos;
561 }
562 return c;
563 }
564 #endif
565
566 static cl_object
567 seq_out_get_position(cl_object strm)
568 {
569 return ecl_make_unsigned_integer(SEQ_STREAM_POSITION(strm));
570 }
571
572 static cl_object
573 seq_out_set_position(cl_object strm, cl_object pos)
574 {
575 cl_object vector = SEQ_STREAM_VECTOR(strm);
576 cl_fixnum disp;
577 if (Null(pos)) {
578 disp = vector->vector.fillp;
579 } else {
580 disp = ecl_to_size(pos);
581 if (disp >= vector->vector.dim) {
582 disp = vector->vector.fillp;
583 }
584 }
585 SEQ_STREAM_POSITION(strm) = disp;
586 return ECL_T;
587 }
588
589 const struct ecl_file_ops seq_out_ops = {
590 ecl_not_input_read_byte8,
591 seq_out_write_byte8,
592
593 ecl_not_input_read_byte,
594 ecl_binary_write_byte,
595 ecl_not_input_unread_byte,
596 ecl_generic_peek_byte,
597
598 ecl_not_input_read_char,
599 ecl_eformat_write_char,
600 ecl_not_input_unread_char,
601 ecl_generic_peek_char,
602
603 ecl_generic_read_vector,
604 ecl_generic_write_vector,
605
606 ecl_not_input_listen,
607 ecl_not_input_clear_input,
608 ecl_generic_void, /* clear-output */
609 ecl_generic_void, /* finish-output */
610 ecl_generic_void, /* force-output */
611
612 ecl_generic_always_false, /* input_p */
613 ecl_generic_always_true, /* output_p */
614 ecl_generic_always_false,
615 seq_file_element_type,
616
617 ecl_not_a_file_stream, /* length */
618 seq_out_get_position,
619 seq_out_set_position,
620 ecl_not_output_string_length,
621 ecl_generic_column,
622
623 ecl_not_a_file_stream,
624 ecl_not_a_file_stream,
625
626 ecl_generic_close
627 };
628
629 static cl_object
630 make_sequence_output_stream(cl_object vector, cl_object external_format)
631 {
632 cl_object strm;
633 cl_elttype type;
634 cl_object type_name;
635 int byte_size;
636 int flags = 0;
637 if (!ECL_VECTORP(vector)) {
638 FEwrong_type_nth_arg(@[ext::make-sequence-output-stream], 1, vector, @[vector]);
639 }
640 type = ecl_array_elttype(vector);
641 type_name = ecl_elttype_to_symbol(type);
642 /* ecl_normalize_stream_element_type errors on illegal element type. */
643 byte_size = ecl_normalize_stream_element_type(type_name);
644 /* Character streams always get some external format. For binary
645 * sequences it has to be explicitly mentioned. */
646 strm = ecl_alloc_stream();
647 strm->stream.ops = ecl_duplicate_dispatch_table(&seq_out_ops);
648 strm->stream.mode = (short)ecl_smm_sequence_output;
649 if (!byte_size && Null(external_format)) {
650 external_format = @':default';
651 }
652 if (ecl_aet_size[type] == 1 && !Null(external_format)) {
653 /* If elements of the stream are byte8, then we can convert them on fly. */
654 ecl_set_stream_elt_type(strm, byte_size, flags, external_format);
655 /* Override byte size */
656 if (byte_size) strm->stream.byte_size = 8;
657 }
658 #ifdef ecl_uint16_t
659 else if (ecl_aet_size[type] == 2 && external_format == @':ucs-2') {
660 SEQ_STREAM_ELT_TYPE(strm) = @'character';
661 strm->stream.format = @':ucs-2';
662 strm->stream.byte_size = 2*8;
663 strm->stream.ops->write_char = seq_out_ucs2_write_char;
664 strm->stream.ops->write_byte = seq_byte_write_byte;
665 }
666 #endif
667 #ifdef ecl_uint32_t
668 else if (ecl_aet_size[type] == 4 && external_format == @':ucs-4') {
669 SEQ_STREAM_ELT_TYPE(strm) = @'character';
670 strm->stream.format = @':ucs-4';
671 strm->stream.byte_size = 4*8;
672 strm->stream.ops->write_char = seq_out_ucs4_write_char;
673 strm->stream.ops->write_byte = seq_byte_write_byte;
674 }
675 #endif
676 else if(!byte_size && external_format == @':default') {
677 /* char vector -> native bivalent stream */
678 SEQ_STREAM_ELT_TYPE(strm) = @'character';
679 strm->stream.ops->write_char = seq_char_write_char;
680 strm->stream.ops->write_byte = seq_char_write_byte;
681 }
682 else if(Null(external_format)) {
683 /* byte vector -> native bivalent stream */
684 ecl_set_stream_elt_type(strm, byte_size, flags, external_format);
685 strm->stream.ops->write_char = seq_byte_write_char;
686 strm->stream.ops->write_byte = seq_byte_write_byte;
687 }
688 else {
689 FEerror("Illegal combination of external-format ~A and output vector ~A "
690 "for MAKE-SEQUENCE-OUTPUT-STREAM.~%", 2, external_format, vector);
691 }
692 SEQ_STREAM_VECTOR(strm) = vector;
693 SEQ_STREAM_POSITION(strm) = vector->vector.fillp;
694 return strm;
695 }
696
697 @(defun ext::make_sequence_output_stream (vector &key (external_format ECL_NIL))
698 @
699 @(return make_sequence_output_stream(vector, external_format));
700 @)
701
702 /*******************************tl***************************************
703 * SEQUENCES I/O
704 */
705
706 void
707 writestr_stream(const char *s, cl_object strm)
708 {
709 cl_object buffer = si_get_buffer_string();
710 cl_index size = ecl_fixnum(cl_array_total_size(buffer));
711 cl_index i = 0;
712 while (*s != '\0') {
713 ecl_char_set(buffer, i++, (ecl_character) *s++);
714 if (i >= size) {
715 si_fill_pointer_set(buffer, ecl_make_fixnum(size));
716 si_do_write_sequence(buffer, strm, ecl_make_fixnum(0), ECL_NIL);
717 i = 0;
718 }
719 }
720 si_fill_pointer_set(buffer, ecl_make_fixnum(i));
721 si_do_write_sequence(buffer, strm, ecl_make_fixnum(0), ECL_NIL);
722 si_put_buffer_string(buffer);
723 }
724
725
726 cl_object
727 si_do_write_sequence(cl_object seq, cl_object stream, cl_object s, cl_object e)
728 {
729 const struct ecl_file_ops *ops;
730 cl_fixnum start,limit,end;
731
732 /* Since we have called ecl_length(), we know that SEQ is a valid
733 sequence. Therefore, we only need to check the type of the
734 object, and seq == ECL_NIL i.f.f. t = t_symbol */
735 limit = ecl_length(seq);
736 if (ecl_unlikely(!ECL_FIXNUMP(s) ||
737 ((start = ecl_fixnum(s)) < 0) ||
738 (start > limit))) {
739 FEwrong_type_key_arg(@[write-sequence], @[:start], s,
740 ecl_make_integer_type(ecl_make_fixnum(0),
741 ecl_make_fixnum(limit-1)));
742 }
743 if (e == ECL_NIL) {
744 end = limit;
745 } else if (ecl_unlikely(!ECL_FIXNUMP(e) ||
746 ((end = ecl_fixnum(e)) < 0) ||
747 (end > limit))) {
748 FEwrong_type_key_arg(@[write-sequence], @[:end], e,
749 ecl_make_integer_type(ecl_make_fixnum(0),
750 ecl_make_fixnum(limit)));
751 }
752 if (end <= start) {
753 goto OUTPUT;
754 }
755 ops = ecl_stream_dispatch_table(stream);
756 if (LISTP(seq)) {
757 cl_object elt_type = cl_stream_element_type(stream);
758 bool ischar = (elt_type == @'base-char') || (elt_type == @'character');
759 cl_object s = ecl_nthcdr(start, seq);
760 loop_for_in(s) {
761 if (start < end) {
762 cl_object elt = CAR(s);
763 if (ischar)
764 ops->write_char(stream, ecl_char_code(elt));
765 else
766 ops->write_byte(stream, elt);
767 start++;
768 } else {
769 goto OUTPUT;
770 }
771 } end_loop_for_in;
772 } else {
773 ops->write_vector(stream, seq, start, end);
774 }
775 OUTPUT:
776 @(return seq);
777 }
778
779 cl_object
780 si_do_read_sequence(cl_object seq, cl_object stream, cl_object s, cl_object e)
781 {
782 const struct ecl_file_ops *ops;
783 cl_fixnum start,limit,end;
784
785 /* Since we have called ecl_length(), we know that SEQ is a valid
786 sequence. Therefore, we only need to check the type of the
787 object, and seq == ECL_NIL i.f.f. t = t_symbol */
788 limit = ecl_length(seq);
789 if (ecl_unlikely(!ECL_FIXNUMP(s) ||
790 ((start = ecl_fixnum(s)) < 0) ||
791 (start > limit))) {
792 FEwrong_type_key_arg(@[read-sequence], @[:start], s,
793 ecl_make_integer_type(ecl_make_fixnum(0),
794 ecl_make_fixnum(limit-1)));
795 }
796 if (e == ECL_NIL) {
797 end = limit;
798 } else if (ecl_unlikely(!ECL_FIXNUMP(e) ||
799 ((end = ecl_fixnum(e)) < 0) ||
800 (end > limit))) {
801 FEwrong_type_key_arg(@[read-sequence], @[:end], e,
802 ecl_make_integer_type(ecl_make_fixnum(0),
803 ecl_make_fixnum(limit)));
804 }
805 if (end <= start) {
806 goto OUTPUT;
807 }
808 ops = ecl_stream_dispatch_table(stream);
809 if (LISTP(seq)) {
810 cl_object elt_type = cl_stream_element_type(stream);
811 bool ischar = (elt_type == @'base-char') || (elt_type == @'character');
812 seq = ecl_nthcdr(start, seq);
813 loop_for_in(seq) {
814 if (start >= end) {
815 goto OUTPUT;
816 } else {
817 cl_object c;
818 if (ischar) {
819 int i = ops->read_char(stream);
820 if (i < 0) goto OUTPUT;
821 c = ECL_CODE_CHAR(i);
822 } else {
823 c = ops->read_byte(stream);
824 if (c == OBJNULL) goto OUTPUT;
825 }
826 ECL_RPLACA(seq, c);
827 start++;
828 }
829 } end_loop_for_in;
830 } else {
831 start = ops->read_vector(stream, seq, start, end);
832 }
833 OUTPUT:
834 @(return ecl_make_fixnum(start));
835 }