[Liblas-commits] hg: remove ReaderFactory::FileOpen and WriterFactory::Create sta...

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Jan 18 11:52:55 EST 2011


details:   http://hg.liblas.orghg/rev/81357682c6b6
changeset: 2773:81357682c6b6
user:      Howard Butler <hobu.inc at gmail.com>
date:      Tue Jan 18 10:52:24 2011 -0600
description:
remove ReaderFactory::FileOpen and WriterFactory::Create static methods and instead move them up to liblas::Open and liblas::Create so users don't have to include private headers to use them
Subject: hg: take advantage of new Create and Open methods

details:   http://hg.liblas.orghg/rev/db7de8b00d68
changeset: 2774:db7de8b00d68
user:      Howard Butler <hobu.inc at gmail.com>
date:      Tue Jan 18 10:52:44 2011 -0600
description:
take advantage of new Create and Open methods

diffstat:

 apps/las2las.cpp           |  11 +++++--
 apps/lasblock.cpp          |  53 ++++++++++++-------------------------
 include/liblas/factory.hpp |  10 ++-----
 include/liblas/liblas.hpp  |  64 ++++++++++++++++++++++++++++++++++++++++++++++
 src/c_api.cpp              |  53 +++++++++++++++++--------------------
 src/factory.cpp            |  57 ----------------------------------------
 6 files changed, 115 insertions(+), 133 deletions(-)

diffs (truncated from 439 to 300 lines):

diff -r 44a3ac08553c -r db7de8b00d68 apps/las2las.cpp
--- a/apps/las2las.cpp	Mon Jan 17 16:07:53 2011 -0600
+++ b/apps/las2las.cpp	Tue Jan 18 10:52:44 2011 -0600
@@ -29,7 +29,7 @@
                       std::string const& output, 
                       liblas::Header const& header)
 {
-ofs = WriterFactory::FileCreate(output);
+ofs = liblas::Create(output, std::ios::out | std::ios::binary);
 if (!ofs)
 {
     std::ostringstream oss;
@@ -291,7 +291,7 @@
             if (verbose)
                 std::cout << "Opening " << input << " to fetch Header" << std::endl;
 
-            std::istream* ifs = ReaderFactory::FileOpen(input);
+            std::istream* ifs = Open(input, std::ios::in | std::ios::binary);
             if (!ifs)
             {
                 std::cerr << "Cannot open " << input << " for read.  Exiting..." << std::endl;
@@ -334,7 +334,7 @@
             SetHeaderCompression(header, output);
         }
 
-        std::istream* ifs = ReaderFactory::FileOpen(input);
+        std::istream* ifs = Open(input, std::ios::in | std::ios::binary);
         if (!ifs)
         {
             std::cerr << "Cannot open " << input << " for read.  Exiting..." << std::endl;
@@ -355,7 +355,10 @@
             return (1);
         }
         
-        delete ifs;
+        if (ifs != 0)
+        {
+            liblas::Cleanup(ifs);
+        }
     }
     catch(std::exception& e)
     {
diff -r 44a3ac08553c -r db7de8b00d68 apps/lasblock.cpp
--- a/apps/lasblock.cpp	Mon Jan 17 16:07:53 2011 -0600
+++ b/apps/lasblock.cpp	Tue Jan 18 10:52:44 2011 -0600
@@ -46,37 +46,17 @@
                       std::string const& output, 
                       liblas::Header const& header)
 {
-ofs = liblas::WriterFactory::FileCreate(output);
-if (!ofs)
-{
-    std::ostringstream oss;
-    oss << "Cannot create " << output << "for write.  Exiting...";
-    throw std::runtime_error(oss.str());
+    ofs = liblas::Create(output, std::ios::out | std::ios::binary);
+    if (!ofs)
+    {
+        std::ostringstream oss;
+        oss << "Cannot create " << output << "for write.  Exiting...";
+        throw std::runtime_error(oss.str());
+    }
+
+    return new liblas::Writer(*ofs, header);
 }
 
-return new liblas::Writer(*ofs, header);
-}
-
-
-
-// WriterPtr start_writer(   OStreamPtr strm, 
-//                           std::string const& output, 
-//                           liblas::Header const& header)
-// {
-//     
-//     if (!liblas::Create(*strm, output.c_str()))
-//     {
-//         std::ostringstream oss;
-//         oss << "Cannot create " << output << "for write.  Exiting...";
-//         throw std::runtime_error(oss.str());
-//     }
-// 
-//     WriterPtr writer( new liblas::Writer(*strm, header));
-//     return writer;
-//     
-// }
-
-
 using namespace liblas;
 
 void OutputHelp( std::ostream & oss, po::options_description const& options)
@@ -104,10 +84,6 @@
 
 
     std::string out = output;
-    
-
-
-    
     liblas::Header header = reader.GetHeader();
     
     if (bCompressed)
@@ -157,7 +133,9 @@
             if (writer != 0)
                 delete writer;
             if (ofs != 0)
-                delete ofs;
+            {
+                liblas::Cleanup(ofs);
+            }
             
         }
         
@@ -295,7 +273,7 @@
             output = std::string(input) + ".kdx";
         }
 
-        std::istream* ifs = liblas::ReaderFactory::FileOpen(input);
+        std::istream* ifs = liblas::Open(input, std::ios::in | std::ios::binary);
         if (!ifs)
         {
             std::cerr << "Cannot open " << input << " for read.  Exiting..." << std::endl;
@@ -327,7 +305,10 @@
         }
         
         if (ifs != 0)
-            delete ifs;
+        {
+            liblas::Cleanup(ifs);
+            ifs = 0;
+        }
     }
 
     catch(std::exception& e)
