changeset 443:25570b554bca

[project @ 1994-06-02 17:05:58 by jwe]
author jwe
date Thu, 02 Jun 1994 17:05:58 +0000
parents 976182cb5e74
children ba637cc5c5f3
files liboctave/Matrix.cc liboctave/Matrix.h
diffstat 2 files changed, 227 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Matrix.cc	Wed Jun 01 01:05:09 1994 +0000
+++ b/liboctave/Matrix.cc	Thu Jun 02 17:05:58 1994 +0000
@@ -25,6 +25,7 @@
 #include "config.h"
 #endif
 
+#include <sys/types.h>
 #include <iostream.h>
 
 #include "Matrix.h"
@@ -4819,6 +4820,211 @@
 }
 
 /*
+ * Read an array of data froma file in binary format.
+ */
+int
+Matrix::read (FILE *fptr, int size, Matrix::conversion conv)
+{
+// Allocate buffer pointers.
+
+  union
+    {
+      void *vd;
+      char *ch;
+      u_char *uc;
+//    s_char *sc; // Some systems may need this?
+      short *sh;
+      u_short *us;
+      int *in;
+      u_int *ui;
+      long *ln;
+      u_long *ul;
+      float *fl;
+      double *db;
+    }
+  buf;
+
+  buf.db = fortran_vec ();
+
+// Read data directly into matrix data array.
+
+  int count = fread (buf.ch, size, length (), fptr);
+
+// Convert data to double.
+
+  int k;
+
+  switch (conv)
+    {
+    case CNV_DOUBLE:
+      break;
+
+    case CNV_CHAR:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.ch[k];
+      break;
+
+    case CNV_UCHAR:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.uc[k];
+      break;
+
+// Some systems may need this??
+//    case CNV_SCHAR:
+//      for (k = count - 1; k > -1; k--)
+//	buf.db[k] = buf.sc[k];
+//      break;
+
+    case CNV_SHORT:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.sh[k];
+      break;
+
+    case CNV_USHORT:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.us[k];
+      break;
+
+    case CNV_INT:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.in[k];
+      break;
+
+    case CNV_UINT:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.ui[k];
+      break;
+
+    case CNV_LONG:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.ln[k];
+      break;
+
+    case CNV_ULONG:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.ul[k];
+      break;
+
+    case CNV_FLOAT:
+      for (k = count - 1; k > -1; k--)
+	buf.db[k] = buf.fl[k];
+      break;
+
+    default:
+      return 0;
+    }
+
+  return count;
+}
+
+/*
+ * Write the data array to a file in binary format.
+ */
+int
+Matrix::write (FILE *fptr, int size, Matrix::conversion conv)
+{
+// Allocate buffer pointers.
+
+  union
+    {
+      void *vd;
+      char *ch;
+      u_char *uc;
+//    s_char *sc; // Some systems may need this?
+      short *sh;
+      u_short *us;
+      int *in;
+      u_int *ui;
+      long *ln;
+      u_long *ul;
+      float *fl;
+      double *db;
+    }
+  buf;
+
+  int len = length ();
+
+  if (conv != CNV_DOUBLE)
+    buf.db = new double [len];
+
+  double *bufi = fortran_vec ();
+
+// Convert from double to correct size.
+
+  int k;
+
+  switch (conv)
+    {
+    case CNV_DOUBLE:
+      buf.db = bufi;
+      break;
+
+    case CNV_CHAR:
+      for (k = 0; k < len; k++)
+	buf.ch[k] = (char) bufi[k];
+      break;
+
+    case CNV_UCHAR:
+      for (k = 0; k < len; k++)
+	buf.uc[k] = (u_char) bufi[k];
+      break;
+
+// Some systems may need this?
+//    case CNV_SCHAR:
+//      for (k = 0; k < len; k++)
+//	buf.uc[k] = (s_char) bufi[k];
+//      break;
+
+    case CNV_SHORT:
+      for (k = 0; k < len; k++)
+	buf.sh[k] = (short) bufi[k];
+      break;
+
+    case CNV_USHORT:
+      for (k = 0; k < len; k++)
+	buf.us[k] = (u_short) bufi[k];
+      break;
+
+    case CNV_INT:
+      for (k = 0; k < len; k++)
+	buf.in[k] = (int) bufi[k];
+      break;
+
+    case CNV_UINT:
+      for (k = 0; k < len; k++)
+	buf.ui[k] = (u_int) bufi[k];
+      break;
+
+    case CNV_LONG:
+      for (k = 0; k < len; k++)
+	buf.ln[k] = (long) bufi[k];
+      break;
+
+    case CNV_ULONG:
+      for (k = 0; k < len; k++)
+	buf.ul[k] = (u_long) bufi[k];
+      break;
+
+    case CNV_FLOAT:
+      for (k = 0; k < len; k++)
+	buf.fl[k] = (float) bufi[k];
+      break;
+
+    default:
+      return 0;
+  }
+
+// Write data from converted matrix data array.
+
+  int count = fwrite (buf.ch, size, length (), fptr);
+
+  if (conv != CNV_DOUBLE)
+    delete [] buf.db;
+
+  return count;
+}
+
+/*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
 ;;; page-delimiter: "^/\\*" ***
--- a/liboctave/Matrix.h	Wed Jun 01 01:05:09 1994 +0000
+++ b/liboctave/Matrix.h	Thu Jun 02 17:05:58 1994 +0000
@@ -36,6 +36,7 @@
 #include <math.h>
 #include <assert.h>
 #include <iostream.h>
+#include <stdio.h>
 // #include <iomanip.h>  // We don\'t use this yet.
 #include <Complex.h>
 
@@ -281,6 +282,26 @@
   friend ostream& operator << (ostream& os, const Matrix& a);
   friend istream& operator >> (istream& is, Matrix& a);
 
+  enum conversion
+    {
+      CNV_UNKNOWN,
+      CNV_UCHAR,
+      CNV_CHAR,
+      CNV_SCHAR,
+      CNV_SHORT,
+      CNV_USHORT,
+      CNV_INT,
+      CNV_UINT,
+      CNV_LONG,
+      CNV_ULONG,
+      CNV_FLOAT,
+      CNV_DOUBLE,
+    };
+
+
+  int read (FILE *fptr, int size, Matrix::conversion);
+  int write (FILE *fptr, int size, Matrix::conversion);
+
 // Until templates really work with g++:
 
 #define KLUDGE_MATRICES