Mercurial > hg > octave-nkf
changeset 1338:3d235e3c13c0
[project @ 1995-08-25 03:10:55 by jwe]
author | jwe |
---|---|
date | Fri, 25 Aug 1995 03:10:55 +0000 |
parents | 52a3f38cbfeb |
children | e1dbc5585afd |
files | src/file-io.cc |
diffstat | 1 files changed, 61 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/src/file-io.cc +++ b/src/file-io.cc @@ -376,7 +376,7 @@ } static Octave_object -fgets_internal (const Octave_object& args, int nargout) +fgets_internal (const Octave_object& args, int nargin, int nargout) { Octave_object retval; @@ -385,41 +385,67 @@ if (! p) return retval; - - double dlen = args(1).double_value (); - - if (error_state) - return retval; - - if (xisnan (dlen)) - { - error ("fgets: NaN invalid as length"); - return retval; - } - - int length = NINT (dlen); - - if ((double) length != dlen) + file_info file = file_list (p); + + FILE *fileptr = file.fptr (); + + char *string = 0; + char *success = 0; + + if (nargin == 2) { - error ("fgets: length not an integer value"); - return retval; + double dlen = args(1).double_value (); + + if (error_state) + return retval; + + if (xisnan (dlen)) + { + error ("fgets: NaN invalid as length"); + return retval; + } + + int length = NINT (dlen); + + if ((double) length != dlen) + { + error ("fgets: length not an integer value"); + return retval; + } + + char *string = new char[length+1]; + char *success = fgets (string, length+1, fileptr); } - - file_info file = file_list (p); - - char string [length+1]; - char *success = fgets (string, length+1, file.fptr ()); - - if (! success) + else { - retval(0) = -1.0; - return retval; + ostrstream buf; + int c; + while ((c = fgetc (fileptr))) + { + buf << (char) c; + if (c == '\n') + { + buf << ends; + string = buf.str (); + break; + } + } + + if (string && strlen (string) > 0) + success = string; } - - if (nargout == 2) - retval(1) = (double) strlen (string); - - retval(0) = string; + + if (success) + { + if (nargout == 2) + retval(1) = (double) strlen (string); + + retval(0) = string; + } + else + retval(0) = -1.0; + + delete [] string; return retval; } @@ -433,10 +459,10 @@ int nargin = args.length (); - if (nargin != 2) + if (nargin == 1 || nargin == 2) + retval = fgets_internal (args, nargin, nargout); + else print_usage ("fgets"); - else - retval = fgets_internal (args, nargout); return retval; }