(st_flush): Increase buffer by 1.5x when growing, to
[bpt/guile.git] / libguile / strports.c
CommitLineData
bf5ad0da 1/* Copyright (C) 1995,1996,1998,1999,2000,2001,2002, 2003, 2005 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library 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 GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
c986274f
RB
21#if HAVE_CONFIG_H
22# include <config.h>
23#endif
24
a0599745 25#include "libguile/_scm.h"
681b9005
MD
26
27#include <stdio.h>
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
a0599745
MD
32#include "libguile/unif.h"
33#include "libguile/eval.h"
34#include "libguile/ports.h"
35#include "libguile/read.h"
36#include "libguile/root.h"
37#include "libguile/strings.h"
07bcf91d 38#include "libguile/modules.h"
fe78b6c0 39#include "libguile/validate.h"
ca13a04a 40#include "libguile/deprecation.h"
20e6290e 41
a0599745 42#include "libguile/strports.h"
0f2d19dd 43
95b88819
GH
44#ifdef HAVE_STRING_H
45#include <string.h>
46#endif
47
0f2d19dd
JB
48\f
49
50/* {Ports - string ports}
51 *
52 */
53
3fe6190f 54/* NOTES:
cc95e00a
MV
55
56 We break the rules set forth by strings.h about accessing the
57 internals of strings here. We can do this since we can guarantee
58 that the string used as pt->stream is not in use by anyone else.
59 Thus, it's representation will not change asynchronously.
60
61 (Ports aren't thread-safe yet anyway...)
62
3fe6190f
GH
63 write_buf/write_end point to the ends of the allocated string.
64 read_buf/read_end in principle point to the part of the string which
65 has been written to, but this is only updated after a flush.
66 read_pos and write_pos in principle should be equal, but this is only true
61e452ba 67 when rw_active is SCM_PORT_NEITHER.
bf5ad0da
KR
68
69 ENHANCE-ME - output blocks:
70
71 The current code keeps an output string as a single block. That means
72 when the size is increased the entire old contents must be copied. It'd
73 be more efficient to begin a new block when the old one is full, so
74 there's no re-copying of previous data.
75
76 To make seeking efficient, keeping the pieces in a vector might be best,
77 though appending is probably the most common operation. The size of each
78 block could be progressively increased, so the bigger the string the
79 bigger the blocks.
80
81 When `get-output-string' is called the blocks have to be coalesced into a
82 string, the result could be kept as a single big block. If blocks were
83 strings then `get-output-string' could notice when there's just one and
84 return that with a copy-on-write (though repeated calls to
85 `get-output-string' are probably unlikely).
86
87 Another possibility would be to extend the port mechanism to let SCM
88 strings come through directly from `display' and friends. That way if a
89 big string is written it can be kept as a copy-on-write, saving time
90 copying and maybe saving some space. */
91
1cc91f1b 92
92c2555f 93scm_t_bits scm_tc16_strport;
a98bddfd
DH
94
95
ee149d03
JB
96static int
97stfill_buffer (SCM port)
0f2d19dd 98{
92c2555f 99 scm_t_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 100
ee149d03
JB
101 if (pt->read_pos >= pt->read_end)
102 return EOF;
103 else
41b0806d 104 return scm_return_first_int (*pt->read_pos, port);
0f2d19dd
JB
105}
106
3fe6190f
GH
107/* change the size of a port's string to new_size. this doesn't
108 change read_buf_size. */
754c9491 109static void
92c2555f 110st_resize_port (scm_t_port *pt, off_t new_size)
754c9491 111{
94115ae3 112 SCM old_stream = SCM_PACK (pt->stream);
cc95e00a
MV
113 const char *src = scm_i_string_chars (old_stream);
114 char *dst;
115 SCM new_stream = scm_i_make_string (new_size, &dst);
116 unsigned long int old_size = scm_i_string_length (old_stream);
c014a02e
ML
117 unsigned long int min_size = min (old_size, new_size);
118 unsigned long int i;
74a16888 119
3fe6190f
GH
120 off_t index = pt->write_pos - pt->write_buf;
121
122 pt->write_buf_size = new_size;
123
94115ae3 124 for (i = 0; i != min_size; ++i)
cc95e00a 125 dst[i] = src[i];
8824ac88
MV
126
127 scm_remember_upto_here_1 (old_stream);
754c9491 128
94115ae3 129 /* reset buffer. */
754c9491 130 {
729dbac3 131 pt->stream = SCM_UNPACK (new_stream);
5a6d139b 132 pt->read_buf = pt->write_buf = (unsigned char *)dst;
3fe6190f
GH
133 pt->read_pos = pt->write_pos = pt->write_buf + index;
134 pt->write_end = pt->write_buf + pt->write_buf_size;
135 pt->read_end = pt->read_buf + pt->read_buf_size;
754c9491
JB
136 }
137}
138
3fe6190f
GH
139/* amount by which write_buf is expanded. */
140#define SCM_WRITE_BLOCK 80
141
142/* ensure that write_pos < write_end by enlarging the buffer when
bf5ad0da
KR
143 necessary. update read_buf to account for written chars.
144
145 The buffer is enlarged by 1.5 times, plus SCM_WRITE_BLOCK. Adding just a
146 fixed amount is no good, because there's a block copy for each increment,
147 and that copying would take quadratic time. In the past it was found to
148 be very slow just adding 80 bytes each time (eg. about 10 seconds for
149 writing a 100kbyte string). */
150
ee149d03
JB
151static void
152st_flush (SCM port)
0f2d19dd 153{
92c2555f 154 scm_t_port *pt = SCM_PTAB_ENTRY (port);
ee149d03
JB
155
156 if (pt->write_pos == pt->write_end)
157 {
bf5ad0da 158 st_resize_port (pt, pt->write_buf_size * 3 / 2 + SCM_WRITE_BLOCK);
754c9491
JB
159 }
160 pt->read_pos = pt->write_pos;
3fe6190f
GH
161 if (pt->read_pos > pt->read_end)
162 {
163 pt->read_end = (unsigned char *) pt->read_pos;
164 pt->read_buf_size = pt->read_end - pt->read_buf;
165 }
61e452ba 166 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
167}
168
31703ab8 169static void
8aa011a1 170st_write (SCM port, const void *data, size_t size)
31703ab8 171{
92c2555f 172 scm_t_port *pt = SCM_PTAB_ENTRY (port);
31703ab8
GH
173 const char *input = (char *) data;
174
175 while (size > 0)
176 {
177 int space = pt->write_end - pt->write_pos;
178 int write_len = (size > space) ? space : size;
179
4b8ec619 180 memcpy ((char *) pt->write_pos, input, write_len);
31703ab8
GH
181 pt->write_pos += write_len;
182 size -= write_len;
183 input += write_len;
184 if (write_len == space)
185 st_flush (port);
186 }
187}
188
754c9491 189static void
affc96b5 190st_end_input (SCM port, int offset)
754c9491 191{
92c2555f 192 scm_t_port *pt = SCM_PTAB_ENTRY (port);
7dcb364d 193
cd19d608
GH
194 if (pt->read_pos - pt->read_buf < offset)
195 scm_misc_error ("st_end_input", "negative position", SCM_EOL);
196
a3c8b9fc 197 pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
61e452ba 198 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
199}
200
201static off_t
202st_seek (SCM port, off_t offset, int whence)
203{
92c2555f 204 scm_t_port *pt = SCM_PTAB_ENTRY (port);
754c9491
JB
205 off_t target;
206
7dcb364d
GH
207 if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
208 /* special case to avoid disturbing the unread-char buffer. */
754c9491 209 {
7dcb364d
GH
210 if (pt->read_buf == pt->putback_buf)
211 {
212 target = pt->saved_read_pos - pt->saved_read_buf
213 - (pt->read_end - pt->read_pos);
214 }
215 else
216 {
217 target = pt->read_pos - pt->read_buf;
218 }
754c9491 219 }
7dcb364d
GH
220 else
221 /* all other cases. */
754c9491 222 {
7dcb364d
GH
223 if (pt->rw_active == SCM_PORT_WRITE)
224 st_flush (port);
225
226 if (pt->rw_active == SCM_PORT_READ)
227 scm_end_input (port);
228
229 switch (whence)
230 {
231 case SEEK_CUR:
232 target = pt->read_pos - pt->read_buf + offset;
233 break;
234 case SEEK_END:
235 target = pt->read_end - pt->read_buf + offset;
236 break;
237 default: /* SEEK_SET */
238 target = offset;
239 break;
240 }
241
242 if (target < 0)
243 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
244
245 if (target >= pt->write_buf_size)
3fe6190f 246 {
206d3de3 247 if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
3fe6190f 248 {
7dcb364d
GH
249 if (target > pt->write_buf_size)
250 {
251 scm_misc_error ("st_seek",
252 "seek past end of read-only strport",
253 SCM_EOL);
254 }
255 }
256 else
257 {
258 st_resize_port (pt, target + (target == pt->write_buf_size
259 ? SCM_WRITE_BLOCK
260 : 0));
3fe6190f
GH
261 }
262 }
7dcb364d
GH
263 pt->read_pos = pt->write_pos = pt->read_buf + target;
264 if (pt->read_pos > pt->read_end)
3fe6190f 265 {
7dcb364d
GH
266 pt->read_end = (unsigned char *) pt->read_pos;
267 pt->read_buf_size = pt->read_end - pt->read_buf;
3fe6190f 268 }
ee149d03 269 }
754c9491
JB
270 return target;
271}
272
273static void
affc96b5 274st_truncate (SCM port, off_t length)
754c9491 275{
92c2555f 276 scm_t_port *pt = SCM_PTAB_ENTRY (port);
3fe6190f
GH
277
278 if (length > pt->write_buf_size)
279 st_resize_port (pt, length);
280
281 pt->read_buf_size = length;
282 pt->read_end = pt->read_buf + length;
283 if (pt->read_pos > pt->read_end)
284 pt->read_pos = pt->read_end;
754c9491 285
3fe6190f
GH
286 if (pt->write_pos > pt->read_end)
287 pt->write_pos = pt->read_end;
0f2d19dd
JB
288}
289
0f2d19dd 290SCM
1bbd0b84 291scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
0f2d19dd
JB
292{
293 SCM z;
92c2555f 294 scm_t_port *pt;
e11e83f3 295 size_t str_len, c_pos;
754c9491 296
cc95e00a
MV
297 SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller);
298
299 str_len = scm_i_string_length (str);
e11e83f3
MV
300 c_pos = scm_to_unsigned_integer (pos, 0, str_len);
301
754c9491
JB
302 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
303 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
5f16b897 304
cc95e00a
MV
305 /* XXX
306
307 Make a new string to isolate us from changes to the original.
308 This is done so that we can rely on scm_i_string_chars to stay in
309 place even across SCM_TICKs.
310
311 Additionally, when we are going to write to the string, we make a
312 copy so that we can write to it without having to use
313 scm_i_string_writable_chars.
314 */
315
316 if (modes & SCM_WRTNG)
317 str = scm_c_substring_copy (str, 0, str_len);
318 else
319 str = scm_c_substring (str, 0, str_len);
320
9de87eea 321 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
da220f27
HWN
322 z = scm_new_port_table_entry (scm_tc16_strport);
323 pt = SCM_PTAB_ENTRY(z);
74a16888 324 SCM_SETSTREAM (z, SCM_UNPACK (str));
da220f27 325 SCM_SET_CELL_TYPE(z, scm_tc16_strport|modes);
cc95e00a 326 /* see above why we can use scm_i_string_chars here. */
5a6d139b 327 pt->write_buf = pt->read_buf = (unsigned char *) scm_i_string_chars (str);
e11e83f3 328 pt->read_pos = pt->write_pos = pt->read_buf + c_pos;
754c9491
JB
329 pt->write_buf_size = pt->read_buf_size = str_len;
330 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
3fe6190f 331
0de97b83 332 pt->rw_random = 1;
3fe6190f 333
9de87eea 334 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
3fe6190f
GH
335
336 /* ensure write_pos is writable. */
754c9491
JB
337 if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
338 st_flush (z);
0f2d19dd
JB
339 return z;
340}
341
3fe6190f
GH
342/* create a new string from a string port's buffer. */
343SCM scm_strport_to_string (SCM port)
344{
92c2555f 345 scm_t_port *pt = SCM_PTAB_ENTRY (port);
36284627 346 SCM str;
cc95e00a
MV
347 char *dst;
348
3fe6190f
GH
349 if (pt->rw_active == SCM_PORT_WRITE)
350 st_flush (port);
36284627 351
cc95e00a
MV
352 str = scm_i_make_string (pt->read_buf_size, &dst);
353 memcpy (dst, (char *) pt->read_buf, pt->read_buf_size);
36284627
DH
354 scm_remember_upto_here_1 (port);
355 return str;
3fe6190f
GH
356}
357
fe78b6c0
KN
358SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
359 (SCM obj, SCM printer),
360 "Return a Scheme string obtained by printing @var{obj}.\n"
361 "Printing function can be specified by the optional second\n"
362 "argument @var{printer} (default: @code{write}).")
1f3908c4
KN
363#define FUNC_NAME s_scm_object_to_string
364{
fe78b6c0
KN
365 SCM str, port;
366
367 if (!SCM_UNBNDP (printer))
368 SCM_VALIDATE_PROC (2, printer);
1f3908c4 369
cc95e00a 370 str = scm_c_make_string (0, SCM_UNDEFINED);
1a92274c 371 port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, FUNC_NAME);
fe78b6c0
KN
372
373 if (SCM_UNBNDP (printer))
374 scm_write (obj, port);
375 else
fdc28395 376 scm_call_2 (printer, obj, port);
fe78b6c0 377
1f3908c4
KN
378 return scm_strport_to_string (port);
379}
380#undef FUNC_NAME
381
3b3b36dd 382SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0,
1bbd0b84 383 (SCM proc),
b380b885
MD
384 "Calls the one-argument procedure @var{proc} with a newly created output\n"
385 "port. When the function returns, the string composed of the characters\n"
386 "written into the port is returned.")
1bbd0b84 387#define FUNC_NAME s_scm_call_with_output_string
0f2d19dd
JB
388{
389 SCM p;
754c9491
JB
390
391 p = scm_mkstrport (SCM_INUM0,
392 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
393 SCM_OPN | SCM_WRTNG,
1bbd0b84 394 FUNC_NAME);
fdc28395 395 scm_call_1 (proc, p);
3fe6190f 396
184b85a3 397 return scm_get_output_string (p);
0f2d19dd 398}
1bbd0b84 399#undef FUNC_NAME
0f2d19dd 400
3b3b36dd 401SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0,
1e6808ea
MG
402 (SCM string, SCM proc),
403 "Calls the one-argument procedure @var{proc} with a newly\n"
404 "created input port from which @var{string}'s contents may be\n"
405 "read. The value yielded by the @var{proc} is returned.")
1bbd0b84 406#define FUNC_NAME s_scm_call_with_input_string
0f2d19dd 407{
1e6808ea 408 SCM p = scm_mkstrport(SCM_INUM0, string, SCM_OPN | SCM_RDNG, FUNC_NAME);
fdc28395 409 return scm_call_1 (proc, p);
0f2d19dd 410}
1bbd0b84 411#undef FUNC_NAME
0f2d19dd 412
e87a03fc
MG
413SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0,
414 (SCM str),
1e6808ea
MG
415 "Take a string and return an input port that delivers characters\n"
416 "from the string. The port can be closed by\n"
e87a03fc
MG
417 "@code{close-input-port}, though its storage will be reclaimed\n"
418 "by the garbage collector if it becomes inaccessible.")
419#define FUNC_NAME s_scm_open_input_string
420{
421 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
422 return p;
423}
424#undef FUNC_NAME
425
426SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0,
427 (void),
1e6808ea 428 "Return an output port that will accumulate characters for\n"
e87a03fc
MG
429 "retrieval by @code{get-output-string}. The port can be closed\n"
430 "by the procedure @code{close-output-port}, though its storage\n"
431 "will be reclaimed by the garbage collector if it becomes\n"
432 "inaccessible.")
433#define FUNC_NAME s_scm_open_output_string
434{
435 SCM p;
436
437 p = scm_mkstrport (SCM_INUM0,
438 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
439 SCM_OPN | SCM_WRTNG,
440 FUNC_NAME);
441 return p;
442}
443#undef FUNC_NAME
444
445SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0,
446 (SCM port),
447 "Given an output port created by @code{open-output-string},\n"
1e6808ea 448 "return a string consisting of the characters that have been\n"
e87a03fc
MG
449 "output to the port so far.")
450#define FUNC_NAME s_scm_get_output_string
451{
452 SCM_VALIDATE_OPOUTSTRPORT (1, port);
453 return scm_strport_to_string (port);
454}
455#undef FUNC_NAME
1cc91f1b 456
cd07a097 457
a8aa30d8
MD
458/* Given a null-terminated string EXPR containing a Scheme expression
459 read it, and return it as an SCM value. */
460SCM
ca13a04a 461scm_c_read_string (const char *expr)
a8aa30d8 462{
3fe6190f 463 SCM port = scm_mkstrport (SCM_INUM0,
cc95e00a 464 scm_from_locale_string (expr),
a8aa30d8 465 SCM_OPN | SCM_RDNG,
ca13a04a 466 "scm_c_read_string");
a8aa30d8
MD
467 SCM form;
468
469 /* Read expressions from that port; ignore the values. */
deca31e1 470 form = scm_read (port);
a8aa30d8
MD
471
472 scm_close_port (port);
473 return form;
474}
475
cd07a097 476/* Given a null-terminated string EXPR containing Scheme program text,
a8aa30d8
MD
477 evaluate it, and return the result of the last expression evaluated. */
478SCM
ca13a04a 479scm_c_eval_string (const char *expr)
cd07a097 480{
cc95e00a 481 return scm_eval_string (scm_from_locale_string (expr));
b377f53e
JB
482}
483
209b52fe
MV
484SCM
485scm_c_eval_string_in_module (const char *expr, SCM module)
486{
cc95e00a 487 return scm_eval_string_in_module (scm_from_locale_string (expr), module);
209b52fe
MV
488}
489
490
96e83482
MV
491static SCM
492inner_eval_string (void *data)
b377f53e 493{
96e83482 494 SCM port = (SCM)data;
cd07a097 495 SCM form;
b377f53e 496 SCM ans = SCM_UNSPECIFIED;
cd07a097
JB
497
498 /* Read expressions from that port; ignore the values. */
0c32d76c 499 while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
96e83482 500 ans = scm_primitive_eval_x (form);
cd07a097 501
0d7368d7
JB
502 /* Don't close the port here; if we re-enter this function via a
503 continuation, then the next time we enter it, we'll get an error.
504 It's a string port anyway, so there's no advantage to closing it
505 early. */
506
a8aa30d8 507 return ans;
cd07a097 508}
96e83482 509
209b52fe
MV
510SCM_DEFINE (scm_eval_string_in_module, "eval-string", 1, 1, 0,
511 (SCM string, SCM module),
96e83482
MV
512 "Evaluate @var{string} as the text representation of a Scheme\n"
513 "form or forms, and return whatever value they produce.\n"
209b52fe
MV
514 "Evaluation takes place in the given module, or the current\n"
515 "module when no module is given.\n"
516 "While the code is evaluated, the given module is made the\n"
517 "current one. The current module is restored when this\n"
518 "procedure returns.")
519#define FUNC_NAME s_scm_eval_string_in_module
96e83482
MV
520{
521 SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
209b52fe
MV
522 FUNC_NAME);
523 if (SCM_UNBNDP (module))
524 module = scm_current_module ();
aeec5be1
MV
525 else
526 SCM_VALIDATE_MODULE (2, module);
209b52fe 527 return scm_c_call_with_current_module (module,
96e83482
MV
528 inner_eval_string, (void *)port);
529}
1bbd0b84 530#undef FUNC_NAME
cd07a097 531
209b52fe
MV
532SCM
533scm_eval_string (SCM string)
534{
535 return scm_eval_string_in_module (string, SCM_UNDEFINED);
536}
537
92c2555f 538static scm_t_bits
0b6881fa 539scm_make_stptob ()
0f2d19dd 540{
92c2555f 541 scm_t_bits tc = scm_make_port_type ("string", stfill_buffer, st_write);
a98bddfd 542
6c747373 543 scm_set_port_mark (tc, scm_markstream);
affc96b5
GH
544 scm_set_port_end_input (tc, st_end_input);
545 scm_set_port_flush (tc, st_flush);
6c747373 546 scm_set_port_seek (tc, st_seek);
affc96b5 547 scm_set_port_truncate (tc, st_truncate);
a98bddfd
DH
548
549 return tc;
0f2d19dd
JB
550}
551
0f2d19dd
JB
552void
553scm_init_strports ()
0f2d19dd 554{
a98bddfd
DH
555 scm_tc16_strport = scm_make_stptob ();
556
a0599745 557#include "libguile/strports.x"
0f2d19dd
JB
558}
559
89e00824
ML
560
561/*
562 Local Variables:
563 c-file-style: "gnu"
564 End:
565*/