* ports.c (scm_seek): Add a special case for SEEK_CUR, offset 0,
[bpt/guile.git] / libguile / strports.c
CommitLineData
754c9491 1/* Copyright (C) 1995,1996,1998,1999 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
41\f
42
0f2d19dd 43#include "_scm.h"
681b9005
MD
44
45#include <stdio.h>
46#ifdef HAVE_UNISTD_H
47#include <unistd.h>
48#endif
49
20e6290e
JB
50#include "unif.h"
51#include "eval.h"
cd07a097 52#include "read.h"
20e6290e
JB
53
54#include "strports.h"
0f2d19dd 55
95b88819
GH
56#ifdef HAVE_STRING_H
57#include <string.h>
58#endif
59
0f2d19dd
JB
60\f
61
62/* {Ports - string ports}
63 *
64 */
65
3fe6190f
GH
66/* NOTES:
67 write_buf/write_end point to the ends of the allocated string.
68 read_buf/read_end in principle point to the part of the string which
69 has been written to, but this is only updated after a flush.
70 read_pos and write_pos in principle should be equal, but this is only true
61e452ba 71 when rw_active is SCM_PORT_NEITHER.
3fe6190f 72*/
1cc91f1b 73
ee149d03
JB
74static int
75stfill_buffer (SCM port)
0f2d19dd 76{
754c9491 77 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 78
ee149d03
JB
79 if (pt->read_pos >= pt->read_end)
80 return EOF;
81 else
5c070ca7 82 return scm_return_first (*pt->read_pos, port);
0f2d19dd
JB
83}
84
3fe6190f
GH
85/* change the size of a port's string to new_size. this doesn't
86 change read_buf_size. */
754c9491 87static void
3fe6190f 88st_resize_port (scm_port *pt, off_t new_size)
754c9491 89{
3fe6190f
GH
90 off_t index = pt->write_pos - pt->write_buf;
91
92 pt->write_buf_size = new_size;
93
94 scm_vector_set_length_x (pt->stream, SCM_MAKINUM (new_size));
754c9491 95
754c9491
JB
96 /* reset buffer in case reallocation moved the string. */
97 {
754c9491 98 pt->read_buf = pt->write_buf = SCM_CHARS (pt->stream);
3fe6190f
GH
99 pt->read_pos = pt->write_pos = pt->write_buf + index;
100 pt->write_end = pt->write_buf + pt->write_buf_size;
101 pt->read_end = pt->read_buf + pt->read_buf_size;
754c9491
JB
102 }
103}
104
3fe6190f
GH
105/* amount by which write_buf is expanded. */
106#define SCM_WRITE_BLOCK 80
107
108/* ensure that write_pos < write_end by enlarging the buffer when
109 necessary. update read_buf to account for written chars. */
ee149d03
JB
110static void
111st_flush (SCM port)
0f2d19dd 112{
754c9491 113 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03
JB
114
115 if (pt->write_pos == pt->write_end)
116 {
3fe6190f 117 st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK);
754c9491
JB
118 }
119 pt->read_pos = pt->write_pos;
3fe6190f
GH
120 if (pt->read_pos > pt->read_end)
121 {
122 pt->read_end = (unsigned char *) pt->read_pos;
123 pt->read_buf_size = pt->read_end - pt->read_buf;
124 }
61e452ba 125 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
126}
127
31703ab8
GH
128static void
129st_write (SCM port, void *data, size_t size)
130{
131 scm_port *pt = SCM_PTAB_ENTRY (port);
132 const char *input = (char *) data;
133
134 while (size > 0)
135 {
136 int space = pt->write_end - pt->write_pos;
137 int write_len = (size > space) ? space : size;
138
139 strncpy (pt->write_pos, input, write_len);
140 pt->write_pos += write_len;
141 size -= write_len;
142 input += write_len;
143 if (write_len == space)
144 st_flush (port);
145 }
146}
147
754c9491 148static void
affc96b5 149st_end_input (SCM port, int offset)
754c9491
JB
150{
151 scm_port *pt = SCM_PTAB_ENTRY (port);
152
153 pt->write_pos = (unsigned char *) pt->read_pos;
61e452ba 154 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
155}
156
157static off_t
158st_seek (SCM port, off_t offset, int whence)
159{
160 scm_port *pt = SCM_PTAB_ENTRY (port);
161 off_t target;
162
3fe6190f 163 /* we can assume at this point that pt->write_pos == pt->read_pos. */
754c9491
JB
164 switch (whence)
165 {
166 case SEEK_CUR:
3fe6190f 167 target = pt->read_pos - pt->read_buf + offset;
754c9491
JB
168 break;
169 case SEEK_END:
3fe6190f 170 target = pt->read_end - pt->read_buf + offset;
754c9491
JB
171 break;
172 default: /* SEEK_SET */
173 target = offset;
174 break;
175 }
176 if (target < 0)
3fe6190f
GH
177 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
178 if (target >= pt->write_buf_size)
754c9491 179 {
3fe6190f
GH
180 if (!(SCM_CAR (port) & SCM_WRTNG))
181 {
182 if (target > pt->write_buf_size)
183 {
184 scm_misc_error ("st_seek", "seek past end of read-only strport",
185 SCM_EOL);
186 }
187 }
188 else
189 {
190 st_resize_port (pt, target + (target == pt->write_buf_size
191 ? SCM_WRITE_BLOCK
192 : 0));
193 }
ee149d03 194 }
754c9491 195 pt->read_pos = pt->write_pos = pt->read_buf + target;
3fe6190f
GH
196 if (pt->read_pos > pt->read_end)
197 {
198 pt->read_end = (unsigned char *) pt->read_pos;
199 pt->read_buf_size = pt->read_end - pt->read_buf;
200 }
754c9491
JB
201 return target;
202}
203
204static void
affc96b5 205st_truncate (SCM port, off_t length)
754c9491
JB
206{
207 scm_port *pt = SCM_PTAB_ENTRY (port);
3fe6190f
GH
208
209 if (length > pt->write_buf_size)
210 st_resize_port (pt, length);
211
212 pt->read_buf_size = length;
213 pt->read_end = pt->read_buf + length;
214 if (pt->read_pos > pt->read_end)
215 pt->read_pos = pt->read_end;
754c9491 216
3fe6190f
GH
217 if (pt->write_pos > pt->read_end)
218 pt->write_pos = pt->read_end;
0f2d19dd
JB
219}
220
0f2d19dd
JB
221SCM
222scm_mkstrport (pos, str, modes, caller)
223 SCM pos;
224 SCM str;
225 long modes;
3eeba8d4 226 const char * caller;
0f2d19dd
JB
227{
228 SCM z;
754c9491
JB
229 scm_port *pt;
230 int str_len;
231
232 SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
233 SCM_ASSERT (SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, caller);
234 str_len = SCM_ROLENGTH (str);
235 if (SCM_INUM (pos) > str_len)
236 scm_out_of_range (caller, pos);
237 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
238 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
0f2d19dd
JB
239 SCM_NEWCELL (z);
240 SCM_DEFER_INTS;
241 pt = scm_add_to_port_table (z);
a6c64c3c 242 SCM_SETCAR (z, scm_tc16_strport | modes);
0f2d19dd 243 SCM_SETPTAB_ENTRY (z, pt);
754c9491 244 SCM_SETSTREAM (z, str);
ee149d03 245 pt->write_buf = pt->read_buf = SCM_ROCHARS (str);
754c9491
JB
246 pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
247 pt->write_buf_size = pt->read_buf_size = str_len;
248 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
3fe6190f 249
0de97b83 250 pt->rw_random = 1;
3fe6190f 251
0f2d19dd 252 SCM_ALLOW_INTS;
3fe6190f
GH
253
254 /* ensure write_pos is writable. */
754c9491
JB
255 if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
256 st_flush (z);
0f2d19dd
JB
257 return z;
258}
259
3fe6190f
GH
260/* create a new string from a string port's buffer. */
261SCM scm_strport_to_string (SCM port)
262{
263 scm_port *pt = SCM_PTAB_ENTRY (port);
264
265 if (pt->rw_active == SCM_PORT_WRITE)
266 st_flush (port);
e684c60f 267 return scm_makfromstr (pt->read_buf, pt->read_buf_size, 0);
3fe6190f
GH
268}
269
0f2d19dd 270SCM_PROC(s_call_with_output_string, "call-with-output-string", 1, 0, 0, scm_call_with_output_string);
1cc91f1b 271
0f2d19dd
JB
272SCM
273scm_call_with_output_string (proc)
274 SCM proc;
0f2d19dd
JB
275{
276 SCM p;
754c9491
JB
277
278 p = scm_mkstrport (SCM_INUM0,
279 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
280 SCM_OPN | SCM_WRTNG,
281 s_call_with_output_string);
0f2d19dd 282 scm_apply (proc, p, scm_listofnull);
3fe6190f
GH
283
284 return scm_strport_to_string (p);
0f2d19dd
JB
285}
286
287
288
289/* Return a Scheme string obtained by printing a given object.
290 */
291
1cc91f1b 292
0f2d19dd
JB
293SCM
294scm_strprint_obj (obj)
295 SCM obj;
0f2d19dd
JB
296{
297 SCM str;
298 SCM port;
299
3fe6190f
GH
300 str = scm_makstr (0, 0);
301 port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj");
87818069 302 scm_prin1 (obj, port, 1);
0f2d19dd 303 {
3fe6190f 304 return scm_strport_to_string (port);
0f2d19dd
JB
305 }
306}
307
308
309
310
311SCM_PROC(s_call_with_input_string, "call-with-input-string", 2, 0, 0, scm_call_with_input_string);
1cc91f1b 312
0f2d19dd
JB
313SCM
314scm_call_with_input_string (str, proc)
315 SCM str;
316 SCM proc;
0f2d19dd
JB
317{
318 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, s_call_with_input_string);
319 return scm_apply (proc, p, scm_listofnull);
320}
321
1cc91f1b 322
cd07a097 323
a8aa30d8
MD
324/* Given a null-terminated string EXPR containing a Scheme expression
325 read it, and return it as an SCM value. */
326SCM
327scm_read_0str (expr)
328 char *expr;
329{
3fe6190f 330 SCM port = scm_mkstrport (SCM_INUM0,
a8aa30d8
MD
331 scm_makfrom0str (expr),
332 SCM_OPN | SCM_RDNG,
333 "scm_eval_0str");
334 SCM form;
335
336 /* Read expressions from that port; ignore the values. */
deca31e1 337 form = scm_read (port);
a8aa30d8
MD
338
339 scm_close_port (port);
340 return form;
341}
342
cd07a097 343/* Given a null-terminated string EXPR containing Scheme program text,
a8aa30d8
MD
344 evaluate it, and return the result of the last expression evaluated. */
345SCM
cd07a097
JB
346scm_eval_0str (expr)
347 char *expr;
348{
b377f53e
JB
349 return scm_eval_string (scm_makfrom0str (expr));
350}
351
352
353SCM_PROC (s_eval_string, "eval-string", 1, 0, 0, scm_eval_string);
354
355SCM
356scm_eval_string (string)
357 SCM string;
358{
3fe6190f 359 SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
cd07a097
JB
360 "scm_eval_0str");
361 SCM form;
b377f53e 362 SCM ans = SCM_UNSPECIFIED;
cd07a097
JB
363
364 /* Read expressions from that port; ignore the values. */
0c32d76c 365 while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
a8aa30d8 366 ans = scm_eval_x (form);
cd07a097 367
0d7368d7
JB
368 /* Don't close the port here; if we re-enter this function via a
369 continuation, then the next time we enter it, we'll get an error.
370 It's a string port anyway, so there's no advantage to closing it
371 early. */
372
a8aa30d8 373 return ans;
cd07a097
JB
374}
375
0b6881fa 376void scm_make_stptob (void); /* Called from ports.c */
cd07a097 377
0b6881fa
MD
378void
379scm_make_stptob ()
0f2d19dd 380{
affc96b5 381 long tc = scm_make_port_type ("string", stfill_buffer, st_write);
6c747373 382 scm_set_port_mark (tc, scm_markstream);
affc96b5
GH
383 scm_set_port_end_input (tc, st_end_input);
384 scm_set_port_flush (tc, st_flush);
6c747373 385 scm_set_port_seek (tc, st_seek);
affc96b5 386 scm_set_port_truncate (tc, st_truncate);
0f2d19dd
JB
387}
388
0f2d19dd
JB
389void
390scm_init_strports ()
0f2d19dd
JB
391{
392#include "strports.x"
393}
394