(scm_object_to_string): Takes an optional argument.
[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 #include "libguile/validate.h"
62
63 #include "libguile/strports.h"
64
65 #ifdef HAVE_STRING_H
66 #include <string.h>
67 #endif
68
69 \f
70
71 /* {Ports - string ports}
72 *
73 */
74
75 /* NOTES:
76 write_buf/write_end point to the ends of the allocated string.
77 read_buf/read_end in principle point to the part of the string which
78 has been written to, but this is only updated after a flush.
79 read_pos and write_pos in principle should be equal, but this is only true
80 when rw_active is SCM_PORT_NEITHER.
81 */
82
83
84 static scm_bits_t scm_tc16_strport;
85
86
87 static int
88 stfill_buffer (SCM port)
89 {
90 scm_port *pt = SCM_PTAB_ENTRY (port);
91
92 if (pt->read_pos >= pt->read_end)
93 return EOF;
94 else
95 return scm_return_first_int (*pt->read_pos, port);
96 }
97
98 /* change the size of a port's string to new_size. this doesn't
99 change read_buf_size. */
100 static void
101 st_resize_port (scm_port *pt, off_t new_size)
102 {
103 SCM old_stream = SCM_PACK (pt->stream);
104 SCM new_stream = scm_makstr (new_size, 0);
105 unsigned long int old_size = SCM_STRING_LENGTH (old_stream);
106 unsigned long int min_size = min (old_size, new_size);
107 unsigned long int i;
108
109 off_t index = pt->write_pos - pt->write_buf;
110
111 pt->write_buf_size = new_size;
112
113 for (i = 0; i != min_size; ++i)
114 SCM_STRING_CHARS (new_stream) [i] = SCM_STRING_CHARS (old_stream) [i];
115
116 /* reset buffer. */
117 {
118 pt->stream = new_stream;
119 pt->read_buf = pt->write_buf = SCM_STRING_UCHARS (new_stream);
120 pt->read_pos = pt->write_pos = pt->write_buf + index;
121 pt->write_end = pt->write_buf + pt->write_buf_size;
122 pt->read_end = pt->read_buf + pt->read_buf_size;
123 }
124 }
125
126 /* amount by which write_buf is expanded. */
127 #define SCM_WRITE_BLOCK 80
128
129 /* ensure that write_pos < write_end by enlarging the buffer when
130 necessary. update read_buf to account for written chars. */
131 static void
132 st_flush (SCM port)
133 {
134 scm_port *pt = SCM_PTAB_ENTRY (port);
135
136 if (pt->write_pos == pt->write_end)
137 {
138 st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK);
139 }
140 pt->read_pos = pt->write_pos;
141 if (pt->read_pos > pt->read_end)
142 {
143 pt->read_end = (unsigned char *) pt->read_pos;
144 pt->read_buf_size = pt->read_end - pt->read_buf;
145 }
146 pt->rw_active = SCM_PORT_NEITHER;
147 }
148
149 static void
150 st_write (SCM port, const void *data, size_t size)
151 {
152 scm_port *pt = SCM_PTAB_ENTRY (port);
153 const char *input = (char *) data;
154
155 while (size > 0)
156 {
157 int space = pt->write_end - pt->write_pos;
158 int write_len = (size > space) ? space : size;
159
160 strncpy ((char *) pt->write_pos, input, write_len);
161 pt->write_pos += write_len;
162 size -= write_len;
163 input += write_len;
164 if (write_len == space)
165 st_flush (port);
166 }
167 }
168
169 static void
170 st_end_input (SCM port, int offset)
171 {
172 scm_port *pt = SCM_PTAB_ENTRY (port);
173
174 if (pt->read_pos - pt->read_buf < offset)
175 scm_misc_error ("st_end_input", "negative position", SCM_EOL);
176
177 pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
178 pt->rw_active = SCM_PORT_NEITHER;
179 }
180
181 static off_t
182 st_seek (SCM port, off_t offset, int whence)
183 {
184 scm_port *pt = SCM_PTAB_ENTRY (port);
185 off_t target;
186
187 if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
188 /* special case to avoid disturbing the unread-char buffer. */
189 {
190 if (pt->read_buf == pt->putback_buf)
191 {
192 target = pt->saved_read_pos - pt->saved_read_buf
193 - (pt->read_end - pt->read_pos);
194 }
195 else
196 {
197 target = pt->read_pos - pt->read_buf;
198 }
199 }
200 else
201 /* all other cases. */
202 {
203 if (pt->rw_active == SCM_PORT_WRITE)
204 st_flush (port);
205
206 if (pt->rw_active == SCM_PORT_READ)
207 scm_end_input (port);
208
209 switch (whence)
210 {
211 case SEEK_CUR:
212 target = pt->read_pos - pt->read_buf + offset;
213 break;
214 case SEEK_END:
215 target = pt->read_end - pt->read_buf + offset;
216 break;
217 default: /* SEEK_SET */
218 target = offset;
219 break;
220 }
221
222 if (target < 0)
223 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
224
225 if (target >= pt->write_buf_size)
226 {
227 if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
228 {
229 if (target > pt->write_buf_size)
230 {
231 scm_misc_error ("st_seek",
232 "seek past end of read-only strport",
233 SCM_EOL);
234 }
235 }
236 else
237 {
238 st_resize_port (pt, target + (target == pt->write_buf_size
239 ? SCM_WRITE_BLOCK
240 : 0));
241 }
242 }
243 pt->read_pos = pt->write_pos = pt->read_buf + target;
244 if (pt->read_pos > pt->read_end)
245 {
246 pt->read_end = (unsigned char *) pt->read_pos;
247 pt->read_buf_size = pt->read_end - pt->read_buf;
248 }
249 }
250 return target;
251 }
252
253 static void
254 st_truncate (SCM port, off_t length)
255 {
256 scm_port *pt = SCM_PTAB_ENTRY (port);
257
258 if (length > pt->write_buf_size)
259 st_resize_port (pt, length);
260
261 pt->read_buf_size = length;
262 pt->read_end = pt->read_buf + length;
263 if (pt->read_pos > pt->read_end)
264 pt->read_pos = pt->read_end;
265
266 if (pt->write_pos > pt->read_end)
267 pt->write_pos = pt->read_end;
268 }
269
270 SCM
271 scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
272 {
273 SCM z;
274 scm_port *pt;
275 int str_len;
276
277 SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
278 SCM_ASSERT (SCM_STRINGP (str), str, SCM_ARG1, caller);
279 str_len = SCM_STRING_LENGTH (str);
280 if (SCM_INUM (pos) > str_len)
281 scm_out_of_range (caller, pos);
282 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
283 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
284 SCM_NEWCELL (z);
285 SCM_DEFER_INTS;
286 pt = scm_add_to_port_table (z);
287 SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes);
288 SCM_SETPTAB_ENTRY (z, pt);
289 SCM_SETSTREAM (z, SCM_UNPACK (str));
290 pt->write_buf = pt->read_buf = SCM_STRING_UCHARS (str);
291 pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
292 pt->write_buf_size = pt->read_buf_size = str_len;
293 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
294
295 pt->rw_random = 1;
296
297 SCM_ALLOW_INTS;
298
299 /* ensure write_pos is writable. */
300 if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
301 st_flush (z);
302 return z;
303 }
304
305 /* create a new string from a string port's buffer. */
306 SCM scm_strport_to_string (SCM port)
307 {
308 scm_port *pt = SCM_PTAB_ENTRY (port);
309
310 if (pt->rw_active == SCM_PORT_WRITE)
311 st_flush (port);
312 return scm_makfromstr ((char *) pt->read_buf, pt->read_buf_size, 0);
313 }
314
315 SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
316 (SCM obj, SCM printer),
317 "Return a Scheme string obtained by printing @var{obj}.\n"
318 "Printing function can be specified by the optional second\n"
319 "argument @var{printer} (default: @code{write}).")
320 #define FUNC_NAME s_scm_object_to_string
321 {
322 SCM str, port;
323
324 if (!SCM_UNBNDP (printer))
325 SCM_VALIDATE_PROC (2, printer);
326
327 str = scm_makstr (0, 0);
328 port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, FUNC_NAME);
329
330 if (SCM_UNBNDP (printer))
331 scm_write (obj, port);
332 else
333 scm_apply (printer, SCM_LIST2 (obj, port), SCM_EOL);
334
335 return scm_strport_to_string (port);
336 }
337 #undef FUNC_NAME
338
339 #if (SCM_DEBUG_DEPRECATED == 0)
340
341 SCM
342 scm_strprint_obj (SCM obj)
343 {
344 return scm_object_to_string (obj, SCM_UNDEFINED);
345 }
346
347 #endif /* (SCM_DEBUG_DEPRECATED == 0) */
348
349 SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0,
350 (SCM proc),
351 "Calls the one-argument procedure @var{proc} with a newly created output\n"
352 "port. When the function returns, the string composed of the characters\n"
353 "written into the port is returned.")
354 #define FUNC_NAME s_scm_call_with_output_string
355 {
356 SCM p;
357
358 p = scm_mkstrport (SCM_INUM0,
359 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
360 SCM_OPN | SCM_WRTNG,
361 FUNC_NAME);
362 scm_apply (proc, p, scm_listofnull);
363
364 return scm_strport_to_string (p);
365 }
366 #undef FUNC_NAME
367
368 SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0,
369 (SCM str, SCM proc),
370 "Calls the one-argument procedure @var{proc} with a newly created input\n"
371 "port from which @var{string}'s contents may be read. The value yielded\n"
372 "by the @var{proc} is returned.")
373 #define FUNC_NAME s_scm_call_with_input_string
374 {
375 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
376 return scm_apply (proc, p, scm_listofnull);
377 }
378 #undef FUNC_NAME
379
380
381
382 /* Given a null-terminated string EXPR containing a Scheme expression
383 read it, and return it as an SCM value. */
384 SCM
385 scm_read_0str (char *expr)
386 {
387 SCM port = scm_mkstrport (SCM_INUM0,
388 scm_makfrom0str (expr),
389 SCM_OPN | SCM_RDNG,
390 "scm_eval_0str");
391 SCM form;
392
393 /* Read expressions from that port; ignore the values. */
394 form = scm_read (port);
395
396 scm_close_port (port);
397 return form;
398 }
399
400 /* Given a null-terminated string EXPR containing Scheme program text,
401 evaluate it, and return the result of the last expression evaluated. */
402 SCM
403 scm_eval_0str (const char *expr)
404 {
405 return scm_eval_string (scm_makfrom0str (expr));
406 }
407
408
409 SCM_DEFINE (scm_eval_string, "eval-string", 1, 0, 0,
410 (SCM string),
411 "Evaluate @var{string} as the text representation of a Scheme\n"
412 "form or forms, and return whatever value they produce.\n"
413 "Evaluation takes place in the environment returned by the\n"
414 "procedure @code{interaction-environment}.")
415 #define FUNC_NAME s_scm_eval_string
416 {
417 SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
418 "scm_eval_0str");
419 SCM form;
420 SCM ans = SCM_UNSPECIFIED;
421 SCM module = scm_interaction_environment ();
422
423 /* Read expressions from that port; ignore the values. */
424 while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
425 ans = scm_eval_x (form, module);
426
427 /* Don't close the port here; if we re-enter this function via a
428 continuation, then the next time we enter it, we'll get an error.
429 It's a string port anyway, so there's no advantage to closing it
430 early. */
431
432 return ans;
433 }
434 #undef FUNC_NAME
435
436 static scm_bits_t
437 scm_make_stptob ()
438 {
439 scm_bits_t tc = scm_make_port_type ("string", stfill_buffer, st_write);
440
441 scm_set_port_mark (tc, scm_markstream);
442 scm_set_port_end_input (tc, st_end_input);
443 scm_set_port_flush (tc, st_flush);
444 scm_set_port_seek (tc, st_seek);
445 scm_set_port_truncate (tc, st_truncate);
446
447 return tc;
448 }
449
450 void
451 scm_init_strports ()
452 {
453 scm_tc16_strport = scm_make_stptob ();
454
455 #ifndef SCM_MAGIC_SNARFER
456 #include "libguile/strports.x"
457 #endif
458 }
459
460
461 /*
462 Local Variables:
463 c-file-style: "gnu"
464 End:
465 */