changeset 1339:e1dbc5585afd

[project @ 1995-08-25 03:58:02 by jwe]
author jwe
date Fri, 25 Aug 1995 03:58:02 +0000
parents 3d235e3c13c0
children 02145fb69a5b
files src/file-io.cc
diffstat 1 files changed, 55 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/file-io.cc	Fri Aug 25 03:10:55 1995 +0000
+++ b/src/file-io.cc	Fri Aug 25 03:58:02 1995 +0000
@@ -376,7 +376,8 @@
 }
 
 static Octave_object
-fgets_internal (const Octave_object& args, int nargin, int nargout)
+fgets_internal (const Octave_object& args, int nargin, int nargout,
+		int strip_final_newline = 0)
 {
   Octave_object retval;
 
@@ -385,12 +386,7 @@
   if (! p)
     return retval;
 
-  file_info file = file_list (p);
-
-  FILE *fileptr = file.fptr ();
-
-  char *string = 0;
-  char *success = 0;
+  int length = 0;
 
   if (nargin == 2)
     {
@@ -405,7 +401,7 @@
 	  return retval;
 	}
 
-      int length = NINT (dlen);
+      length = NINT (dlen);
 
       if ((double) length != dlen)
 	{
@@ -413,32 +409,50 @@
 	  return retval;
 	}
 
-      char *string = new char[length+1];
-      char *success = fgets (string, length+1, fileptr);
+      if (length < 0)
+	{
+	  error ("fgets: length must be a nonnegative integer");
+	  return retval;
+	}
     }
-  else
+
+  file_info file = file_list (p);
+  FILE *fileptr = file.fptr ();
+
+  ostrstream buf;
+  int c;
+  int count = 0;
+  int newline_stripped = 0;
+
+  if (nargin == 1 || length > 0)
     {
-      ostrstream buf;
-      int c;
-      while ((c = fgetc (fileptr)))
+      while ((c = fgetc (fileptr)) != EOF)
 	{
-	  buf << (char) c;
+	  count++;
 	  if (c == '\n')
 	    {
-	      buf << ends;
-	      string = buf.str ();
+	      if (! strip_final_newline)
+		buf << (char) c;
+	      else
+		newline_stripped = 1;
+
 	      break;
 	    }
+	  else
+	    buf << (char) c;
+
+	  if (nargin == 2 && count == length)
+	    break;
 	}
-
-      if (string && strlen (string) > 0)
-	success = string;
     }
-  
-  if (success)
+
+  buf << ends;
+  char *string = buf.str ();
+
+  if (count)
     {
       if (nargout == 2)
-	retval(1) = (double) strlen (string);
+	retval(1) = (double) (count - newline_stripped);
 
       retval(0) = string;
     }
@@ -450,8 +464,25 @@
   return retval;
 }
 
+DEFUN ("fgetl", Ffgetl, Sfgetl, 2, 2,
+  "[STRING, LENGTH] = fgetl (FILENAME or FILENUM [, LENGTH])\n\
+\n\
+read a string from a file")
+{
+  Octave_object retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 1 || nargin == 2)
+    retval = fgets_internal (args, nargin, nargout, 1);
+  else
+    print_usage ("fgetl");
+
+  return retval;
+}
+
 DEFUN ("fgets", Ffgets, Sfgets, 2, 2,
-  "[STRING, LENGTH] = fgets (FILENAME or FILENUM, LENGTH)\n\
+  "[STRING, LENGTH] = fgets (FILENAME or FILENUM [, LENGTH])\n\
 \n\
 read a string from a file")
 {