[cig-commits] r4107 - in cs/spatialdata-0.1/trunk: libsrc/utils tests/libtests/utils tests/libtests/utils/data tests/pytests/utils

baagaard at geodynamics.org baagaard at geodynamics.org
Tue Jul 25 20:30:43 PDT 2006


Author: baagaard
Date: 2006-07-25 20:30:43 -0700 (Tue, 25 Jul 2006)
New Revision: 4107

Modified:
   cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.cc
   cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.hh
   cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.icc
   cs/spatialdata-0.1/trunk/tests/libtests/utils/Makefile.am
   cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.cc
   cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.hh
   cs/spatialdata-0.1/trunk/tests/libtests/utils/data/Makefile.am
   cs/spatialdata-0.1/trunk/tests/pytests/utils/testutils.py
Log:
Switched to specifying input/ouput in PointsStream with name of file.

Modified: cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.cc	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.cc	2006-07-26 03:30:43 UTC (rev 4107)
@@ -13,6 +13,7 @@
 #include "PointsStream.hh" // Implementation of class methods
 
 #include <iostream> // USES std::cout, std::cin
+#include <fstream> // USES std::ifstream, std::ofstream
 #include <iomanip> // USES std::setprecision(), std::width()
 #include <vector> // USES std::vector
 #include <assert.h> // USES assert()
@@ -21,10 +22,9 @@
 // Default constructor
 spatialdata::utils::PointsStream::PointsStream(void) :
   _commentFlag("#"),
+  _filename(""),
   _fieldWidth(14),
-  _precision(5),
-  _pIn(&std::cin),
-  _pOut(&std::cout)
+  _precision(5)
 { // constructor
 } // constructor
 
@@ -35,29 +35,15 @@
 } // destructor
 
 // ----------------------------------------------------------------------
-// Set input stream.
-void
-spatialdata::utils::PointsStream::input(std::istream* pIn)
-{ // input
-  _pIn = pIn;
-} // input
-
-// ----------------------------------------------------------------------
-// Set output stream.
-void
-spatialdata::utils::PointsStream::output(std::ostream* pOut)
-{ // output
-  _pOut = pOut;
-} // output
-
-// ----------------------------------------------------------------------
 // Read points from stdin.
 void
 spatialdata::utils::PointsStream::read(double** ppPoints,
 				       int* pNumPts,
 				       int* pNumDims) const
 { // read
-  assert(0 != _pIn);
+  std::istream* pIn = (0 != _filename.length()) ?
+    new std::ifstream(_filename.c_str()) :
+    &std::cin;
 
   size_t numPts = 0;
   size_t numDims = 3;
@@ -67,14 +53,14 @@
 
   size_t capacity = initialCapacity;
   size_t index = 0;
-  while(!_pIn->eof() && _pIn->good()) {
+  while(!pIn->eof() && pIn->good()) {
     if (numPts*numDims >= capacity) {
       capacity *= 2;
       buffer.resize(capacity);
     } // if
     for (size_t iDim=0; iDim < numDims; ++iDim)
-      *_pIn >> buffer[index++];
-    if (_pIn->good())
+      *pIn >> buffer[index++];
+    if (pIn->good())
       ++numPts;
   } // while
 
@@ -89,6 +75,8 @@
     *pNumPts = numPts;
   if (0 != pNumDims)
     *pNumDims = numDims;
+  if (&std::cin != pIn)
+    delete pIn;
 } // read
 
 // ----------------------------------------------------------------------
@@ -98,19 +86,23 @@
 					const int numPts,
 					const int numDims) const
 { // write
-  assert(0 != _pOut);
+  std::ostream* pOut = (0 != _filename.length()) ?
+    new std::ofstream(_filename.c_str()) :
+    &std::cout;
 
-  *_pOut
+  *pOut
     << std::resetiosflags(std::ios::fixed)
     << std::setiosflags(std::ios::scientific)
     << std::setprecision(_precision);
 
   for (size_t iPoint=0, index=0; iPoint < numPts; ++iPoint) {
     for (size_t iDim=0; iDim < numDims; ++iDim)
-      *_pOut << std::setw(_fieldWidth) << pPoints[index++];
-    *_pOut << "\n";
+      *pOut << std::setw(_fieldWidth) << pPoints[index++];
+    *pOut << "\n";
   } // for
 
+  if (&std::cout != pOut)
+    delete pOut;
 } // write
 
 // End of file

Modified: cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.hh
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.hh	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.hh	2006-07-26 03:30:43 UTC (rev 4107)
@@ -28,7 +28,6 @@
 } // spatialdata
 
 #include <string> // HASA std::string
-#include <iosfwd> // USES std::istream, std::ostream
 
 /// C++ object for reading/writing points to/from stdin/stdout.
 class spatialdata::utils::PointsStream
@@ -80,17 +79,21 @@
    */
   int precision(void) const;
 
-  /** Set input stream.
+  /** Set name of file.
    *
-   * @param pIn Pointer to input stream
+   * If no filename is supplied, stdin/stdout is used.
+   *
+   * @param filename Name of file
    */
-  void input(std::istream* pIn);
+  void filename(const char* filename);
 
-  /** Set output stream.
+  /** Get name of fle.
    *
-   * @param pOut Pointer to output stream
+   * If no filename is supplied, stdin/stdout is used.
+   *
+   * @returns Name of file
    */
