[cig-commits] r8911 - cs/benchmark/cigma/trunk/src
luis at geodynamics.org
luis at geodynamics.org
Wed Dec 19 12:01:32 PST 2007
Author: luis
Date: 2007-12-19 12:01:31 -0800 (Wed, 19 Dec 2007)
New Revision: 8911
Added:
cs/benchmark/cigma/trunk/src/TextReader.cpp
cs/benchmark/cigma/trunk/src/TextReader.h
cs/benchmark/cigma/trunk/src/TextWriter.cpp
cs/benchmark/cigma/trunk/src/TextWriter.h
Log:
Split TextIO into TextReader/TextWriter
Added: cs/benchmark/cigma/trunk/src/TextReader.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/TextReader.cpp 2007-12-18 23:59:20 UTC (rev 8910)
+++ cs/benchmark/cigma/trunk/src/TextReader.cpp 2007-12-19 20:01:31 UTC (rev 8911)
@@ -0,0 +1,119 @@
+#include "TextReader.h"
+#include <cassert>
+#include <cstdlib>
+
+// ---------------------------------------------------------------------------
+cigma::TextReader::TextReader()
+{
+ fp = NULL;
+}
+
+cigma::TextReader::~TextReader()
+{
+ close();
+}
+
+// ---------------------------------------------------------------------------
+void cigma::TextReader::open(std::string filename)
+{
+ fp = fopen(filename.c_str(), "r");
+ assert(fp != NULL);
+}
+
+void cigma::TextReader::close()
+{
+ if (fp != NULL)
+ {
+ fclose(fp);
+ }
+}
+
+
+// ---------------------------------------------------------------------------
+
+static bool read_dmat(FILE *fp, double **mat, int *rows, int *cols)
+{
+ assert(fp != NULL);
+
+ int nrows, ncols;
+ int ret;
+
+ ret = fscanf(fp, "%d", &nrows);
+ assert(ret == 1);
+ assert(nrows > 0);
+
+ ret = fscanf(fp, "%d", &ncols);
+ assert(ret == 1);
+ assert(ncols > 0);
+
+ double *m = new double[nrows * ncols];
+
+ for (int i = 0; i < nrows; i++)
+ {
+ for (int j = 0; j < ncols; j++)
+ {
+ int n = ncols*i + j;
+ ret = fscanf(fp, "%lf", &m[n]);
+ assert(ret == 1);
+ }
+ }
+
+ *mat = m;
+ *rows = nrows;
+ *cols = ncols;
+
+ return true;
+}
+
+static bool read_imat(FILE *fp, int **mat, int *rows, int *cols)
+{
+ assert(fp != NULL);
+
+ int nrows, ncols;
+ int ret;
+
+ ret = fscanf(fp, "%d", &nrows);
+ assert(ret == 1);
+ assert(nrows > 0);
+
+ ret = fscanf(fp, "%d", &ncols);
+ assert(ret == 1);
+ assert(ncols > 0);
+
+ int *m = new int[nrows * ncols];
+
+ for (int i = 0; i < nrows; i++)
+ {
+ for (int j = 0; j < ncols; j++)
+ {
+ int n = ncols*i + j;
+ ret = fscanf(fp, "%d", &m[n]);
+ assert(ret == 1);
+ }
+ }
+
+ *mat = m;
+ *rows = nrows;
+ *cols = ncols;
+
+ return true;
+}
+
+// ---------------------------------------------------------------------------
+
+void cigma::TextReader::get_connectivity(int **connectivity, int *nel, int *ndofs)
+{
+ read_imat(fp, connectivity, nel, ndofs);
+}
+
+void cigma::TextReader::get_coordinates(double **coordinates, int *nno, int *nsd)
+{
+ read_dmat(fp, coordinates, nno, nsd);
+}
+
+void cigma::TextReader::get_dofs(double **dofs, int *num, int *ndim)
+{
+ read_dmat(fp, dofs, num, ndim);
+}
+
+// ---------------------------------------------------------------------------
Added: cs/benchmark/cigma/trunk/src/TextReader.h
===================================================================
--- cs/benchmark/cigma/trunk/src/TextReader.h 2007-12-18 23:59:20 UTC (rev 8910)
+++ cs/benchmark/cigma/trunk/src/TextReader.h 2007-12-19 20:01:31 UTC (rev 8911)
@@ -0,0 +1,33 @@
+#ifndef __TEXTREADER_H__
+#define __TEXTREADER_H__
+
+#include <cstdio>
+#include <string>
+
+namespace cigma
+{
+ class TextReader;
+};
+
+class cigma::TextReader
+{
+public:
+ TextReader();
+ ~TextReader();
+
+public:
+ void open(std::string filename);
+ void close();
+
+public:
+ void get_connectivity(int **connectivity, int *nel, int *ndofs);
+ void get_coordinates(double **coordinates, int *nno, int *nsd);
+ void get_dofs(double **dofs, int *nno, int *ndim);
+
+public:
+ FILE *fp;
+};
+
+// ---------------------------------------------------------------------------
+
+#endif
Added: cs/benchmark/cigma/trunk/src/TextWriter.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/TextWriter.cpp 2007-12-18 23:59:20 UTC (rev 8910)
+++ cs/benchmark/cigma/trunk/src/TextWriter.cpp 2007-12-19 20:01:31 UTC (rev 8911)
@@ -0,0 +1,89 @@
+#include "TextWriter.h"
+#include <cassert>
+#include <cstdlib>
+
+// ---------------------------------------------------------------------------
+cigma::TextWriter::TextWriter()
+{
+ fp = NULL;
+}
+
+cigma::TextWriter::~TextWriter()
+{
+ close();
+}
+
+// ---------------------------------------------------------------------------
+void cigma::TextWriter::open(std::string filename)
+{
+ fp = fopen(filename.c_str(), "w");
+ assert(fp != NULL);
+}
+
+void cigma::TextWriter::close()
+{
+ if (fp != NULL)
+ {
+ fclose(fp);
+ }
+}
+
+
+// ---------------------------------------------------------------------------
+
+static bool write_dmat(FILE *fp, double *mat, int rows, int cols)
+{
+ assert(fp != NULL);
+ assert(rows > 0);
+ assert(cols > 0);
+
+ fprintf(fp, "%d %d\n", rows, cols);
+ for (int i = 0; i < rows; i++)
+ {
+ for (int j = 0; j < cols; j++)
+ {
+ fprintf(fp, " %g", mat[cols*i + j]);
+ }
+ fprintf(fp, "\n");
+ }
+
+ return true;
+}
+
+static bool write_imat(FILE *fp, int *mat, int rows, int cols)
+{
+ assert(fp != NULL);
+ assert(rows > 0);
+ assert(cols > 0);
+
+ fprintf(fp, "%d %d\n", rows, cols);
+ for (int i = 0; i < rows; i++)
+ {
+ for (int j = 0; j < cols; j++)
+ {
+ fprintf(fp, " %d", mat[cols*i + j]);
+ }
+ fprintf(fp, "\n");
+ }
+
+ return true;
+}
+
+// ---------------------------------------------------------------------------
+
+void cigma::TextWriter::write_connectivity(int *connectivity, int nel, int ndofs)
+{
+ write_imat(fp, connectivity, nel, ndofs);
+}
+
+void cigma::TextWriter::write_coordinates(double *coordinates, int nno, int nsd)
+{
+ write_dmat(fp, coordinates, nno, nsd);
+}
+
+void cigma::TextWriter::write_dofs(double *dofs, int num, int ndim)
+{
+ write_dmat(fp, dofs, num, ndim);
+}
+
+// ---------------------------------------------------------------------------
Added: cs/benchmark/cigma/trunk/src/TextWriter.h
===================================================================
--- cs/benchmark/cigma/trunk/src/TextWriter.h 2007-12-18 23:59:20 UTC (rev 8910)
+++ cs/benchmark/cigma/trunk/src/TextWriter.h 2007-12-19 20:01:31 UTC (rev 8911)
@@ -0,0 +1,33 @@
+#ifndef __TEXTWRITER_H__
+#define __TEXTWRITER_H__
+
+#include <cstdio>
+#include <string>
+
+namespace cigma
+{
+ class TextWriter;
+};
+
+class cigma::TextWriter
+{
+public:
+ TextWriter();
+ ~TextWriter();
+
+public:
+ void open(std::string filename);
+ void close();
+
+public:
+ void write_connectivity(int *connectivity, int nel, int ndofs);
+ void write_coordinates(double *coordinates, int nno, int nsd);
+ void write_dofs(double *dofs, int nno, int ndim);
+
+public:
+ FILE *fp;
+};
+
+// ---------------------------------------------------------------------------
+
+#endif
More information about the cig-commits
mailing list