*/
/* NOTES:
+
+ We break the rules set forth by strings.h about accessing the
+ internals of strings here. We can do this since we can guarantee
+ that the string used as pt->stream is not in use by anyone else.
+ Thus, it's representation will not change asynchronously.
+
+ (Ports aren't thread-safe yet anyway...)
+
write_buf/write_end point to the ends of the allocated string.
read_buf/read_end in principle point to the part of the string which
has been written to, but this is only updated after a flush.
st_resize_port (scm_t_port *pt, off_t new_size)
{
SCM old_stream = SCM_PACK (pt->stream);
- SCM new_stream = scm_allocate_string (new_size);
- unsigned long int old_size = SCM_STRING_LENGTH (old_stream);
+ const char *src = scm_i_string_chars (old_stream);
+ char *dst;
+ SCM new_stream = scm_i_make_string (new_size, &dst);
+ unsigned long int old_size = scm_i_string_length (old_stream);
unsigned long int min_size = min (old_size, new_size);
unsigned long int i;
pt->write_buf_size = new_size;
for (i = 0; i != min_size; ++i)
- SCM_STRING_CHARS (new_stream) [i] = SCM_STRING_CHARS (old_stream) [i];
+ dst[i] = src[i];
+
+ scm_remember_upto_here_1 (old_stream);
/* reset buffer. */
{
pt->stream = SCM_UNPACK (new_stream);
- pt->read_buf = pt->write_buf = SCM_STRING_UCHARS (new_stream);
+ pt->read_buf = pt->write_buf = dst;
pt->read_pos = pt->write_pos = pt->write_buf + index;
pt->write_end = pt->write_buf + pt->write_buf_size;
pt->read_end = pt->read_buf + pt->read_buf_size;
{
SCM z;
scm_t_port *pt;
- size_t str_len;
+ size_t str_len, c_pos;
+
+ SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller);
+
+ str_len = scm_i_string_length (str);
+ c_pos = scm_to_unsigned_integer (pos, 0, str_len);
- SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
- SCM_ASSERT (SCM_STRINGP (str), str, SCM_ARG1, caller);
- str_len = SCM_STRING_LENGTH (str);
- if (SCM_INUM (pos) > str_len)
- scm_out_of_range (caller, pos);
if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
+ /* XXX
+
+ Make a new string to isolate us from changes to the original.
+ This is done so that we can rely on scm_i_string_chars to stay in
+ place even across SCM_TICKs.
+
+ Additionally, when we are going to write to the string, we make a
+ copy so that we can write to it without having to use
+ scm_i_string_writable_chars.
+ */
+
+ if (modes & SCM_WRTNG)
+ str = scm_c_substring_copy (str, 0, str_len);
+ else
+ str = scm_c_substring (str, 0, str_len);
+
scm_mutex_lock (&scm_i_port_table_mutex);
z = scm_new_port_table_entry (scm_tc16_strport);
pt = SCM_PTAB_ENTRY(z);
SCM_SETSTREAM (z, SCM_UNPACK (str));
SCM_SET_CELL_TYPE(z, scm_tc16_strport|modes);
- pt->write_buf = pt->read_buf = SCM_STRING_UCHARS (str);
- pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
+ /* see above why we can use scm_i_string_chars here. */
+ pt->write_buf = pt->read_buf = (char *)scm_i_string_chars (str);
+ pt->read_pos = pt->write_pos = pt->read_buf + c_pos;
pt->write_buf_size = pt->read_buf_size = str_len;
pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
{
scm_t_port *pt = SCM_PTAB_ENTRY (port);
SCM str;
-
+ char *dst;
+
if (pt->rw_active == SCM_PORT_WRITE)
st_flush (port);
- str = scm_mem2string ((char *) pt->read_buf, pt->read_buf_size);
+ str = scm_i_make_string (pt->read_buf_size, &dst);
+ memcpy (dst, (char *) pt->read_buf, pt->read_buf_size);
scm_remember_upto_here_1 (port);
return str;
}
if (!SCM_UNBNDP (printer))
SCM_VALIDATE_PROC (2, printer);
- str = scm_allocate_string (0);
+ str = scm_c_make_string (0, SCM_UNDEFINED);
port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, FUNC_NAME);
if (SCM_UNBNDP (printer))
FUNC_NAME);
scm_call_1 (proc, p);
- return scm_strport_to_string (p);
+ return scm_get_output_string (p);
}
#undef FUNC_NAME
scm_c_read_string (const char *expr)
{
SCM port = scm_mkstrport (SCM_INUM0,
- scm_makfrom0str (expr),
+ scm_from_locale_string (expr),
SCM_OPN | SCM_RDNG,
"scm_c_read_string");
SCM form;
SCM
scm_c_eval_string (const char *expr)
{
- return scm_eval_string (scm_makfrom0str (expr));
+ return scm_eval_string (scm_from_locale_string (expr));
}
SCM
scm_c_eval_string_in_module (const char *expr, SCM module)
{
- return scm_eval_string_in_module (scm_makfrom0str (expr), module);
+ return scm_eval_string_in_module (scm_from_locale_string (expr), module);
}