# HG changeset patch # User jwe # Date 837384389 0 # Node ID 31e7eb125d89b1be44fe1f21e9ddc7537fa1c056 # Parent 8c09c04f7747bea5cbdbec47170e8b911af22852 [project @ 1996-07-14 22:44:50 by jwe] diff --git a/src/file-io.cc b/src/file-io.cc --- a/src/file-io.cc +++ b/src/file-io.cc @@ -258,11 +258,11 @@ if (! error_state) { - octave_base_stream::arch_type at - = octave_stream::string_to_arch_type (arch); + oct_mach_info::float_format flt_fmt = + oct_mach_info::string_to_float_format (arch); if (! error_state) - retval = new octave_fstream (name, md, at); + retval = new octave_fstream (name, md, flt_fmt); } return retval; @@ -301,9 +301,12 @@ } DEFUN (fopen, args, , - "FILENUM = fopen (FILENAME, MODE [, ARCH]): open a file\n\ + "[FILENUM, ERRMSG] = fopen (FILENAME, MODE [, ARCH]): open a file\n\ \n\ - Valid values for mode include:\n\ + FILENAME is a string specifying the name of the file.\n\ +\n\ + MODE is a string specifying whether the file should be opened for\n\ + reading, writing, or both. Valid values for MODE include:\n\ \n\ r : open text file for reading\n\ w : open text file for writing; discard previous contents if any\n\ @@ -312,7 +315,25 @@ w+ : create text file for update; discard previous contents if any\n\ a+ : append; open or create text file for update, writing at end\n\ \n\ - Update mode permits reading from and writing to the same file.") + Update mode permits reading from and writing to the same file.\n\ +\n\ + ARCH is a string specifying the default data format for the file.\n\ + Valid values for ARCH are:\n\ +\n\ + native -- the format of the current machine (this is the default)\n\ + ieee-le -- IEEE big endian\n\ + ieee-be -- IEEE little endian\n\ + vaxd -- VAX D floating format\n\ + vaxg -- VAX G floating format\n\ + cray -- Cray floating format\n +\n\ + however, conversions are currently only supported for ieee-be, and\n\ + ieee-le formats.\n\ +\n\ +\n\ + FILENUM is a number that can be used to refer to the open file.\n\ + If fopen fails, FILENUM is set to -1 and ERRMSG contains a\n\ + system-dependent error message") { octave_value_list retval = -1.0; @@ -807,8 +828,8 @@ if (! error_state) { - octave_base_stream::data_type dt - = octave_stream::string_to_data_type (prec); + oct_data_conv::data_type dt + = oct_data_conv::string_to_data_type (prec); if (! error_state) { @@ -824,11 +845,11 @@ if (! error_state) { - octave_base_stream::arch_type at - = octave_stream::string_to_arch_type (arch); + oct_mach_info::float_format flt_fmt + = oct_mach_info::string_to_float_format (arch); if (! error_state) - retval = os.read (size, dt, skip, at, count); + retval = os.read (size, dt, skip, flt_fmt, count); } else ::error ("fread: architecture type must be a string"); @@ -854,23 +875,54 @@ DEFUN (fread, args, , "[DATA, COUNT] = fread (FILENUM [, SIZE] [, PRECISION] [, SKIP] [, ARCH])\n\ \n\ - Reads data in binary form of type PRECISION from a file.\n\ +Reads data in binary form of type PRECISION from a file.\n\ +\n\ + FILENUM : file number from fopen\n\ \n\ - FILENUM : file number from fopen\n\ - SIZE : size specification for the Data matrix\n\ - PRECISION : type of data to read, valid types are\n\ + SIZE : size specification for the data matrix\n\ +\n\ + PRECISION : string specifying type of data to read, valid types are\n\ \n\ - \"char\" \"schar\" \"short\" \"int\" \"long\" \"float\"\n\ - \"double\" \"uchar\" \"ushort\" \"uint\" \"ulong\"\n\ + char, char*1, integer*1, int8 -- character\n\ + schar, signed char -- signed character\n\ + uchar, unsigned char -- unsigned character (default)\n\ + short -- short integer\n\ + ushort, unsigned short -- unsigned short integer\n\ + int -- integer\n\ + uint, unsigned int -- unsigned integer\n\ + long -- long integer\n\ + ulong, unsigned long -- unsigned long integer\n\ + float, float32, real*4 -- single precision float\n\ + double, float64, real*8 -- double precision float\n\ + int16, integer*2 -- two byte integer\n\ + int32, integer*4 -- four byte integer\n\ +\n\ + SKIP : number of bytes to skip before each element is read\n\ + (default is 0)\n\ \n\ - DATA : matrix in which the data is stored\n\ - COUNT : number of elements read") + ARCH : string specifying the data format for the file. Valid + values are\n\ +\n\ + native -- the format of the current machine (default)\n\ + ieee-le -- IEEE big endian\n\ + ieee-be -- IEEE little endian\n\ + vaxd -- VAX D floating format\n\ + vaxg -- VAX G floating format\n\ + cray -- Cray floating format\n +\n\ + however, conversions are currently only supported for ieee-be, and\n\ + ieee-le formats.\n\ +\n\ +\n\ + DATA : matrix in which the data is stored\n\ +\n\ + COUNT : number of elements read") { octave_value_list retval; int nargin = args.length (); - if (nargin > 1 && nargin < 6) + if (nargin > 0 && nargin < 6) { retval(1) = -1.0; retval(0) = Matrix (); @@ -889,7 +941,7 @@ ? args(3) : octave_value (0.0); octave_value arch = (nargin > 4) - ? args(4) : octave_value ("native"); + ? args(4) : octave_value ("unknown"); int count = -1; @@ -918,8 +970,8 @@ if (! error_state) { - octave_base_stream::data_type dt - = octave_stream::string_to_data_type (prec); + oct_data_conv::data_type dt + = oct_data_conv::string_to_data_type (prec); if (! error_state) { @@ -935,11 +987,11 @@ if (! error_state) { - octave_base_stream::arch_type at - = octave_stream::string_to_arch_type (arch); + oct_mach_info::float_format flt_fmt + = oct_mach_info::string_to_float_format (arch); if (! error_state) - retval = os.write (data, dt, skip, at); + retval = os.write (data, dt, skip, flt_fmt); } else ::error ("fwrite: architecture type must be a string"); @@ -960,16 +1012,46 @@ DEFUN (fwrite, args, , "COUNT = fwrite (FILENUM, DATA [, PRECISION] [, SKIP] [, ARCH])\n\ \n\ - Writes data to a file in binary form of size PRECISION\n\ + Writes data to a file in binary form of size PRECISION\n\ +\n\ + FILENUM : file number from fopen\n\ +\n\ + DATA : matrix of elements to be written\n\ +\n\ + PRECISION : string specifying type of data to read, valid types are\n\ \n\ - FILENUM : file number from fopen\n\ - DATA : matrix of elements to be written\n\ - PRECISION : type of data to read, valid types are\n\ + char, char*1, integer*1, int8 -- character\n\ + schar, signed char -- signed character\n\ + uchar, unsigned char -- unsigned character (default)\n\ + short -- short integer\n\ + ushort, unsigned short -- unsigned short integer\n\ + int -- integer\n\ + uint, unsigned int -- unsigned integer\n\ + long -- long integer\n\ + ulong, unsigned long -- unsigned long integer\n\ + float, float32, real*4 -- single precision float\n\ + double, float64, real*8 -- double precision float\n\ + int16, integer*2 -- two byte integer\n\ + int32, integer*4 -- four byte integer\n\ \n\ - \"char\" \"schar\" \"short\" \"int\" \"long\" \"float\"\n\ - \"double\" \"uchar\" \"ushort\" \"uint\" \"ulong\"\n\ + SKIP : number of bytes to skip before each element is read\n\ + (the default is 0)\n\ +\n\ + ARCH : string specifying the data format for the file. Valid + values are\n\ \n\ - COUNT : number of elements written") + native -- the format of the current machine (default)\n\ + ieee-le -- IEEE big endian\n\ + ieee-be -- IEEE little endian\n\ + vaxd -- VAX D floating format\n\ + vaxg -- VAX G floating format\n\ + cray -- Cray floating format\n +\n\ + however, conversions are currently only supported for ieee-be, and\n\ + ieee-le formats.\n\ +\n\ +\n\ + COUNT : number of elements written") { octave_value retval = -1.0; @@ -982,9 +1064,15 @@ if (os) { octave_value data = args(1); - octave_value prec = (nargin > 2) ? args(2) : octave_value ("uchar"); - octave_value skip = (nargin > 3) ? args(3) : octave_value (0.0); - octave_value arch = (nargin > 4) ? args(4) : octave_value ("native"); + + octave_value prec = (nargin > 2) + ? args(2) : octave_value ("uchar"); + + octave_value skip = (nargin > 3) + ? args(3) : octave_value (0.0); + + octave_value arch = (nargin > 4) + ? args(4) : octave_value ("unknown"); retval = do_fwrite (*os, data, prec, skip, arch); } diff --git a/src/load-save.cc b/src/load-save.cc --- a/src/load-save.cc +++ b/src/load-save.cc @@ -36,7 +36,7 @@ #include "byte-swap.h" #include "data-conv.h" -#include "float-fmt.h" +#include "mach-info.h" #include "oct-glob.h" #include "str-vec.h" @@ -703,7 +703,8 @@ // FILENAME is used for error messages. static char * -read_binary_data (istream& is, int swap, floating_point_format fmt, +read_binary_data (istream& is, int swap, + oct_mach_info::float_format fmt, const string& filename, int& global, octave_value& tc, char *&doc) { @@ -913,7 +914,8 @@ static void read_mat_binary_data (istream& is, double *data, int precision, - int len, int swap, floating_point_format flt_fmt) + int len, int swap, + oct_mach_info::float_format flt_fmt) { switch (precision) { @@ -983,7 +985,7 @@ // // Gag me. - if (octave_words_big_endian && mopt == 0) + if (oct_mach_info::words_big_endian () && mopt == 0) swap = 1; // mopt is signed, therefore byte swap may result in negative value. @@ -1016,41 +1018,75 @@ // We don't just use a cast here, because we need to be able to detect // possible errors. -static floating_point_format -get_floating_point_format (int mach) +static oct_mach_info::float_format +mopt_digit_to_float_format (int mach) { - floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; + oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; switch (mach) { case 0: - flt_fmt = OCTAVE_IEEE_LITTLE; + flt_fmt = oct_mach_info::ieee_little_endian; break; case 1: - flt_fmt = OCTAVE_IEEE_BIG; + flt_fmt = oct_mach_info::ieee_big_endian; break; case 2: - flt_fmt = OCTAVE_VAX_D; + flt_fmt = oct_mach_info::vax_d; break; case 3: - flt_fmt = OCTAVE_VAX_G; + flt_fmt = oct_mach_info::vax_g; break; case 4: - flt_fmt = OCTAVE_CRAY; + flt_fmt = oct_mach_info::cray; break; default: - flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; + flt_fmt = oct_mach_info::unknown; break; } return flt_fmt; } +static int +float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) +{ + int retval = -1; + + switch (flt_fmt) + { + case oct_mach_info::ieee_little_endian: + retval = 0; + break; + + case oct_mach_info::ieee_big_endian: + retval = 1; + break; + + case oct_mach_info::vax_d: + retval = 2; + break; + + case oct_mach_info::vax_g: + retval = 3; + break; + + case oct_mach_info::cray: + retval = 4; + break; + + default: + break; + } + + return retval; +} + // Extract one value (scalar, matrix, string, etc.) from stream IS and // place it in TC, returning the name of the variable. // @@ -1070,7 +1106,7 @@ // initialization of variable. Matrix re; - floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; + oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; char *name = 0; int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; @@ -1091,8 +1127,9 @@ mopt /= 100; // Skip unused third digit too. mach = mopt % 10; // IEEE, VAX, etc. - flt_fmt = get_floating_point_format (mach); - if (flt_fmt == OCTAVE_UNKNOWN_FLT_FMT) + flt_fmt = mopt_digit_to_float_format (mach); + + if (flt_fmt == oct_mach_info::unknown) { error ("load: unrecognized binary format!"); return 0; @@ -1180,16 +1217,17 @@ static int read_binary_file_header (istream& is, int& swap, - floating_point_format& flt_fmt, int quiet = 0) + oct_mach_info::float_format& flt_fmt, + int quiet = 0) { int magic_len = 10; char magic [magic_len+1]; is.read (magic, magic_len); magic[magic_len] = '\0'; if (strncmp (magic, "Octave-1-L", magic_len) == 0) - swap = octave_words_big_endian; + swap = oct_mach_info::words_big_endian (); else if (strncmp (magic, "Octave-1-B", magic_len) == 0) - swap = ! octave_words_big_endian; + swap = ! oct_mach_info::words_big_endian (); else { if (! quiet) @@ -1200,8 +1238,9 @@ char tmp = 0; is.read (&tmp, 1); - flt_fmt = get_floating_point_format (tmp); - if (flt_fmt == OCTAVE_UNKNOWN_FLT_FMT) + flt_fmt = mopt_digit_to_float_format (tmp); + + if (flt_fmt == oct_mach_info::unknown) { if (! quiet) error ("load: unrecognized binary format!"); @@ -1225,7 +1264,7 @@ } int swap; - floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; + oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) retval = LS_BINARY; @@ -1264,7 +1303,7 @@ static octave_value_list do_load (istream& stream, const string& orig_fname, int force, - load_save_format format, floating_point_format flt_fmt, + load_save_format format, oct_mach_info::float_format flt_fmt, int list_only, int swap, int verbose, const string_vector& argv, int argv_idx, int argc, int nargout) { @@ -1442,7 +1481,7 @@ string orig_fname = argv[i]; - floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; + oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; int swap = 0; @@ -1726,7 +1765,11 @@ FOUR_BYTE_INT mopt = 0; mopt += tc.is_string () ? 1 : 0; - mopt += 1000 * get_floating_point_format (native_float_format); + + oct_mach_info::float_format flt_fmt = + oct_mach_info::native_float_format ();; + + mopt += 1000 * float_format_to_mopt_digit (flt_fmt); os.write (&mopt, 4); @@ -2128,9 +2171,13 @@ { if (format == LS_BINARY) { - os << (octave_words_big_endian ? "Octave-1-B" : "Octave-1-L"); + os << (oct_mach_info::words_big_endian () + ? "Octave-1-B" : "Octave-1-L"); - char tmp = (char) native_float_format; + oct_mach_info::float_format flt_fmt = + oct_mach_info::native_float_format (); + + char tmp = (char) float_format_to_mopt_digit (flt_fmt); os.write (&tmp, 1); }