* Change a couple of functions to accept either symbols or strings only.
[bpt/guile.git] / libguile / strports.c
index 127dc1d..8717e98 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995,1996,1998,1999 Free Software Foundation, Inc.
+/*     Copyright (C) 1995,1996,1998,1999, 2000 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -57,7 +57,7 @@
 #include "libguile/read.h"
 #include "libguile/root.h"
 #include "libguile/strings.h"
-#include "libguile/vectors.h"
+#include "libguile/modules.h"
 
 #include "libguile/strports.h"
 
@@ -95,17 +95,23 @@ stfill_buffer (SCM port)
 static void 
 st_resize_port (scm_port *pt, off_t new_size)
 {
-  SCM stream = SCM_PACK (pt->stream);
+  SCM old_stream = SCM_PACK (pt->stream);
+  SCM new_stream = scm_makstr (new_size, 0);
+  unsigned long int old_size = SCM_STRING_LENGTH (old_stream);
+  unsigned long int min_size = min (old_size, new_size);
+  unsigned long int i;
 
   off_t index = pt->write_pos - pt->write_buf;
 
   pt->write_buf_size = new_size;
 
-  scm_vector_set_length_x (stream, SCM_MAKINUM (new_size));
+  for (i = 0; i != min_size; ++i)
+    SCM_STRING_CHARS (new_stream) [i] = SCM_STRING_CHARS (old_stream) [i];
 
-  /* reset buffer in case reallocation moved the string. */
+  /* reset buffer. */
   {
-    pt->read_buf = pt->write_buf = SCM_CHARS (stream);
+    pt->stream = new_stream;
+    pt->read_buf = pt->write_buf = SCM_STRING_UCHARS (new_stream);
     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;
@@ -146,7 +152,7 @@ st_write (SCM port, const void *data, size_t size)
       int space = pt->write_end - pt->write_pos;
       int write_len = (size > space) ? space : size;
       
-      strncpy (pt->write_pos, input, write_len);
+      strncpy ((char *) pt->write_pos, input, write_len);
       pt->write_pos += write_len;
       size -= write_len;
       input += write_len;
@@ -264,8 +270,8 @@ scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
   int str_len;
 
   SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
-  SCM_ASSERT (SCM_ROSTRINGP(str), str, SCM_ARG1, caller);
-  str_len = SCM_ROLENGTH (str);
+  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)))
@@ -276,7 +282,7 @@ scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
   SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes);
   SCM_SETPTAB_ENTRY (z, pt);
   SCM_SETSTREAM (z, SCM_UNPACK (str));
-  pt->write_buf = pt->read_buf = SCM_ROCHARS (str);
+  pt->write_buf = pt->read_buf = SCM_ROUCHARS (str);
   pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
   pt->write_buf_size = pt->read_buf_size = str_len;
   pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
@@ -298,7 +304,7 @@ SCM scm_strport_to_string (SCM port)
 
   if (pt->rw_active == SCM_PORT_WRITE)
     st_flush (port);
-  return scm_makfromstr (pt->read_buf, pt->read_buf_size, 0);
+  return scm_makfromstr ((char *) pt->read_buf, pt->read_buf_size, 0);
 }
 
 SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0, 
@@ -387,17 +393,19 @@ scm_eval_0str (const char *expr)
 SCM_DEFINE (scm_eval_string, "eval-string", 1, 0, 0, 
             (SCM string),
            "Evaluate @var{string} as the text representation of a Scheme form\n"
-           "or forms, and return whatever value they produce.")
+           "or forms, and return whatever value they produce.\n"
+           "Evaluation takes place in (interaction-environment).")
 #define FUNC_NAME s_scm_eval_string
 {
   SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
                            "scm_eval_0str");
   SCM form;
   SCM ans = SCM_UNSPECIFIED;
+  SCM module = scm_interaction_environment ();
 
   /* Read expressions from that port; ignore the values.  */
   while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
-    ans = scm_eval_x (form);
+    ans = scm_eval_x (form, module);
 
   /* Don't close the port here; if we re-enter this function via a
      continuation, then the next time we enter it, we'll get an error.