# HG changeset patch # User jwe # Date 941789519 0 # Node ID 14cfc9475fe4945af8366e26fc906587545da5c3 # Parent 585a8809fd9be9e5786957d09d357e32ee10c000 [project @ 1999-11-05 08:11:58 by jwe] diff -r 585a8809fd9b -r 14cfc9475fe4 src/file-io.cc --- a/src/file-io.cc Fri Nov 05 07:02:36 1999 +0000 +++ b/src/file-io.cc Fri Nov 05 08:11:59 1999 +0000 @@ -94,12 +94,6 @@ octave_stream_list::clear (); } -static void -gripe_invalid_file_id (const char *fcn) -{ - ::error ("%s: invalid file id", fcn); -} - static int fopen_mode_to_ios_mode (const string& mode) { @@ -161,12 +155,8 @@ int nargin = args.length (); if (nargin == 1) - { - retval = static_cast (octave_stream_list::remove (args(0))); - - if (retval < 0) - gripe_invalid_file_id ("fclose"); - } + retval = static_cast (octave_stream_list::remove (args(0), + "fclose")); else print_usage ("fclose"); @@ -194,12 +184,10 @@ } else { - octave_stream os = octave_stream_list::lookup (fid); + octave_stream os = octave_stream_list::lookup (fid, "fflush"); - if (os.is_valid ()) + if (! error_state) retval = static_cast (os.flush ()); - else - gripe_invalid_file_id ("fflush"); } } else @@ -222,9 +210,9 @@ if (nargin == 1 || nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fgetl"); - if (os.is_valid ()) + if (! error_state) { octave_value len_arg = (nargin == 2) ? args(1) : octave_value (static_cast (INT_MAX)); @@ -239,8 +227,6 @@ retval(0) = tmp; } } - else - gripe_invalid_file_id ("fgetl"); } else print_usage ("fgetl"); @@ -262,9 +248,9 @@ if (nargin == 1 || nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fgets"); - if (os.is_valid ()) + if (! error_state) { octave_value len_arg = (nargin == 2) ? args(1) : octave_value (static_cast (INT_MAX)); @@ -279,8 +265,6 @@ retval(0) = tmp; } } - else - gripe_invalid_file_id ("fgets"); } else print_usage ("fgets"); @@ -472,12 +456,10 @@ if (nargin == 1) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "frewind"); - if (os.is_valid ()) + if (! error_state) retval = static_cast (os.rewind ()); - else - gripe_invalid_file_id ("frewind"); } else print_usage ("frewind"); @@ -502,17 +484,15 @@ if (nargin == 2 || nargin == 3) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fseek"); - if (os.is_valid ()) + if (! error_state) { octave_value origin_arg = (nargin == 3) ? args(2) : octave_value (-1.0); retval = static_cast (os.seek (args(1), origin_arg)); } - else - ::error ("fseek: invalid file id"); } else print_usage ("fseek"); @@ -529,12 +509,10 @@ if (nargin == 1) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "ftell"); - if (os.is_valid ()) + if (! error_state) retval = static_cast (os.tell ()); - else - gripe_invalid_file_id ("ftell"); } else print_usage ("ftell"); @@ -555,14 +533,14 @@ int fmt_n = 0; if (args(0).is_string ()) - os = octave_stream_list::lookup (1); + os = octave_stream_list::lookup (1, "fprintf"); else { fmt_n = 1; - os = octave_stream_list::lookup (args(0)); + os = octave_stream_list::lookup (args(0), "fprintf"); } - if (os.is_valid ()) + if (! error_state) { if (args(fmt_n).is_string ()) { @@ -583,8 +561,6 @@ else ::error ("fprintf: format must be a string"); } - else - gripe_invalid_file_id ("fprintf"); } else print_usage ("fprintf"); @@ -601,12 +577,10 @@ if (nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fputs"); - if (os.is_valid ()) + if (! error_state) retval = os.puts (args(1)); - else - gripe_invalid_file_id ("fputs"); } else print_usage ("fputs"); @@ -692,9 +666,9 @@ if (nargin == 3 && args(2).is_string ()) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); - if (os.is_valid ()) + if (! error_state) { if (args(1).is_string ()) { @@ -705,8 +679,6 @@ else ::error ("fscanf: format must be a string"); } - else - gripe_invalid_file_id ("fscanf"); } else { @@ -715,9 +687,9 @@ if (nargin == 2 || nargin == 3) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); - if (os.is_valid ()) + if (error_state) { if (args(1).is_string ()) { @@ -739,8 +711,6 @@ else ::error ("fscanf: format must be a string"); } - else - gripe_invalid_file_id ("fscanf"); } else print_usage ("fscanf"); @@ -983,9 +953,9 @@ retval(1) = -1.0; retval(0) = Matrix (); - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fread"); - if (os.is_valid ()) + if (! error_state) { octave_value size = (nargin > 1) ? args(1) : octave_value (octave_Inf); @@ -1006,8 +976,6 @@ retval(1) = static_cast (count); retval(0) = tmp; } - else - gripe_invalid_file_id ("fread"); } else print_usage ("fread"); @@ -1111,9 +1079,9 @@ if (nargin > 1 && nargin < 6) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "fwrite"); - if (os.is_valid ()) + if (! error_state) { octave_value data = args(1); @@ -1130,8 +1098,6 @@ retval = status; } - else - gripe_invalid_file_id ("fwrite"); } else print_usage ("fwrite"); @@ -1151,12 +1117,10 @@ if (nargin == 1) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "feof"); - if (os.is_valid ()) + if (! error_state) retval = os.eof () ? 1.0 : 0.0; - else - gripe_invalid_file_id ("feof"); } else print_usage ("feof"); @@ -1176,9 +1140,9 @@ if (nargin == 1 || nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0)); + octave_stream os = octave_stream_list::lookup (args(0), "ferror"); - if (os.is_valid ()) + if (! error_state) { bool clear = false; @@ -1199,8 +1163,6 @@ retval(1) = static_cast (error_number); retval(0) = error_message; } - else - gripe_invalid_file_id ("ferror"); } else print_usage ("ferror"); @@ -1294,12 +1256,8 @@ int nargin = args.length (); if (nargin == 1) - { - retval = static_cast (octave_stream_list::remove (args(0))); - - if (retval < 0) - gripe_invalid_file_id ("pclose"); - } + retval = static_cast (octave_stream_list::remove (args(0), + "pclose")); else print_usage ("pclose"); diff -r 585a8809fd9b -r 14cfc9475fe4 src/oct-stream.cc --- a/src/oct-stream.cc Fri Nov 05 07:02:36 1999 +0000 +++ b/src/oct-stream.cc Fri Nov 05 08:11:59 1999 +0000 @@ -2638,27 +2638,27 @@ } octave_stream -octave_stream_list::lookup (int fid) +octave_stream_list::lookup (int fid, const string& who) { - return (instance_ok ()) ? instance->do_lookup (fid) : octave_stream (); + return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); } octave_stream -octave_stream_list::lookup (const octave_value& fid) +octave_stream_list::lookup (const octave_value& fid, const string& who) { - return (instance_ok ()) ? instance->do_lookup (fid) : octave_stream (); + return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); } int -octave_stream_list::remove (int fid) +octave_stream_list::remove (int fid, const string& who) { - return (instance_ok ()) ? instance->do_remove (fid) : -1; + return (instance_ok ()) ? instance->do_remove (fid, who) : -1; } int -octave_stream_list::remove (const octave_value& fid) +octave_stream_list::remove (const octave_value& fid, const string& who) { - return (instance_ok ()) ? instance->do_remove (fid) : -1; + return (instance_ok ()) ? instance->do_remove (fid, who) : -1; } void @@ -2736,32 +2736,44 @@ return octave_value (os, stream_number); } +static void +gripe_invalid_file_id (int fid, const string& who) +{ + if (who.empty ()) + ::error ("invalid stream number = %d", fid); + else + ::error ("%s: invalid stream number = %d", who.c_str (), fid); +} + octave_stream -octave_stream_list::do_lookup (int fid) const +octave_stream_list::do_lookup (int fid, const string& who) const { octave_stream retval; if (fid >= 0 && fid < curr_len) retval = list(fid); + else + gripe_invalid_file_id (fid, who); return retval; } octave_stream -octave_stream_list::do_lookup (const octave_value& fid) const +octave_stream_list::do_lookup (const octave_value& fid, + const string& who) const { octave_stream retval; int i = get_file_number (fid); if (! error_state) - retval = do_lookup (i); + retval = do_lookup (i, who); return retval; } int -octave_stream_list::do_remove (int fid) +octave_stream_list::do_remove (int fid, const string& who) { int retval = -1; @@ -2771,26 +2783,30 @@ { octave_stream os = list(fid); - if (os) + if (os.is_valid ()) { os.close (); list(fid) = octave_stream (); retval = 0; } + else + gripe_invalid_file_id (fid, who); } + else + gripe_invalid_file_id (fid, who); return retval; } int -octave_stream_list::do_remove (const octave_value& fid) +octave_stream_list::do_remove (const octave_value& fid, const string& who) { int retval = -1; int i = get_file_number (fid); if (! error_state) - retval = do_remove (i); + retval = do_remove (i, who); return retval; } @@ -2816,7 +2832,7 @@ octave_stream os = do_lookup (fid); - if (os) + if (os.is_valid ()) { retval.resize (3); @@ -2825,7 +2841,7 @@ retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); } else - ::error ("invalid file id"); + ::error ("invalid file id = %d", fid); return retval; } diff -r 585a8809fd9b -r 14cfc9475fe4 src/oct-stream.h --- a/src/oct-stream.h Fri Nov 05 07:02:36 1999 +0000 +++ b/src/oct-stream.h Fri Nov 05 08:11:59 1999 +0000 @@ -495,11 +495,13 @@ static octave_value insert (const octave_stream& os); - static octave_stream lookup (int fid); - static octave_stream lookup (const octave_value& fid); + static octave_stream lookup (int fid, const string& who = string ()); + static octave_stream lookup (const octave_value& fid, + const string& who = string ()); - static int remove (int fid); - static int remove (const octave_value& fid); + static int remove (int fid, const string& who = string ()); + static int remove (const octave_value& fid, + const string& who = string ()); static void clear (void); @@ -522,11 +524,12 @@ octave_value do_insert (const octave_stream& os); - octave_stream do_lookup (int fid) const; - octave_stream do_lookup (const octave_value& fid) const; + octave_stream do_lookup (int fid, const string& who = string ()) const; + octave_stream do_lookup (const octave_value& fid, + const string& who = string ()) const; - int do_remove (int fid); - int do_remove (const octave_value& fid); + int do_remove (int fid, const string& who = string ()); + int do_remove (const octave_value& fid, const string& who = string ()); void do_clear (void); diff -r 585a8809fd9b -r 14cfc9475fe4 src/syscalls.cc --- a/src/syscalls.cc Fri Nov 05 07:02:36 1999 +0000 +++ b/src/syscalls.cc Fri Nov 05 08:11:59 1999 +0000 @@ -108,25 +108,29 @@ if (nargin == 2) { - octave_stream old_stream = octave_stream_list::lookup (args(0)); - octave_stream new_stream = octave_stream_list::lookup (args(1)); + octave_stream old_stream + = octave_stream_list::lookup (args(0), "dup2"); - if (old_stream.is_valid () && new_stream.is_valid ()) + if (! error_state) { - int i_old = old_stream.file_number (); - int i_new = new_stream.file_number (); + octave_stream new_stream + = octave_stream_list::lookup (args(1), "dup2"); - if (i_old >= 0 && i_new >= 0) + if (! error_state) { - string msg; + int i_old = old_stream.file_number (); + int i_new = new_stream.file_number (); - int status = octave_syscalls::dup2 (i_old, i_new, msg); + if (i_old >= 0 && i_new >= 0) + { + string msg; - retval(0) = static_cast (status); - retval(1) = msg; + int status = octave_syscalls::dup2 (i_old, i_new, msg); + + retval(0) = static_cast (status); + retval(1) = msg; + } } - else - error ("dup2: invalid file id"); } else error ("dup2: invalid stream");