-  void output(std::ostream* pOut);
+  const char* filename(void) const;
 
   /** Read points from stdin.
    *
@@ -121,13 +124,11 @@
 private :
  // PRIVATE MEMBERS ////////////////////////////////////////////////////
 
+  std::string _filename; ///< Name of file for input/output
   std::string _commentFlag; ///< String identifying comments in input
   int _fieldWidth; ///< Width of field in output
   int _precision; ///< Precision in floating point output
 
-  std::istream* _pIn; ///< Pointer to input stream
-  std::ostream* _pOut; ///< Pointer to output stream
-
 }; // class PointsStream
 
 #include "PointsStream.icc" // inline methods

Modified: cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.icc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.icc	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/libsrc/utils/PointsStream.icc	2006-07-26 03:30:43 UTC (rev 4107)
@@ -14,6 +14,18 @@
 #error "PointsSTream.icc must only be included from PointsStream.hh"
 #else
 
+// Set name of file for input/output.
+void
+spatialdata::utils::PointsStream::filename(const char* filename) {
+  _filename = filename;
+}
+
+// Get name of file for input/output.
+const char*
+spatialdata::utils::PointsStream::filename(void) const {
+  return _filename.c_str();
+}
+
 // Set comment flag.
 void
 spatialdata::utils::PointsStream::commentFlag(const char* flag) {

Modified: cs/spatialdata-0.1/trunk/tests/libtests/utils/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/utils/Makefile.am	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/tests/libtests/utils/Makefile.am	2006-07-26 03:30:43 UTC (rev 4107)
@@ -28,8 +28,6 @@
 
 testutils_LDFLAGS =
 
-INCLUDES +=
-
 testutils_LDADD = \
 	-lcppunit -ldl \
 	$(top_builddir)/libsrc/utils/libspatialdatautils.la

Modified: cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.cc	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.cc	2006-07-26 03:30:43 UTC (rev 4107)
@@ -58,20 +58,16 @@
 } // testPrecision
 
 // ----------------------------------------------------------------------
-// Test read()
+// Test write() and read()
 void
-spatialdata::utils::TestPointsStream::testRead(void)
-{ // testRead
-  std::stringstream buffer;
+spatialdata::utils::TestPointsStream::testWriteRead(void)
+{ // testWriteRead
+  const char* filename = "data/tmp.txt";
 
-  //buffer << "# This is a comment\n";
-  const int size = _NUMPTS * _NUMDIMS;
-  for (int i=0; i < size; ++i)
-    buffer << "  " << _POINTS[i];
-  buffer << "\n";
-
   PointsStream s;
-  s.input(&buffer);
+  s.filename(filename);
+  s.write(_POINTS, _NUMPTS, _NUMDIMS);
+
   double* points = 0;
   int numPts = 0;
   int numDims = 0;
@@ -79,27 +75,10 @@
 
   CPPUNIT_ASSERT_EQUAL(_NUMPTS, numPts);
   CPPUNIT_ASSERT_EQUAL(_NUMDIMS, numDims);
+
+  const int size = _NUMPTS * _NUMDIMS;
   for (int i=0; i < size; ++i)
     CPPUNIT_ASSERT_EQUAL(_POINTS[i], points[i]);
-} // testRead
+} // testWriteRead
 
-// ----------------------------------------------------------------------
-// Test write()
-void
-spatialdata::utils::TestPointsStream::testWrite(void)
-{ // testWrite
-  std::stringstream buffer;
-
-  PointsStream s;
-  s.output(&buffer);
-  s.write(_POINTS, _NUMPTS, _NUMDIMS);
-
-  const int size = _NUMPTS * _NUMDIMS;
-  for (int i=0; i < size; ++i) {
-    double value;
-    buffer >> value;
-    CPPUNIT_ASSERT_EQUAL(_POINTS[i], value);
-  } // for
-} // testWrite
-
 // End of file 

Modified: cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.hh
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.hh	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/tests/libtests/utils/TestPointsStream.hh	2006-07-26 03:30:43 UTC (rev 4107)
@@ -38,8 +38,7 @@
   CPPUNIT_TEST( testCommentFlag );
   CPPUNIT_TEST( testFieldWidth );
   CPPUNIT_TEST( testPrecision );
-  CPPUNIT_TEST( testRead );
-  CPPUNIT_TEST( testWrite );
+  CPPUNIT_TEST( testWriteRead );
   CPPUNIT_TEST_SUITE_END();
 
   // PUBLIC METHODS /////////////////////////////////////////////////////
@@ -54,12 +53,9 @@
   /// Test precision()
   void testPrecision(void);
 
-  /// Test read()
-  void testRead(void);
+  /// Test write()/read()
+  void testWriteRead(void);
 
-  /// Test write()
-  void testWrite(void);
-
   // PRIVATE METHODS ////////////////////////////////////////////////////
 private :
 

Modified: cs/spatialdata-0.1/trunk/tests/libtests/utils/data/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/utils/data/Makefile.am	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/tests/libtests/utils/data/Makefile.am	2006-07-26 03:30:43 UTC (rev 4107)
@@ -16,4 +16,6 @@
 # 'export' the input files by performing a mock install
 export_datadir = $(top_builddir)/tests/libtests/utils/data
 
+CLEANFILES = tmp.txt
+
 # End of file 

Modified: cs/spatialdata-0.1/trunk/tests/pytests/utils/testutils.py
===================================================================
--- cs/spatialdata-0.1/trunk/tests/pytests/utils/testutils.py	2006-07-26 00:39:45 UTC (rev 4106)
+++ cs/spatialdata-0.1/trunk/tests/pytests/utils/testutils.py	2006-07-26 03:30:43 UTC (rev 4107)
@@ -19,6 +19,9 @@
   from TestSimpleArray import TestSimpleArray
   suite.addTest(unittest.makeSuite(TestSimpleArray))
 
+  from TestPointsStream import TestPointsStream
+  suite.addTest(unittest.makeSuite(TestPointsStream))
+
   return suite
 
 def main():



More information about the cig-commits mailing list