# HG changeset patch # User John W. Eaton # Date 1205284559 14400 # Node ID e0d7bb35a3ec0cf994e68c50d982af2214b049d4 # Parent b4aa9ef3d3ef253fca8b4cdfbee9de8bf2f1c167 style fixes diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2008-03-11 John W. Eaton + + * io/csread.m, io/csvwrite.m, io/dlmwrite.m: Style fixes. + 2008-03-12 David Bateman * io/dlmwrite.m, io/csvread.m, io/csvwrite.m: Files ported from diff --git a/scripts/io/csvread.m b/scripts/io/csvread.m --- a/scripts/io/csvread.m +++ b/scripts/io/csvread.m @@ -17,14 +17,17 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {x} = csvread (@var{filename}) +## @deftypefn {Function File} {@var{x}} = csvread (@var{filename}) ## Read the matrix @var{x} from a file. ## -## This function is equivalent to dlmread (@var{filename}, "," , ...) +## This function is equivalent to +## @example +## dlmread (@var{filename}, "," , @dots{}) +## @end example ## ## @seealso{dlmread, dlmwrite, csvwrite} ## @end deftypefn -function m = csvread (f, varargin) - m = dlmread (f, ',', varargin{:}); +function x = csvread (f, varargin) + x = dlmread (f, ",", varargin{:}); endfunction diff --git a/scripts/io/csvwrite.m b/scripts/io/csvwrite.m --- a/scripts/io/csvwrite.m +++ b/scripts/io/csvwrite.m @@ -17,14 +17,17 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {x} = csvwrite (@var{filename}, @var{x}) +## @deftypefn {Function File} {@var{x}} = csvwrite (@var{filename}, @var{x}) ## Write the matrix @var{x} to a file. ## -## This function is equivalent to dlmwrite(@var{filename},@var{x},",",...) +## This function is equivalent to +## @example +## dlmwrite (@var{filename}, @var{x}, ",", @dots{}) +## @end example ## ## @seealso{dlmread, dlmwrite, csvread} ## @end deftypefn -function csvwrite(f, m, varargin) - dlmwrite (f, m, ',', varargin{:}); +function csvwrite (f, m, varargin) + dlmwrite (f, m, ",", varargin{:}); endfunction diff --git a/scripts/io/dlmwrite.m b/scripts/io/dlmwrite.m --- a/scripts/io/dlmwrite.m +++ b/scripts/io/dlmwrite.m @@ -19,8 +19,8 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} dlmwrite (@var{file}, @var{a}) ## @deftypefnx {Function File} {} dmlwrite (@var{file}, @var{a}, @var{delim}, @var{r}, @var{c}) -## @deftypefnx {Function File} {} dmlwrite (@var{file}, @var{a}, 'attrib1', @var{value1}, 'attrib2', @var{value2}, @dots{}) -## @deftypefnx {Function File} {} dmlwrite (@var{file}, @var{a}, '-append', @dots{}) +## @deftypefnx {Function File} {} dmlwrite (@var{file}, @var{a}, "attrib1", @var{value1}, "attrib2", @var{value2}, @dots{}) +## @deftypefnx {Function File} {} dmlwrite (@var{file}, @var{a}, "-append", @dots{}) ## ## Write the matrix @var{a} to the text @var{file} using delimiters. ## @@ -34,28 +34,28 @@ ## @item c ## the number of delimiters to prepend to each line of data. ## -## @item '-append' +## @item "-append" ## append to the end of the @var{file}. ## -## @item 'append', state -## Either @samp{'on'} or @samp{'off'}. See @samp{'-append'} above. +## @item "append", state +## Either @samp{"on"} or @samp{"off"}. See @samp{"-append"} above. ## -## @item 'delimiter', d +## @item "delimiter", d ## See @var{delim} above. ## -## @item 'newline', os +## @item "newline", os ## The character(s) to use to separate each row. Three special cases -## exist for this option. @samp{'unix'} is changed into "\n", @samp{'pc'} is -## changed into "\r\n", and @samp{'mac'} is changed into "\r". Other -## values for this option are kept as is. +## exist for this option. @samp{"unix"} is changed into '\n', +## @samp{"pc"} is changed into '\r\n', and @samp{"mac"} is changed +## into '\r'. Other values for this option are kept as is. ## -## @item 'roffset', r +## @item "roffset", r ## See @var{r} above. ## -## @item 'coffset', c +## @item "coffset", c ## See @var{c} above. ## -## @item 'precision', p +## @item "precision", p ## The precision to use when writing the file. It can either be a ## format string (as used by fprintf) or a number of significant digits. ## @end table @@ -65,11 +65,8 @@ ## dlmwrite(@code{"file.csv"}, @var{A}) ## @end example ## -## Note the extra escaping of the backslashes necessary in using the -## latex delimiter of "\\" with a unix style newline. -## ## @example -## dlmwrite(@code{"file.tex"}, @var{a}, 'delimiter', '&', 'newline', '\\\\\n') +## dlmwrite (@code{"file.tex"}, @var{a}, "delimiter", "&", "newline", "\\n") ## @end example ## ## @seealso{dlmread, csvread, csvwrite} @@ -87,7 +84,7 @@ function dlmwrite (file, a, varargin) - if (nargin < 2 || ! ischar( file)) + if (nargin < 2 || ! ischar (file)) ptint_usage (); endif @@ -157,8 +154,8 @@ error (msg); else if (r > 0) - fprintf (fid, "%s", repmat ([repmat(delim, 1, c + columns(a) - 1), ... - newline], 1, r)); + fprintf (fid, "%s", + repmat ([repmat(delim, 1, c + columns(a)-1), newline], 1, r)); endif if (iscomplex (a)) cprecision = regexprep (precision, '^%([-\d.])','%+$1'); @@ -166,7 +163,7 @@ repmat([delim, precision, cprecision, "i"], 1, ... columns(a) - 1), newline ]; else - template = [precision, repmat([delim, precision], 1, columns(a) - 1),... + template = [precision, repmat([delim, precision], 1, columns(a)-1),... newline]; endif if (c > 0) diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -4,6 +4,16 @@ * DLD-FUNCTIONS/qp.cc (qp, Fqp): Likewise. * xpow.cc (xpow): Likewise. +2008-03-11 John W. Eaton + + * DLD-FUNCTIONS/dlmread.cc: Style fixes. + +2008-03-12 David Bateman + + * DLD-FUNCTIONS/dlmread.cc: Function ported from octave forge. Add + spreadsheet style ranges. + * Makefile.in (DLD_XSRC): Add dlmread.cc. + 2008-03-10 John W. Eaton * mex.cc (mxCreateLogicalScalar): Argument is now mxLogical. diff --git a/src/DLD-FUNCTIONS/dlmread.cc b/src/DLD-FUNCTIONS/dlmread.cc --- a/src/DLD-FUNCTIONS/dlmread.cc +++ b/src/DLD-FUNCTIONS/dlmread.cc @@ -20,17 +20,16 @@ */ -/* - Adapted from previous version of dlmread.occ as authored by Kai Habel, - but core code has been completely re-written. -*/ - -#include +// Adapted from previous version of dlmread.occ as authored by Kai +// Habel, but core code has been completely re-written. #ifdef HAVE_CONFIG_H #include #endif +#include +#include + #include "lo-ieee.h" #include "defun-dld.h" @@ -39,29 +38,21 @@ #include "utils.h" static bool -isletter (char c) +read_cell_spec (std::istream& is, unsigned long& row, unsigned long& col) { - return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); -} - -static bool -read_cell_spec(std::istream& is, unsigned long& row, unsigned long& col) -{ - bool stat = false; - if (is.peek () == std::istream::traits_type::eof()) + if (is.peek () == std::istream::traits_type::eof ()) stat = true; else { - if (isletter (is.peek ())) + if (::isalpha (is.peek ())) { - col = 0; - while (is && isletter (is.peek ())) + while (is && ::isalpha (is.peek ())) { char ch = is.get (); - col *= 26; + col *= 26; if (ch >= 'a') col += ch - 'a'; else @@ -82,9 +73,9 @@ } static bool -parse_range_spec(const octave_value& range_spec, - unsigned long& rlo, unsigned long& clo, - unsigned long& rup, unsigned long& cup) +parse_range_spec (const octave_value& range_spec, + unsigned long& rlo, unsigned long& clo, + unsigned long& rup, unsigned long& cup) { bool stat = true; @@ -139,12 +130,12 @@ if (stat && is && !is.eof ()) stat = read_cell_spec (is, rup, cup); - if (! is || !is.eof ()) + if (!is || !is.eof ()) stat = false; } else if (range_spec.is_real_matrix () && range_spec.numel () == 4) { - ColumnVector range(range_spec.vector_value()); + ColumnVector range(range_spec.vector_value ()); // double --> unsigned int rlo = static_cast (range(0)); clo = static_cast (range(1)); @@ -158,10 +149,10 @@ } DEFUN_DLD (dlmread, args, , - "-*- texinfo -*-\n\ + "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {@var{data} =} dlmread (@var{file})\n\ @deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep})\n\ -@deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{R0}, @var{C0})\n\ +@deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{r0}, @var{c0})\n\ @deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{range})\n\ Read the matrix @var{data} from a text file. If not defined the separator\n\ between fields is determined from the file itself. Otherwise the\n\ @@ -177,89 +168,101 @@ @end deftypefn") { octave_value_list retval; - int nargin = args.length(); - bool sepflag = 0; + + int nargin = args.length (); + if (nargin < 1 || nargin > 4) { print_usage (); return retval; } - if ( !args (0).is_string() ) + if (!args(0).is_string ()) { error ("dlmread: 1st argument must be a string"); return retval; } - std::string fname (args(0).string_value()); - std::ifstream file (fname.c_str()); + std::string fname (args(0).string_value ()); + if (error_state) + return retval; + + std::ifstream file (fname.c_str ()); if (!file) { - error("dlmread: could not open file"); + error ("dlmread: unable to open file `%s'", fname.c_str ()); return retval; } - // set default separator + // Set default separator. std::string sep; if (nargin > 1) { if (args(1).is_sq_string ()) - sep = do_string_escapes (args(1).string_value()); + sep = do_string_escapes (args(1).string_value ()); else - sep = args(1).string_value(); + sep = args(1).string_value (); + + if (error_state) + return retval; } - unsigned long i = 0, j = 0, r = 1, c = 1, rmax = 0, cmax = 0; - std::string line; - std::string str; - Matrix rdata; - ComplexMatrix cdata; - bool iscmplx = false; - size_t pos1, pos2; - - // take a subset if a range was given + // Take a subset if a range was given. unsigned long r0 = 0, c0 = 0, r1 = ULONG_MAX-1, c1 = ULONG_MAX-1; if (nargin > 2) { if (nargin == 3) { - if (! parse_range_spec(args (2), r0, c0, r1, c1)) + if (!parse_range_spec (args (2), r0, c0, r1, c1)) error ("dlmread: error parsing range"); } else if (nargin == 4) { - r0 = args(2).ulong_value(); - c0 = args(3).ulong_value(); + r0 = args(2).ulong_value (); + c0 = args(3).ulong_value (); + + if (error_state) + return retval; } } if (!error_state) { + unsigned long i = 0, j = 0, r = 1, c = 1, rmax = 0, cmax = 0; + + Matrix rdata; + ComplexMatrix cdata; + + bool iscmplx = false; + bool sepflag = false; + unsigned long maxrows = r1 - r0; - // Skip the r0 leading lines as these might be a header + std::string line; + + // Skip the r0 leading lines as these might be a header. for (unsigned long m = 0; m < r0; m++) getline (file, line); r1 -= r0; - - // read in the data one field at a time, growing the data matrix as needed + // Read in the data one field at a time, growing the data matrix + // as needed. while (getline (file, line)) { - // skip blank lines for compatibility + // Skip blank lines for compatibility. if (line.find_first_not_of (" \t") == NPOS) continue; - //to be compatible with matlab, blank separator should correspond - //to whitespace as delimter; - if (! sep.length ()) + // To be compatible with matlab, blank separator should + // correspond to whitespace as delimter. + if (!sep.length ()) { size_t n = line.find_first_of (",:; \t", - line.find_first_of ("0123456789")); + line.find_first_of ("0123456789")); if (n == NPOS) { sep = " \t"; - sepflag = 1; + sepflag = true; } else { @@ -269,7 +272,8 @@ { case ' ': case '\t': - sepflag = 1; + sepflag = true; + default: sep = ch; } @@ -278,65 +282,70 @@ r = (r > i + 1 ? r : i + 1); j = 0; - pos1 = 0; - do { - pos2 = line.find_first_of (sep, pos1); - str = line.substr (pos1, pos2 - pos1); - - if (sepflag && pos2 != NPOS) - // treat consecutive separators as one - pos2 = line.find_first_not_of (sep, pos2) - 1; + size_t pos1 = 0; + do + { + size_t pos2 = line.find_first_of (sep, pos1); + std::string str = line.substr (pos1, pos2 - pos1); - c = (c > j + 1 ? c : j + 1); - if (r > rmax || c > cmax) - { - // use resize_and_fill for the case of not-equal length rows - if (iscmplx) - cdata.resize_and_fill (r, c, 0); - else - rdata.resize_and_fill (r, c, 0); - rmax = r; - cmax = c; - } + if (sepflag && pos2 != NPOS) + // Treat consecutive separators as one. + pos2 = line.find_first_not_of (sep, pos2) - 1; - std::istringstream tmp_stream (str); - double x = octave_read_double (tmp_stream); - if (tmp_stream) - { - if (tmp_stream.eof()) + c = (c > j + 1 ? c : j + 1); + if (r > rmax || c > cmax) + { + // Use resize_and_fill for the case of not-equal + // length rows. if (iscmplx) - cdata (i, j++) = x; + cdata.resize_and_fill (r, c, 0); else - rdata (i, j++) = x; - else - { - double y = octave_read_double (tmp_stream); + rdata.resize_and_fill (r, c, 0); + rmax = r; + cmax = c; + } - if (!iscmplx && y != 0.) - { - iscmplx = true; - cdata = ComplexMatrix (rdata); - } - + std::istringstream tmp_stream (str); + double x = octave_read_double (tmp_stream); + if (tmp_stream) + { + if (tmp_stream.eof ()) if (iscmplx) - cdata (i, j++) = Complex (x, y); + cdata(i,j++) = x; else - rdata (i, j++) = x; - } - } - else if (iscmplx) - cdata (i, j++) = 0.; - else - rdata (i, j++) = 0.; + rdata(i,j++) = x; + else + { + double y = octave_read_double (tmp_stream); + + if (!iscmplx && y != 0.) + { + iscmplx = true; + cdata = ComplexMatrix (rdata); + } - if (pos2 != NPOS) - pos1 = pos2 + 1; - else - pos1 = NPOS; + if (iscmplx) + cdata(i,j++) = Complex (x, y); + else + rdata(i,j++) = x; + } + } + else if (iscmplx) + cdata(i,j++) = 0.; + else + rdata(i,j++) = 0.; - } while ( pos1 != NPOS ); + if (pos2 != NPOS) + pos1 = pos2 + 1; + else + pos1 = NPOS; + + } + while (pos1 != NPOS); + if (nargin == 3 && i == maxrows) break; + i++; } @@ -351,12 +360,13 @@ } else if (nargin == 4) { - // if r1 and c1 are not given, use what was found to be the maximum + // If r1 and c1 are not given, use what was found to be + // the maximum. r1 = r - 1; c1 = c - 1; } - // now take the subset of the matrix + // Now take the subset of the matrix. if (iscmplx) { cdata = cdata.extract (0, c0, r1, c1); @@ -370,9 +380,9 @@ } if (iscmplx) - retval(0) = octave_value(cdata); + retval(0) = cdata; else - retval(0) = octave_value(rdata); + retval(0) = rdata; } return retval;