changeset 9952:7cd2e1b372e5

allow scanf to store ASCII NUL values
author John W. Eaton <jwe@octave.org>
date Thu, 10 Dec 2009 01:03:34 -0500
parents d64d15e12e6b
children 225bfa546ae7
files src/ChangeLog src/oct-stream.cc
diffstat 2 files changed, 14 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2009-12-10  John W. Eaton  <jwe@octave.org>
+
+	* oct-stream.cc (BEGIN_C_CONVERSION, BEGIN_S_CONVERSION):
+	Store characters directly in appropriately sized std::string object.
+	(FINISH_CHARACTER_CONVERSION): Do store ASCII NUL values.
+
 2009-12-09  John W. Eaton  <jwe@octave.org>
 
 	* DLD-FUNCTIONS/fltk_backend.cc: Style fixes.
--- a/src/oct-stream.cc
+++ b/src/oct-stream.cc
@@ -1555,22 +1555,16 @@
  \
   int width = elt->width ? elt->width : 1; \
  \
-  char *tbuf = new char[width + 1]; \
+  std::string tmp (width, '\0'); \
  \
   int c = EOF; \
   int n = 0; \
  \
   while (is && n < width && (c = is.get ()) != EOF) \
-    tbuf[n++] = static_cast<char> (c);		    \
- \
-  tbuf[n] = '\0'; \
+    tmp[n++] = static_cast<char> (c); \
  \
   if (n > 0 && c == EOF) \
-    is.clear (); \
- \
-  std::string tmp = tbuf; \
- \
-  delete [] tbuf
+    is.clear ()
 
 // For a `%s' format, skip initial whitespace and then read until the
 // next whitespace character or until WIDTH characters have been read.
@@ -1583,7 +1577,7 @@
     { \
       if (width) \
         { \
-          char *tbuf = new char [width+1]; \
+          std::string tmp (width, '\0'); \
  \
           int c = EOF; \
  \
@@ -1593,7 +1587,7 @@
             { \
               if (! isspace (c)) \
                 { \
-                  tbuf[n++] = static_cast<char> (c); \
+                  tmp[n++] = static_cast<char> (c); \
                   break; \
                 } \
             } \
@@ -1606,17 +1600,13 @@
                   break; \
                 } \
               else \
-                tbuf[n++] = static_cast<char> (c); \
+                tmp[n++] = static_cast<char> (c); \
             } \
  \
-          tbuf[n] = '\0'; \
- \
           if (n > 0 && c == EOF) \
             is.clear (); \
  \
-          tmp = tbuf; \
- \
-          delete [] tbuf; \
+          tmp.resize (n); \
         } \
       else \
         { \
@@ -1680,7 +1670,7 @@
 	    { \
 	      conversion_count++; \
  \
-	      while (i < width && tmp[i] != '\0') \
+	      while (i < width) \
 		{ \
 		  if (data_index == max_size) \
 		    { \