diff -r 44a3ac08553c -r db7de8b00d68 include/liblas/factory.hpp
--- a/include/liblas/factory.hpp	Mon Jan 17 16:07:53 2011 -0600
+++ b/include/liblas/factory.hpp	Tue Jan 18 10:52:44 2011 -0600
@@ -71,8 +71,8 @@
     
     // help function to create an input stream
     // returns NULL if failed to open
-    static std::istream* FileOpen(std::string const& filename);
-    static void FileClose(std::istream*);
+    // static std::istream* FileOpen(std::string const& filename, std::ios::openmode mode);
+    // static void FileClose(std::istream*);
 
     /// Destructor.
     /// @exception nothrow
@@ -110,11 +110,7 @@
     // returns Unknown, unless we find a .laz or .las extension
     static FileType InferFileTypeFromExtension(const std::string&);
 
-    // help function to create an output stream
-    // returns NULL if failed to open
-    static std::ostream* FileCreate(std::string const& filename);
-    static void FileClose(std::ostream*);
-
+// 
 private:
 
 };
diff -r 44a3ac08553c -r db7de8b00d68 include/liblas/liblas.hpp
--- a/include/liblas/liblas.hpp	Mon Jan 17 16:07:53 2011 -0600
+++ b/include/liblas/liblas.hpp	Tue Jan 18 10:52:44 2011 -0600
@@ -75,6 +75,16 @@
 #include <boost/concept_check.hpp>
 #include <boost/cstdint.hpp>
 #include <boost/shared_ptr.hpp>
+
+//#define USE_BOOST_IO
+#ifdef USE_BOOST_IO
+#include <ostream>
+#include <boost/iostreams/device/file.hpp>
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/stream_buffer.hpp>
+#endif
+
+
 // std
 #include <cstring>
 #include <fstream>
@@ -105,6 +115,22 @@
     return ifs.is_open();
 }
 
+inline std::istream* Open(std::string const& filename, std::ios::openmode mode) // throw()
+{
+#ifdef USE_BOOST_IO
+    namespace io = boost::iostreams;
+    io::stream<io::file_source>* ifs = new io::stream<io::file_source>();
+    ifs->open(filename.c_str(), mode);
+    if (ifs->is_open() == false) return NULL;
+    return ifs;
+#else
+    std::ifstream* ifs = new std::ifstream();
+    ifs->open(filename.c_str(), mode);
+    if (ifs->is_open() == false) return NULL;
+    return ifs;
+#endif
+}
+
 /// Create file and open to write in binary mode.
 /// The output file is also attached to output stream.
 /// \param ofs - reference to output file stream to
@@ -119,6 +145,44 @@
     return ofs.is_open();
 }
 
+inline std::ostream* Create(std::string const& filename, std::ios::openmode mode)
+{
+#ifdef USE_BOOST_IO
+    namespace io = boost::iostreams;
+    io::stream<io::file_sink>* ofs = new io::stream<io::file_sink>();
+    ofs->open(filename.c_str(), mode);
+    if (ofs->is_open() == false) return NULL;
+    return ofs;
+#else
+    std::ofstream* ofs = new std::ofstream();
+    ofs->open(filename.c_str(), mode);
+    if (ofs->is_open() == false) return NULL;
+    return ofs;
+#endif    
+}
+
+inline void Cleanup(std::ostream* ofs)
+{
+    // An ofstream is closeable and deletable, but 
+    // an ostream like &std::cout isn't.
+    if (static_cast<std::ofstream&>(*ofs))
+    {
+        static_cast<std::ofstream&>(*ofs).close();
+        delete ofs;
+    }
+}
+
+inline void Cleanup(std::istream* ifs)
+{
+    // An ifstream is closeable and deletable, but 
+    // an istream like &std::cin isn't.
+    if (static_cast<std::ifstream&>(*ifs))
+    {
+        static_cast<std::ifstream&>(*ifs).close();
+        delete ifs;
+    }
+}
+
 class ReaderI
 {
 public:
diff -r 44a3ac08553c -r db7de8b00d68 src/c_api.cpp
--- a/src/c_api.cpp	Mon Jan 17 16:07:53 2011 -0600
+++ b/src/c_api.cpp	Tue Jan 18 10:52:44 2011 -0600
@@ -65,6 +65,7 @@
 typedef struct LASColorHS *LASColorH;
 typedef struct LASSRSHS *LASSRSH;
 typedef struct LASSchemaHS *LASSchemaH;
+typedef struct LASFilterHS *LASFilterH;
 
 // boost
 #include <boost/cstdint.hpp>
@@ -89,27 +90,6 @@
 #define compare_no_case(a,b,n)  strncasecmp( (a), (b), (n) )
 #endif
 
-std::istream* OpenInput(std::string filename) 
-{
-    std::ios::openmode const mode = std::ios::in | std::ios::binary;
-    std::istream* istrm = 0;
-    if (compare_no_case(filename.c_str(),"STDIN",5) == 0)
-    {
-        istrm = &std::cin;
-    }
-    else 
-    {
-        istrm = new std::ifstream(filename.c_str(), mode);
-    }
-    
-    if (!istrm->good())
-    {
-        delete istrm;
-        throw std::runtime_error("Reading stream was not able to be created");
-    }
-    return istrm;
-}
-
 
 
 LAS_C_START
@@ -120,8 +100,6 @@
 
 
 // Error stuff
-
-
 typedef enum
 {
     LE_None = 0,
@@ -131,8 +109,28 @@
     LE_Fatal = 4


More information about the Liblas-commits mailing list