[cig-commits] r13574 - in cs/cigma/trunk: src tests/libcigma

luis at geodynamics.org luis at geodynamics.org
Tue Dec 9 18:14:50 PST 2008


Author: luis
Date: 2008-12-09 18:14:50 -0800 (Tue, 09 Dec 2008)
New Revision: 13574

Modified:
   cs/cigma/trunk/src/io_file_reader.cpp
   cs/cigma/trunk/src/io_file_reader.h
   cs/cigma/trunk/src/io_file_writer.cpp
   cs/cigma/trunk/src/io_file_writer.h
   cs/cigma/trunk/src/io_hdf5_reader.cpp
   cs/cigma/trunk/src/io_hdf5_reader.h
   cs/cigma/trunk/src/io_hdf5_writer.cpp
   cs/cigma/trunk/src/io_hdf5_writer.h
   cs/cigma/trunk/src/io_null_reader.cpp
   cs/cigma/trunk/src/io_null_reader.h
   cs/cigma/trunk/src/io_null_writer.cpp
   cs/cigma/trunk/src/io_null_writer.h
   cs/cigma/trunk/src/io_text_reader.cpp
   cs/cigma/trunk/src/io_text_reader.h
   cs/cigma/trunk/src/io_text_writer.cpp
   cs/cigma/trunk/src/io_text_writer.h
   cs/cigma/trunk/src/io_vtk_writer.cpp
   cs/cigma/trunk/src/io_vtk_writer.h
   cs/cigma/trunk/tests/libcigma/ReaderTest.cpp
Log:
Added mode argument to File{Reader,Writer}::open() methods

