* vectors.c (s_scm_vector_p, list->vector, scm_vector)
[bpt/guile.git] / libguile / strports.c
1 /* Copyright (C) 1995,1996,1998,1999, 2000 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include "libguile/_scm.h"
48
49 #include <stdio.h>
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53
54 #include "libguile/unif.h"
55 #include "libguile/eval.h"
56 #include "libguile/ports.h"
57 #include "libguile/read.h"
58 #include "libguile/root.h"
59 #include "libguile/strings.h"
60 #include "libguile/modules.h"
61
62 #include "libguile/strports.h"
63
64 #ifdef HAVE_STRING_H
65 #include <string.h>
66 #endif
67
68 \f
69
70 /* {Ports - string ports}
71 *
72 */
73
74 /* NOTES:
75 write_buf/write_end point to the ends of the allocated string.
76 read_buf/read_end in principle point to the part of the string which
77 has been written to, but this is only updated after a flush.
78 read_pos and write_pos in principle should be equal, but this is only true
79 when rw_active is SCM_PORT_NEITHER.
80 */
81
82
83 static scm_bits_t scm_tc16_strport;
84
85
86 static int
87 stfill_buffer (SCM port)
88 {
89 scm_port *pt = SCM_PTAB_ENTRY (port);
90
91 if (pt->read_pos >= pt->read_end)
92 return EOF;
93 else
94 return scm_return_first_int (*pt->read_pos, port);
95 }
96
97 /* change the size of a port's string to new_size. this doesn't
98 change read_buf_size. */
99 static void
100 st_resize_port (scm_port *pt, off_t new_size)
101 {
102 SCM old_stream = SCM_PACK (pt->stream);
103 SCM new_stream = scm_makstr (new_size, 0);
104 unsigned long int old_size = SCM_STRING_LENGTH (old_stream);
105 unsigned long int min_size = min (old_size, new_size);
106 unsigned long int i;
107
108 off_t index = pt->write_pos - pt->write_buf;
109
110 pt->write_buf_size = new_size;
111
112 for (i = 0; i != min_size; ++i)
113 SCM_STRING_CHARS (new_stream) [i] = SCM_STRING_CHARS (old_stream) [i];
114
115 /* reset buffer. */
116 {
117 pt->stream = new_stream;
118 pt->read_buf = pt->write_buf = SCM_STRING_UCHARS (new_stream);
119 pt->read_pos = pt->write_pos = pt->write_buf + index;
120 pt->write_end = pt->write_buf + pt->write_buf_size;
121 pt->read_end = pt->read_buf + pt->read_buf_size;
122 }
123 }
124
125 /* amount by which write_buf is expanded. */
126 #define SCM_WRITE_BLOCK 80
127
128 /* ensure that write_pos < write_end by enlarging the buffer when
129 necessary. update read_buf to account for written chars. */
130 static void
131 st_flush (SCM port)
132 {
133 scm_port *pt = SCM_PTAB_ENTRY (port);
134
135 if (pt->write_pos == pt->write_end)
136 {
137 st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK);
138 }
139 pt->read_pos = pt->write_pos;
140 if (pt->read_pos > pt->read_end)
141 {
142 pt->read_end = (unsigned char *) pt->read_pos;
143 pt->read_buf_size = pt->read_end - pt->read_buf;
144 }
145 pt->rw_active = SCM_PORT_NEITHER;
146 }
147
148 static void
149 st_write (SCM port, const void *data, size_t size)
150 {
151 scm_port *pt = SCM_PTAB_ENTRY (port);
152 const char *input = (char *) data;
153
154 while (size > 0)
155 {
156 int space = pt->write_end - pt->write_pos;
157 int write_len = (size > space) ? space : size;
158
159 strncpy ((char *) pt->write_pos, input, write_len);
160 pt->write_pos += write_len;
161 size -= write_len;
162 input += write_len;
163 if (write_len == space)
164 st_flush (port);
165 }
166 }
167
168 static void
169 st_end_input (SCM port, int offset)
170 {
171 scm_port *pt = SCM_PTAB_ENTRY (port);
172
173 if (pt->read_pos - pt->read_buf < offset)
174 scm_misc_error ("st_end_input", "negative position", SCM_EOL);
175
176 pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
177 pt->rw_active = SCM_PORT_NEITHER;
178 }
179
180 static off_t
181 st_seek (SCM port, off_t offset, int whence)
182 {
183 scm_port *pt = SCM_PTAB_ENTRY (port);
184 off_t target;
185
186 if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
187 /* special case to avoid disturbing the unread-char buffer. */
188 {
189 if (pt->read_buf == pt->putback_buf)
190 {
191 target = pt->saved_read_pos - pt->saved_read_buf
192 - (pt->read_end - pt->read_pos);
193 }
194 else
195 {
196 target = pt->read_pos - pt->read_buf;
197 }
198 }
199 else
200 /* all other cases. */
201 {
202 if (pt->rw_active == SCM_PORT_WRITE)
203 st_flush (port);
204
205 if (pt->rw_active == SCM_PORT_READ)
206 scm_end_input (port);
207
208 switch (whence)
209 {
210 case SEEK_CUR:
211 target = pt->read_pos - pt->read_buf + offset;
212 break;
213 case SEEK_END:
214 target = pt->read_end - pt->read_buf + offset;
215 break;
216 default: /* SEEK_SET */
217 target = offset;
218 break;
219 }
220
221 if (target < 0)
222 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
223
224 if (target >= pt->write_buf_size)
225 {
226 if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
227 {
228 if (target > pt->write_buf_size)
229 {
230 scm_misc_error ("st_seek",
231 "seek past end of read-only strport",
232 SCM_EOL);
233 }
234 }
235 else
236 {
237 st_resize_port (pt, target + (target == pt->write_buf_size
238 ? SCM_WRITE_BLOCK
239 : 0));
240 }
241 }
242 pt->read_pos = pt->write_pos = pt->read_buf + target;
243 if (pt->read_pos > pt->read_end)
244 {
245 pt->read_end = (unsigned char *) pt->read_pos;
246 pt->read_buf_size = pt->read_end - pt->read_buf;
247 }
248 }
249 return target;
250 }
251
252 static void
253 st_truncate (SCM port, off_t length)
254 {
255 scm_port *pt = SCM_PTAB_ENTRY (port);
256
257 if (length > pt->write_buf_size)
258 st_resize_port (pt, length);
259
260 pt->read_buf_size = length;
261 pt->read_end = pt->read_buf + length;
262 if (pt->read_pos > pt->read_end)
263 pt->read_pos = pt->read_end;
264
265 if (pt->write_pos > pt->read_end)
266 pt->write_pos = pt->read_end;
267 }
268
269 SCM
270 scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
271 {
272 SCM z;
273 scm_port *pt;
274 int str_len;
275
276 SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
277 SCM_ASSERT (SCM_STRINGP (str), str, SCM_ARG1, caller);
278 str_len = SCM_STRING_LENGTH (str);
279 if (SCM_INUM (pos) > str_len)
280 scm_out_of_range (caller, pos);
281 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
282 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
283 SCM_NEWCELL (z);
284 SCM_DEFER_INTS;
285 pt = scm_add_to_port_table (z);
286 SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes);
287 SCM_SETPTAB_ENTRY (z, pt);
288 SCM_SETSTREAM (z, SCM_UNPACK (str));
289 pt->write_buf = pt->read_buf = SCM_STRING_UCHARS (str);
290 pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
291 pt->write_buf_size = pt->read_buf_size = str_len;
292 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
293
294 pt->rw_random = 1;
295
296 SCM_ALLOW_INTS;
297
298 /* ensure write_pos is writable. */
299 if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
300 st_flush (z);
301 return z;
302 }
303
304 /* create a new string from a string port's buffer. */
305 SCM scm_strport_to_string (SCM port)
306 {
307 scm_port *pt = SCM_PTAB_ENTRY (port);
308
309 if (pt->rw_active == SCM_PORT_WRITE)
310 st_flush (port);
311 return scm_makfromstr ((char *) pt->read_buf, pt->read_buf_size, 0);
312 }
313
314 SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0,
315 (SCM proc),
316 "Calls the one-argument procedure @var{proc} with a newly created output\n"
317 "port. When the function returns, the string composed of the characters\n"
318 "written into the port is returned.")
319 #define FUNC_NAME s_scm_call_with_output_string
320 {
321 SCM p;
322
323 p = scm_mkstrport (SCM_INUM0,
324 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
325 SCM_OPN | SCM_WRTNG,
326 FUNC_NAME);
327 scm_apply (proc, p, scm_listofnull);
328
329 return scm_strport_to_string (p);
330 }
331 #undef FUNC_NAME
332
333
334
335 /* Return a Scheme string obtained by printing a given object.
336 */
337
338
339 SCM
340 scm_strprint_obj (SCM obj)
341 {
342 SCM str;
343 SCM port;
344
345 str = scm_makstr (0, 0);
346 port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj");
347 scm_prin1 (obj, port, 1);
348 {
349 return scm_strport_to_string (port);
350 }
351 }
352
353
354
355
356 SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0,
357 (SCM str, SCM proc),
358 "Calls the one-argument procedure @var{proc} with a newly created input\n"
359 "port from which @var{string}'s contents may be read. The value yielded\n"
360 "by the @var{proc} is returned.")
361 #define FUNC_NAME s_scm_call_with_input_string
362 {
363 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
364 return scm_apply (proc, p, scm_listofnull);
365 }
366 #undef FUNC_NAME
367
368
369
370 /* Given a null-terminated string EXPR containing a Scheme expression
371 read it, and return it as an SCM value. */
372 SCM
373 scm_read_0str (char *expr)
374 {
375 SCM port = scm_mkstrport (SCM_INUM0,
376 scm_makfrom0str (expr),
377 SCM_OPN | SCM_RDNG,
378 "scm_eval_0str");
379 SCM form;
380
381 /* Read expressions from that port; ignore the values. */
382 form = scm_read (port);
383
384 scm_close_port (port);
385 return form;
386 }
387
388 /* Given a null-terminated string EXPR containing Scheme program text,
389 evaluate it, and return the result of the last expression evaluated. */
390 SCM
391 scm_eval_0str (const char *expr)
392 {
393 return scm_eval_string (scm_makfrom0str (expr));
394 }
395
396
397 SCM_DEFINE (scm_eval_string, "eval-string", 1, 0, 0,
398 (SCM string),
399 "Evaluate @var{string} as the text representation of a Scheme\n"
400 "form or forms, and return whatever value they produce.\n"
401 "Evaluation takes place in the environment returned by the\n"
402 "procedure @code{interaction-environment}.")
403 #define FUNC_NAME s_scm_eval_string
404 {
405 SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
406 "scm_eval_0str");
407 SCM form;
408 SCM ans = SCM_UNSPECIFIED;
409 SCM module = scm_interaction_environment ();
410
411 /* Read expressions from that port; ignore the values. */
412 while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
413 ans = scm_eval_x (form, module);
414
415 /* Don't close the port here; if we re-enter this function via a
416 continuation, then the next time we enter it, we'll get an error.
417 It's a string port anyway, so there's no advantage to closing it
418 early. */
419
420 return ans;
421 }
422 #undef FUNC_NAME
423
424 static scm_bits_t
425 scm_make_stptob ()
426 {
427 scm_bits_t tc = scm_make_port_type ("string", stfill_buffer, st_write);
428
429 scm_set_port_mark (tc, scm_markstream);
430 scm_set_port_end_input (tc, st_end_input);
431 scm_set_port_flush (tc, st_flush);
432 scm_set_port_seek (tc, st_seek);
433 scm_set_port_truncate (tc, st_truncate);
434
435 return tc;
436 }
437
438 void
439 scm_init_strports ()
440 {
441 scm_tc16_strport = scm_make_stptob ();
442
443 #ifndef SCM_MAGIC_SNARFER
444 #include "libguile/strports.x"
445 #endif
446 }
447
448
449 /*
450 Local Variables:
451 c-file-style: "gnu"
452 End:
453 */