changeset 8425:2e777f5135a3

support -tabs option for save -ascii
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 25 Dec 2008 12:31:07 +0100
parents a84d71abdc5b
children 7523742aa026
files src/ChangeLog src/load-save.cc src/load-save.h src/ls-mat-ascii.cc src/ls-mat-ascii.h
diffstat 5 files changed, 73 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Dec 24 16:45:11 2008 -0500
+++ b/src/ChangeLog	Thu Dec 25 12:31:07 2008 +0100
@@ -1,3 +1,14 @@
+2008-12-25  Jaroslav Hajek <highegg@gmail.com>
+
+	* load-save.h (load_save_format_type, load_save_format_options): New
+	enums.
+	(load_save_format): Convert to a class.
+	* load-save.cc (do_save(..., const octave_value&,...)): Use fmt.type
+	in switch. Use options of LS_MAT_ASCII.
+	(do_load, write_header): Use format.type in switch.
+	* ls-mat-ascii.h (save_mat_ascii_data): Add tabs option.
+	* ls-mat-ascii.cc (save_mat_ascii_data): Implement it.
+
 2008-12-24  Ben Abbott  <bpabbott@mac.com>
 
 	* DLD-FUNCTIONS/chol.cc: Compare results to 0 in tests.
--- a/src/load-save.cc	Wed Dec 24 16:45:11 2008 -0500
+++ b/src/load-save.cc	Thu Dec 25 12:31:07 2008 +0100
@@ -347,7 +347,7 @@
       std::string name;
       std::string doc;
 
-      switch (format)
+      switch (format.type)
 	{
 	case LS_ASCII:
 	  name = read_ascii_data (stream, orig_fname, global, tc, count);
@@ -928,7 +928,7 @@
 	 const std::string& name, const std::string& help,
 	 bool global, load_save_format fmt, bool save_as_floats)
 {
-  switch (fmt)
+  switch (fmt.type)
     {
     case LS_ASCII:
       save_ascii_data (os, tc, name, global, 0);
@@ -939,8 +939,8 @@
       break;
 
     case LS_MAT_ASCII:
-    case LS_MAT_ASCII_LONG:
-      if (! save_mat_ascii_data (os, tc, fmt == LS_MAT_ASCII ? 8 : 16))
+      if (! save_mat_ascii_data (os, tc, fmt.opts & LS_MAT_ASCII_LONG ? 16 : 8, 
+                                 fmt.opts & LS_MAT_ASCII_TABS))
 	warning ("save: unable to save %s in ASCII format", name.c_str ());
       break;
 
@@ -1048,7 +1048,7 @@
   string_vector retval;
   int argc = argv.length ();
 
-  bool do_double = false;
+  bool do_double = false, do_tabs = false;
 
   for (int i = 0; i < argc; i++)
     {
@@ -1064,6 +1064,10 @@
 	{
 	  do_double = true;
 	}
+      else if (argv[i] == "-tabs")
+	{
+	  do_tabs = true;
+	}
       else if (argv[i] == "-text" || argv[i] == "-t")
 	{
 	  format = LS_ASCII;
@@ -1125,11 +1129,19 @@
   if (do_double)
     {
       if (format == LS_MAT_ASCII)
-	format = LS_MAT_ASCII_LONG;
+	format.opts |= LS_MAT_ASCII_LONG;
       else
 	warning ("save: \"-double\" option only has an effect with \"-ascii\"");
     }
 
+  if (do_tabs)
+    {
+      if (format == LS_MAT_ASCII)
+	format.opts |= LS_MAT_ASCII_TABS;
+      else
+	warning ("save: \"-tabs\" option only has an effect with \"-ascii\"");
+    }
+
   return retval;
 }
 
@@ -1155,7 +1167,7 @@
 void
 write_header (std::ostream& os, load_save_format format)
 {
-  switch (format)
+  switch (format.type)
     {
     case LS_BINARY:
       {
--- a/src/load-save.h	Wed Dec 24 16:45:11 2008 -0500
+++ b/src/load-save.h	Thu Dec 25 12:31:07 2008 +0100
@@ -30,12 +30,13 @@
 
 class octave_value;
 
-enum load_save_format
+// FIXME: maybe MAT5 and MAT7 should be options to MAT_BINARY.
+// Similarly, save_as_floats may be an option for LS_BINARY, LS_HDF5 etc.
+enum load_save_format_type
   {
     LS_ASCII,
     LS_BINARY,
     LS_MAT_ASCII,
-    LS_MAT_ASCII_LONG,
     LS_MAT_BINARY,
     LS_MAT5_BINARY,
     LS_MAT7_BINARY,
@@ -45,6 +46,29 @@
     LS_UNKNOWN
   };
 
+enum load_save_format_options
+{
+  // LS_MAT_ASCII options (not exclusive)
+  LS_MAT_ASCII_LONG = 1,
+  LS_MAT_ASCII_TABS = 2,
+  // LS_MAT_BINARY options
+  LS_MAT_BINARY_V5 = 1,
+  LS_MAT_BINARY_V7,
+  // zero means no option.
+  LS_NO_OPTION = 0  
+};
+
+class load_save_format
+{
+public:
+  load_save_format (load_save_format_type t,
+                    load_save_format_options o = LS_NO_OPTION)
+    : type (t), opts (o) { }
+  operator int (void) const
+    { return type; }
+  int type, opts;
+};
+
 extern void dump_octave_core (void);
 
 extern int
--- a/src/ls-mat-ascii.cc	Wed Dec 24 16:45:11 2008 -0500
+++ b/src/ls-mat-ascii.cc	Thu Dec 25 12:31:07 2008 +0100
@@ -329,7 +329,7 @@
 
 bool
 save_mat_ascii_data (std::ostream& os, const octave_value& val,
-		     int precision)
+		     int precision, bool tabs)
 {
   bool success = true;
 
@@ -353,7 +353,21 @@
       std::ios::fmtflags oflags
 	= os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific));
 
-      os << m;
+      if (tabs)
+        {
+          for (octave_idx_type i = 0; i < m.rows (); i++)
+            {
+              for (octave_idx_type j = 0; j < m.cols (); j++)
+                {
+                  // Omit leading tabs.
+                  if (j != 0) os << '\t'; 
+                  octave_write_double (os, m (i, j));
+                }
+              os << "\n";
+            }
+        }
+      else
+        os << m;
 
       os.flags (oflags);
 
--- a/src/ls-mat-ascii.h	Wed Dec 24 16:45:11 2008 -0500
+++ b/src/ls-mat-ascii.h	Thu Dec 25 12:31:07 2008 +0100
@@ -29,7 +29,7 @@
 
 extern bool
 save_mat_ascii_data (std::ostream& os, const octave_value& val_arg,
-		     int precision);
+		     int precision, bool tabs = false);
 
 #endif