Modified: cs/cigma/trunk/src/io_file_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_file_reader.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_file_reader.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -33,25 +33,20 @@
     {
         if (is_hdf5_extension(ext.c_str()))
         {
-            //*
             tmp.reset(new HDF5_Reader());
-            status = tmp->open(filename.c_str());
+            status = tmp->open(filename.c_str(), mode.c_str());
             if (status < 0)
             {
                 std::ostringstream stream;
                 stream << "Could not open HDF5 file '" << filename << "' for reading" << std::ends;
                 throw cigma::Exception("FileReader::New", stream.str());
-            } // */
-            
-            //
-            //throw cigma::Exception("FileReader::New", "Need HDF5_Reader");
-            //
+            }
         }
         else if (is_vtk_extension(ext.c_str()))
         {
             /*
             tmp.reset(new VtkReader());
-            status = tmp->open(filename.c_str());
+            status = tmp->open(filename.c_str(), mode.c_str());
             if (status < 0)
             {
                 std::ostringstream stream;
@@ -63,7 +58,7 @@
         else if (is_text_extension(ext.c_str()))
         {
             tmp.reset(new TextReader());
-            status = tmp->open(filename.c_str());
+            status = tmp->open(filename.c_str(), mode.c_str());
             if (status < 0)
             {
                 std::ostringstream stream;

Modified: cs/cigma/trunk/src/io_file_reader.h
===================================================================
--- cs/cigma/trunk/src/io_file_reader.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_file_reader.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -22,7 +22,7 @@
     FileReader();
     virtual ~FileReader();
 
-    virtual int open(const char *filename) = 0;
+    virtual int open(const char *filename, const char *mode) = 0;
     virtual int close() = 0;
 
     virtual int getDataset(const char *loc, double **data, int *num, int *dim) = 0;

Modified: cs/cigma/trunk/src/io_file_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_file_writer.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_file_writer.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -48,7 +48,7 @@
     if (is_hdf5_extension(ext.c_str()))
     {
         tmp.reset(new HDF5_Writer());
-        status = tmp->open(filename.c_str());
+        status = tmp->open(filename.c_str(), mode.c_str());
         if (status < 0)
         {
             std::ostringstream stream;
@@ -61,7 +61,7 @@
         throw cigma::Exception("FileWriter::New", "Need VTK writer");
         /*
         tmp.reset(new VtkWriter());
-        status = tmp->open(filename.c_str());
+        status = tmp->open(filename.c_str(), mode.c_str());
         if (status < 0)
         {
             std::ostringstream stream;
@@ -73,7 +73,7 @@
     else if (is_text_extension(ext.c_str()))
     {
         tmp.reset(new TextWriter());
-        status = tmp->open(filename.c_str());
+        status = tmp->open(filename.c_str(), mode.c_str());
         if (status < 0)
         {
             std::ostringstream stream;

Modified: cs/cigma/trunk/src/io_file_writer.h
===================================================================
--- cs/cigma/trunk/src/io_file_writer.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_file_writer.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -21,7 +21,7 @@
     FileWriter();
     virtual ~FileWriter();
 
-    virtual int open(const char *filename) = 0; // XXX: add "const char *mode" argument
+    virtual int open(const char *filename, const char *mode) = 0;
     virtual int close() = 0;
 
     virtual int writeDataset(const char *loc, const double *data, int nno, int ndim) = 0;

Modified: cs/cigma/trunk/src/io_hdf5_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_hdf5_reader.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_hdf5_reader.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -20,21 +20,47 @@
 // ----------------------------------------------------------------------------
 
 
-int HDF5_Reader::open(const char *filename)
+int HDF5_Reader::open(const char *filename, const char *mode)
 {
     TRI_LOG_STR("HDF5_Reader::open()");
     TRI_LOG(filename);
 
+    H5::Exception::dontPrint();
+
+    if (file != 0) { return -2; }
+
     try
     {
-        H5::Exception::dontPrint();
-        file = new H5::H5File(filename, H5F_ACC_RDONLY);
+        string m(mode);
+
+        if (m == "r")
+        {
+            file = new H5::H5File(filename, H5F_ACC_RDONLY);
+        }
+        else if (m == "a")
+        {
+            try
+            {
+                file = new H5::H5File(filename, H5F_ACC_RDWR);
+            }
+            catch (H5::FileIException e)
+            {
+            }
+
+            if (file == 0)
+            {
+                file = new H5::H5File(filename, H5F_ACC_TRUNC);
+            }
+        }
     }
     catch (H5::FileIException error)
     {
         error.printError();
         return -1;
     }
+
+    if (file == 0) { return -3; }
+
     return 0;
 }
 

Modified: cs/cigma/trunk/src/io_hdf5_reader.h
===================================================================
--- cs/cigma/trunk/src/io_hdf5_reader.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_hdf5_reader.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -17,7 +17,7 @@
     
     ReaderType getReaderType() { return HDF5_FILE_READER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int getDataset(const char *loc, double **data, int *num, int *dim);

Modified: cs/cigma/trunk/src/io_hdf5_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_hdf5_writer.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_hdf5_writer.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -18,26 +18,47 @@
 
 // ----------------------------------------------------------------------------
 
-int HDF5_Writer::open(const char *filename)
+int HDF5_Writer::open(const char *filename, const char *mode)
 {
     TRI_LOG_STR("HDF5_Writer::open()");
     TRI_LOG(filename);
 
+    H5::Exception::dontPrint();
+
+    if (file != 0) { return -2; }
+
     try
     {
-        H5::Exception::dontPrint();
+        string m(mode);
 
-        // XXX: add mode to argument list
-        // if (mode is rdwr) { file = new H5::H5File(filename, H5F_ACC_RDWR); }
-        // if (mode is create) { file = new H5::H5File(filename, H5F_ACC_CREAT); }
-        // if (mode is trunc) { file = new H5::H5File(filename, H5F_ACC_TRUNC); }
-        file = new H5::H5File(filename, H5F_ACC_TRUNC);
+        if (m == "w")
+        {
+            file = new H5::H5File(filename, H5F_ACC_TRUNC);
+        }
+        else if (m == "a")
+        {
+            try
+            {
+                file = new H5::H5File(filename, H5F_ACC_RDWR);
+            }
+            catch (H5::FileIException e)
+            {
+            }
+
+            if (file == 0)
+            {
+                file = new H5::H5File(filename, H5F_ACC_TRUNC);
+            }
+        }
     }
     catch (H5::FileIException error)
     {
         error.printError();
         return -1;
     }
+
+    if (file == 0) { return -3; }
+
     return 0;
 }
 

Modified: cs/cigma/trunk/src/io_hdf5_writer.h
===================================================================
--- cs/cigma/trunk/src/io_hdf5_writer.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_hdf5_writer.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -17,7 +17,7 @@
 
     WriterType getWriterType() { return HDF5_FILE_WRITER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int writeDataset(const char *loc, const double *data, int nno, int ndim);

Modified: cs/cigma/trunk/src/io_null_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_null_reader.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_null_reader.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -6,7 +6,7 @@
 NullReader::NullReader() {}
 NullReader::~NullReader() {}
 
-int NullReader::open(const char *filename) { return -1; }
+int NullReader::open(const char *filename, const char *mode) { return -1; }
 int NullReader::close() { return -1; }
 
 int NullReader::getDataset(const char *loc, double **data, int *num, int *dim)             { return -1; }

Modified: cs/cigma/trunk/src/io_null_reader.h
===================================================================
--- cs/cigma/trunk/src/io_null_reader.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_null_reader.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -17,7 +17,7 @@
 
     ReaderType getReaderType() { return NULL_FILE_READER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int getDataset(const char *loc, double **data, int *num, int *dim);

Modified: cs/cigma/trunk/src/io_null_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_null_writer.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_null_writer.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -5,7 +5,7 @@
 NullWriter::NullWriter() {}
 NullWriter::~NullWriter() {}
 
-int NullWriter::open(const char *filename) { return -1; }
+int NullWriter::open(const char *filename, const char *mode) { return -1; }
 int NullWriter::close() { return -1; }
 
 int NullWriter::writeDataset(const char *loc, const double *data, int nno, int ndim)            { return -1; }

Modified: cs/cigma/trunk/src/io_null_writer.h
===================================================================
--- cs/cigma/trunk/src/io_null_writer.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_null_writer.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -16,7 +16,7 @@
 
     WriterType getWriterType() { return NULL_FILE_WRITER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int writeDataset(const char *loc, const double *data, int nno, int ndim);

Modified: cs/cigma/trunk/src/io_text_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_text_reader.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_text_reader.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -22,7 +22,7 @@
 
 // ----------------------------------------------------------------------------
 
-int TextReader::open(const char *filename)
+int TextReader::open(const char *filename, const char *mode)
 {
     TRI_LOG_STR("TextReader::open()");
     TRI_LOG(filename);

Modified: cs/cigma/trunk/src/io_text_reader.h
===================================================================
--- cs/cigma/trunk/src/io_text_reader.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_text_reader.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -18,7 +18,7 @@
 
     ReaderType getReaderType() { return TEXT_FILE_READER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int getDataset(const char *loc, double **data, int *num, int *dim);

Modified: cs/cigma/trunk/src/io_text_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_text_writer.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_text_writer.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -20,7 +20,7 @@
 
 // ----------------------------------------------------------------------------
 
-int TextWriter::open(const char *filename)
+int TextWriter::open(const char *filename, const char *mode)
 {
     TRI_LOG_STR("TextWriter::open()");
     TRI_LOG(filename);

Modified: cs/cigma/trunk/src/io_text_writer.h
===================================================================
--- cs/cigma/trunk/src/io_text_writer.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_text_writer.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -18,7 +18,7 @@
 
     WriterType getWriterType() { return TEXT_FILE_WRITER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int writeConnectivity(const char *loc, const int *connectivity, int nel, int ndofs);

Modified: cs/cigma/trunk/src/io_vtk_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_vtk_writer.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_vtk_writer.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -20,7 +20,7 @@
 
 // ----------------------------------------------------------------------------
 
-int VtkWriter::open(const char *filename)
+int VtkWriter::open(const char *filename, const char *mode)
 {
     fp = fopen(filename, "w");
     if (fp == 0)

Modified: cs/cigma/trunk/src/io_vtk_writer.h
===================================================================
--- cs/cigma/trunk/src/io_vtk_writer.h	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/src/io_vtk_writer.h	2008-12-10 02:14:50 UTC (rev 13574)
@@ -18,7 +18,7 @@
 
     WriterType getWriterType() { return VTK_FILE_WRITER; }
 
-    int open(const char *filename);
+    int open(const char *filename, const char *mode);
     int close();
 
     int writeDataset(const char *loc, double *data, int num, int ndim);

Modified: cs/cigma/trunk/tests/libcigma/ReaderTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/ReaderTest.cpp	2008-12-10 02:14:48 UTC (rev 13573)
+++ cs/cigma/trunk/tests/libcigma/ReaderTest.cpp	2008-12-10 02:14:50 UTC (rev 13574)
@@ -38,7 +38,7 @@
     TextReader reader;
     CPPUNIT_ASSERT_EQUAL(FileReader::TEXT_FILE_READER, reader.getReaderType());
 
-    status = reader.open("tests/data/brick1/connect.dat");
+    status = reader.open("tests/data/brick1/connect.dat", "r");
     CPPUNIT_ASSERT_EQUAL(0, status);
 
     int nel,ndofs;



More information about the CIG-COMMITS mailing list