Debian Codegraph @

src/c/streams/strm_string.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_string.d - String Streams 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/internal.h>
18
19 /**********************************************************************
20 * STRING OUTPUT STREAMS
21 */
22
23 static ecl_character
24 str_out_write_char(cl_object strm, ecl_character c)
25 {
26 write_char_increment_column(strm, c);
27 ecl_string_push_extend(STRING_OUTPUT_STRING(strm), c);
28 return c;
29 }
30
31 static cl_object
32 str_out_element_type(cl_object strm)
33 {
34 cl_object string = STRING_OUTPUT_STRING(strm);
35 if (ECL_BASE_STRING_P(string))
36 return @'base-char';
37 return @'character';
38 }
39
40 static cl_object
41 str_out_get_position(cl_object strm)
42 {
43 return ecl_make_unsigned_integer(STRING_OUTPUT_STRING(strm)->base_string.fillp);
44 }
45
46 static cl_object
47 str_out_string_length(cl_object strm, cl_object string)
48 {
49 cl_fixnum l = 0;
50 switch (ecl_t_of(string)) {
51 #ifdef ECL_UNICODE
52 case t_string:
53 #endif
54 case t_base_string:
55 l = string->base_string.fillp;
56 break;
57 case t_character:
58 l = 1;
59 break;
60 default:
61 FEwrong_type_nth_arg(@[file-string-length], 2, string, @[string]);
62 }
63 return ecl_make_fixnum(l);
64 }
65
66 static cl_object
67 str_out_set_position(cl_object strm, cl_object pos)
68 {
69 cl_object string = STRING_OUTPUT_STRING(strm);
70 cl_fixnum disp;
71 if (Null(pos)) {
72 disp = strm->base_string.dim;
73 } else {
74 disp = ecl_to_size(pos);
75 }
76 if (disp < string->base_string.fillp) {
77 string->base_string.fillp = disp;
78 } else {
79 disp -= string->base_string.fillp;
80 while (disp-- > 0)
81 ecl_write_char(' ', strm);
82 }
83 return ECL_T;
84 }
85
86 const struct ecl_file_ops str_out_ops = {
87 ecl_not_binary_read_byte8,
88 ecl_not_output_write_byte8,
89
90 ecl_not_input_read_byte,
91 ecl_not_binary_write_byte,
92 ecl_not_input_unread_byte,
93 ecl_generic_peek_byte,
94
95 ecl_not_input_read_char,
96 str_out_write_char,
97 ecl_not_input_unread_char,
98 ecl_generic_peek_char,
99
100 ecl_generic_read_vector,
101 ecl_generic_write_vector,
102
103 ecl_not_input_listen,
104 ecl_not_input_clear_input,
105 ecl_generic_void, /* clear-output */
106 ecl_generic_void, /* finish-output */
107 ecl_generic_void, /* force-output */
108
109 ecl_generic_always_false, /* input_p */
110 ecl_generic_always_true, /* output_p */
111 ecl_generic_always_false,
112 str_out_element_type,
113
114 ecl_not_a_file_stream, /* length */
115 str_out_get_position,
116 str_out_set_position,
117 str_out_string_length,
118 ecl_generic_column,
119
120 ecl_not_a_file_stream,
121 ecl_not_a_file_stream,
122
123 ecl_generic_close
124 };
125
126
127 cl_object
128 si_make_string_output_stream_from_string(cl_object s)
129 {
130 cl_object strm = ecl_alloc_stream();
131 unlikely_if (!ECL_STRINGP(s) || !ECL_ARRAY_HAS_FILL_POINTER_P(s))
132 FEerror("~S is not a -string with a fill-pointer.", 1, s);
133 strm->stream.ops = ecl_duplicate_dispatch_table(&str_out_ops);
134 strm->stream.mode = (short)ecl_smm_string_output;
135 STRING_OUTPUT_STRING(strm) = s;
136 strm->stream.column = 0;
137 #if !defined(ECL_UNICODE)
138 strm->stream.format = @':pass-through';
139 strm->stream.flags = ECL_STREAM_DEFAULT_FORMAT;
140 strm->stream.byte_size = 8;
141 #else
142 if (ECL_BASE_STRING_P(s)) {
143 strm->stream.format = @':latin-1';
144 strm->stream.flags = ECL_STREAM_LATIN_1;
145 strm->stream.byte_size = 8;
146 } else {
147 strm->stream.format = @':ucs-4';
148 strm->stream.flags = ECL_STREAM_UCS_4;
149 strm->stream.byte_size = 32;
150 }
151 #endif
152 @(return strm);
153 }
154
155 cl_object
156 ecl_make_string_output_stream(cl_index line_length, int extended)
157 {
158 #ifdef ECL_UNICODE
159 cl_object s = extended?
160 ecl_alloc_adjustable_extended_string(line_length) :
161 ecl_alloc_adjustable_base_string(line_length);
162 #else
163 cl_object s = ecl_alloc_adjustable_base_string(line_length);
164 #endif
165 return si_make_string_output_stream_from_string(s);
166 }
167
168 @(defun make-string-output-stream (&key (element_type @'character'))
169 int extended = 0;
170 @
171 if (element_type == @'base-char') {
172 (void)0;
173 } else if (element_type == @'character') {
174 #ifdef ECL_UNICODE
175 extended = 1;
176 #endif
177 } else if (!Null(_ecl_funcall3(@'subtypep', element_type, @'base-char'))) {
178 (void)0;
179 } else if (!Null(_ecl_funcall3(@'subtypep', element_type, @'character'))) {
180 #ifdef ECL_UNICODE
181 extended = 1;
182 #endif
183 } else {
184 FEerror("In MAKE-STRING-OUTPUT-STREAM, the argument :ELEMENT-TYPE (~A) must be a subtype of character",
185 1, element_type);
186 }
187 @(return ecl_make_string_output_stream(128, extended));
188 @)
189
190 cl_object
191 cl_get_output_stream_string(cl_object strm)
192 {
193 cl_object strng;
194 unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_string_output))
195 FEwrong_type_only_arg(@[get-output-stream-string],
196 strm, @[string-stream]);
197 strng = cl_copy_seq(STRING_OUTPUT_STRING(strm));
198 STRING_OUTPUT_STRING(strm)->base_string.fillp = 0;
199 @(return strng);
200 }
201
202 /**********************************************************************
203 * STRING INPUT STREAMS
204 */
205
206 static ecl_character
207 str_in_read_char(cl_object strm)
208 {
209 cl_fixnum curr_pos = STRING_INPUT_POSITION(strm);
210 ecl_character c;
211 if (curr_pos >= STRING_INPUT_LIMIT(strm)) {
212 c = EOF;
213 } else {
214 c = ecl_char(STRING_INPUT_STRING(strm), curr_pos);
215 STRING_INPUT_POSITION(strm) = curr_pos+1;
216 }
217 return c;
218 }
219
220 static void
221 str_in_unread_char(cl_object strm, ecl_character c)
222 {
223 cl_fixnum curr_pos = STRING_INPUT_POSITION(strm);
224 unlikely_if (curr_pos <= 0) {
225 ecl_unread_error(strm);
226 }
227 STRING_INPUT_POSITION(strm) = curr_pos - 1;
228 }
229
230 static ecl_character
231 str_in_peek_char(cl_object strm)
232 {
233 cl_index pos = STRING_INPUT_POSITION(strm);
234 if (pos >= STRING_INPUT_LIMIT(strm)) {
235 return EOF;
236 } else {
237 return ecl_char(STRING_INPUT_STRING(strm), pos);
238 }
239 }
240
241 static int
242 str_in_listen(cl_object strm)
243 {
244 if (STRING_INPUT_POSITION(strm) < STRING_INPUT_LIMIT(strm))
245 return ECL_LISTEN_AVAILABLE;
246 else
247 return ECL_LISTEN_EOF;
248 }
249
250 static cl_object
251 str_in_element_type(cl_object strm)
252 {
253 cl_object string = STRING_INPUT_STRING(strm);
254 if (ECL_BASE_STRING_P(string))
255 return @'base-char';
256 return @'character';
257 }
258
259 static cl_object
260 str_in_get_position(cl_object strm)
261 {
262 return ecl_make_unsigned_integer(STRING_INPUT_POSITION(strm));
263 }
264
265 static cl_object
266 str_in_set_position(cl_object strm, cl_object pos)
267 {
268 cl_fixnum disp;
269 if (Null(pos)) {
270 disp = STRING_INPUT_LIMIT(strm);
271 } else {
272 disp = ecl_to_size(pos);
273 if (disp >= STRING_INPUT_LIMIT(strm)) {
274 disp = STRING_INPUT_LIMIT(strm);
275 }
276 }
277 STRING_INPUT_POSITION(strm) = disp;
278 return ECL_T;
279 }
280
281 const struct ecl_file_ops str_in_ops = {
282 ecl_not_binary_read_byte8,
283 ecl_not_output_write_byte8,
284
285 ecl_not_binary_read_byte,
286 ecl_not_output_write_byte,
287 ecl_not_binary_write_byte,
288 ecl_not_binary_read_byte,
289
290 str_in_read_char,
291 ecl_not_output_write_char,
292 str_in_unread_char,
293 str_in_peek_char,
294
295 ecl_generic_read_vector,
296 ecl_generic_write_vector,
297
298 str_in_listen,
299 ecl_generic_void, /* clear-input */
300 ecl_not_output_clear_output,
301 ecl_not_output_finish_output,
302 ecl_not_output_force_output,
303
304 ecl_generic_always_true, /* input_p */
305 ecl_generic_always_false, /* output_p */
306 ecl_generic_always_false,
307 str_in_element_type,
308
309 ecl_not_a_file_stream, /* length */
310 str_in_get_position,
311 str_in_set_position,
312 ecl_not_output_string_length,
313 ecl_unknown_column,
314
315 ecl_not_a_file_stream,
316 ecl_not_a_file_stream,
317
318 ecl_generic_close
319 };
320
321 cl_object
322 ecl_make_string_input_stream(cl_object strng, cl_index istart, cl_index iend)
323 {
324 cl_object strm;
325
326 strm = ecl_alloc_stream();
327 strm->stream.ops = ecl_duplicate_dispatch_table(&str_in_ops);
328 strm->stream.mode = (short)ecl_smm_string_input;
329 STRING_INPUT_STRING(strm) = strng;
330 STRING_INPUT_POSITION(strm) = istart;
331 STRING_INPUT_LIMIT(strm) = iend;
332 #if !defined(ECL_UNICODE)
333 strm->stream.format = @':pass-through';
334 strm->stream.flags = ECL_STREAM_DEFAULT_FORMAT;
335 strm->stream.byte_size = 8;
336 #else
337 if (ECL_BASE_STRING_P(strng)) {
338 strm->stream.format = @':latin-1';
339 strm->stream.flags = ECL_STREAM_LATIN_1;
340 strm->stream.byte_size = 8;
341 } else {
342 strm->stream.format = @':ucs-4';
343 strm->stream.flags = ECL_STREAM_UCS_4;
344 strm->stream.byte_size = 32;
345 }
346 #endif
347 return strm;
348 }
349
350 @(defun make_string_input_stream (strng &o (istart ecl_make_fixnum(0)) iend)
351 cl_index_pair p;
352 @
353 strng = cl_string(strng);
354 p = ecl_vector_start_end(@[make-string-input-stream], strng, istart, iend);
355 @(return (ecl_make_string_input_stream(strng, p.start, p.end)));
356 @)