1 /* Copyright (C) 1995,1996,1998,1999 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
66 /* {Ports - string ports}
71 write_buf/write_end point to the ends of the allocated string.
72 read_buf/read_end in principle point to the part of the string which
73 has been written to, but this is only updated after a flush.
74 read_pos and write_pos in principle should be equal, but this is only true
75 when rw_active is SCM_PORT_NEITHER.
79 stfill_buffer (SCM port
)
81 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
83 if (pt
->read_pos
>= pt
->read_end
)
86 return scm_return_first (*pt
->read_pos
, port
);
89 /* change the size of a port's string to new_size. this doesn't
90 change read_buf_size. */
92 st_resize_port (scm_port
*pt
, off_t new_size
)
94 off_t index
= pt
->write_pos
- pt
->write_buf
;
96 pt
->write_buf_size
= new_size
;
98 scm_vector_set_length_x (pt
->stream
, SCM_MAKINUM (new_size
));
100 /* reset buffer in case reallocation moved the string. */
102 pt
->read_buf
= pt
->write_buf
= SCM_CHARS (pt
->stream
);
103 pt
->read_pos
= pt
->write_pos
= pt
->write_buf
+ index
;
104 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
105 pt
->read_end
= pt
->read_buf
+ pt
->read_buf_size
;
109 /* amount by which write_buf is expanded. */
110 #define SCM_WRITE_BLOCK 80
112 /* ensure that write_pos < write_end by enlarging the buffer when
113 necessary. update read_buf to account for written chars. */
117 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
119 if (pt
->write_pos
== pt
->write_end
)
121 st_resize_port (pt
, pt
->write_buf_size
+ SCM_WRITE_BLOCK
);
123 pt
->read_pos
= pt
->write_pos
;
124 if (pt
->read_pos
> pt
->read_end
)
126 pt
->read_end
= (unsigned char *) pt
->read_pos
;
127 pt
->read_buf_size
= pt
->read_end
- pt
->read_buf
;
129 pt
->rw_active
= SCM_PORT_NEITHER
;
133 st_write (SCM port
, void *data
, size_t size
)
135 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
136 const char *input
= (char *) data
;
140 int space
= pt
->write_end
- pt
->write_pos
;
141 int write_len
= (size
> space
) ? space
: size
;
143 strncpy (pt
->write_pos
, input
, write_len
);
144 pt
->write_pos
+= write_len
;
147 if (write_len
== space
)
153 st_end_input (SCM port
, int offset
)
155 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
157 if (pt
->read_pos
- pt
->read_buf
< offset
)
158 scm_misc_error ("st_end_input", "negative position", SCM_EOL
);
160 pt
->write_pos
= (unsigned char *) pt
->read_pos
= pt
->read_pos
- offset
;
161 pt
->rw_active
= SCM_PORT_NEITHER
;
165 st_seek (SCM port
, off_t offset
, int whence
)
167 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
170 if (pt
->rw_active
== SCM_PORT_READ
&& offset
== 0 && whence
== SEEK_CUR
)
171 /* special case to avoid disturbing the unread-char buffer. */
173 if (pt
->read_buf
== pt
->putback_buf
)
175 target
= pt
->saved_read_pos
- pt
->saved_read_buf
176 - (pt
->read_end
- pt
->read_pos
);
180 target
= pt
->read_pos
- pt
->read_buf
;
184 /* all other cases. */
186 if (pt
->rw_active
== SCM_PORT_WRITE
)
189 if (pt
->rw_active
== SCM_PORT_READ
)
190 scm_end_input (port
);
195 target
= pt
->read_pos
- pt
->read_buf
+ offset
;
198 target
= pt
->read_end
- pt
->read_buf
+ offset
;
200 default: /* SEEK_SET */
206 scm_misc_error ("st_seek", "negative offset", SCM_EOL
);
208 if (target
>= pt
->write_buf_size
)
210 if (!(SCM_CAR (port
) & SCM_WRTNG
))
212 if (target
> pt
->write_buf_size
)
214 scm_misc_error ("st_seek",
215 "seek past end of read-only strport",
221 st_resize_port (pt
, target
+ (target
== pt
->write_buf_size
226 pt
->read_pos
= pt
->write_pos
= pt
->read_buf
+ target
;
227 if (pt
->read_pos
> pt
->read_end
)
229 pt
->read_end
= (unsigned char *) pt
->read_pos
;
230 pt
->read_buf_size
= pt
->read_end
- pt
->read_buf
;
237 st_truncate (SCM port
, off_t length
)
239 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
241 if (length
> pt
->write_buf_size
)
242 st_resize_port (pt
, length
);
244 pt
->read_buf_size
= length
;
245 pt
->read_end
= pt
->read_buf
+ length
;
246 if (pt
->read_pos
> pt
->read_end
)
247 pt
->read_pos
= pt
->read_end
;
249 if (pt
->write_pos
> pt
->read_end
)
250 pt
->write_pos
= pt
->read_end
;
254 scm_mkstrport (SCM pos
, SCM str
, long modes
, const char *caller
)
260 SCM_ASSERT (SCM_INUMP(pos
) && SCM_INUM(pos
) >= 0, pos
, SCM_ARG1
, caller
);
261 SCM_ASSERT (SCM_ROSTRINGP(str
), str
, SCM_ARG1
, caller
);
262 str_len
= SCM_ROLENGTH (str
);
263 if (SCM_INUM (pos
) > str_len
)
264 scm_out_of_range (caller
, pos
);
265 if (!((modes
& SCM_WRTNG
) || (modes
& SCM_RDNG
)))
266 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL
);
269 pt
= scm_add_to_port_table (z
);
270 SCM_SETCAR (z
, scm_tc16_strport
| modes
);
271 SCM_SETPTAB_ENTRY (z
, pt
);
272 SCM_SETSTREAM (z
, str
);
273 pt
->write_buf
= pt
->read_buf
= SCM_ROCHARS (str
);
274 pt
->read_pos
= pt
->write_pos
= pt
->read_buf
+ SCM_INUM (pos
);
275 pt
->write_buf_size
= pt
->read_buf_size
= str_len
;
276 pt
->write_end
= pt
->read_end
= pt
->read_buf
+ pt
->read_buf_size
;
282 /* ensure write_pos is writable. */
283 if ((modes
& SCM_WRTNG
) && pt
->write_pos
== pt
->write_end
)
288 /* create a new string from a string port's buffer. */
289 SCM
scm_strport_to_string (SCM port
)
291 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
293 if (pt
->rw_active
== SCM_PORT_WRITE
)
295 return scm_makfromstr (pt
->read_buf
, pt
->read_buf_size
, 0);
298 SCM_DEFINE(scm_call_with_output_string
, "call-with-output-string", 1, 0, 0,
300 "Calls the one-argument procedure @var{proc} with a newly created output
301 port. When the function returns, the string composed of the characters
302 written into the port is returned.")
303 #define FUNC_NAME s_scm_call_with_output_string
307 p
= scm_mkstrport (SCM_INUM0
,
308 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
311 scm_apply (proc
, p
, scm_listofnull
);
313 return scm_strport_to_string (p
);
319 /* Return a Scheme string obtained by printing a given object.
324 scm_strprint_obj (SCM obj
)
329 str
= scm_makstr (0, 0);
330 port
= scm_mkstrport (SCM_INUM0
, str
, SCM_OPN
| SCM_WRTNG
, "scm_strprint_obj");
331 scm_prin1 (obj
, port
, 1);
333 return scm_strport_to_string (port
);
340 SCM_DEFINE(scm_call_with_input_string
, "call-with-input-string", 2, 0, 0,
342 "Calls the one-argument procedure @var{proc} with a newly created input
343 port from which @var{string}'s contents may be read. The value yielded
344 by the @var{proc} is returned.")
345 #define FUNC_NAME s_scm_call_with_input_string
347 SCM p
= scm_mkstrport(SCM_INUM0
, str
, SCM_OPN
| SCM_RDNG
, FUNC_NAME
);
348 return scm_apply (proc
, p
, scm_listofnull
);
354 /* Given a null-terminated string EXPR containing a Scheme expression
355 read it, and return it as an SCM value. */
357 scm_read_0str (char *expr
)
359 SCM port
= scm_mkstrport (SCM_INUM0
,
360 scm_makfrom0str (expr
),
365 /* Read expressions from that port; ignore the values. */
366 form
= scm_read (port
);
368 scm_close_port (port
);
372 /* Given a null-terminated string EXPR containing Scheme program text,
373 evaluate it, and return the result of the last expression evaluated. */
375 scm_eval_0str (const char *expr
)
377 return scm_eval_string (scm_makfrom0str (expr
));
381 SCM_DEFINE (scm_eval_string
, "eval-string", 1, 0, 0,
383 "Evaluate @var{string} as the text representation of a Scheme form
384 or forms, and return whatever value they produce.")
385 #define FUNC_NAME s_scm_eval_string
387 SCM port
= scm_mkstrport (SCM_INUM0
, string
, SCM_OPN
| SCM_RDNG
,
390 SCM ans
= SCM_UNSPECIFIED
;
392 /* Read expressions from that port; ignore the values. */
393 while (!SCM_EOF_OBJECT_P (form
= scm_read (port
)))
394 ans
= scm_eval_x (form
);
396 /* Don't close the port here; if we re-enter this function via a
397 continuation, then the next time we enter it, we'll get an error.
398 It's a string port anyway, so there's no advantage to closing it
405 void scm_make_stptob (void); /* Called from ports.c */
410 long tc
= scm_make_port_type ("string", stfill_buffer
, st_write
);
411 scm_set_port_mark (tc
, scm_markstream
);
412 scm_set_port_end_input (tc
, st_end_input
);
413 scm_set_port_flush (tc
, st_flush
);
414 scm_set_port_seek (tc
, st_seek
);
415 scm_set_port_truncate (tc
, st_truncate
);
421 #include "strports.x"