[Liblas-commits] hg-main-tree: split some code out of Utils.cpp into a new FileUt...

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Aug 2 13:31:55 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/9bb7365e9274
changeset: 977:9bb7365e9274
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Aug 02 09:55:01 2011 -0700
description:
split some code out of Utils.cpp into a new FileUtils.cpp
Subject: hg-main-tree: added FileUtils::getcwd, ::toAbsolutePath

details:   http://hg.libpc.orghg-main-tree/rev/04ee96a6899c
changeset: 978:04ee96a6899c
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Aug 02 10:31:31 2011 -0700
description:
added FileUtils::getcwd, ::toAbsolutePath

diffstat:

 apps/pc2pc.cpp                      |    7 +-
 apps/pcinfo.cpp                     |    4 +-
 apps/pcpipeline.cpp                 |    3 +-
 include/pdal/FileUtils.hpp          |   89 +++++++++++++++++++
 include/pdal/Utils.hpp              |   14 ---
 include/pdal/XMLSchema.hpp          |    1 -
 src/CMakeLists.txt                  |    2 +
 src/Dimension.cpp                   |    1 -
 src/FileUtils.cpp                   |  165 ++++++++++++++++++++++++++++++++++++
 src/StreamManager.cpp               |   10 +-
 src/Utils.cpp                       |  104 ----------------------
 src/XMLSchema.cpp                   |    1 -
 src/drivers/las/Iterator.cpp        |    6 +-
 src/drivers/las/LasHeaderReader.cpp |    1 -
 src/drivers/las/Reader.cpp          |    5 +-
 src/drivers/liblas/Iterator.cpp     |    6 +-
 src/drivers/liblas/Reader.cpp       |    5 +-
 src/drivers/oci/Iterator.cpp        |    2 -
 src/drivers/oci/Reader.cpp          |    4 +-
 src/drivers/oci/Writer.cpp          |   13 +-
 src/drivers/qfit/Iterator.cpp       |   10 +-
 src/drivers/qfit/Reader.cpp         |    4 +-
 src/drivers/terrasolid/Iterator.cpp |   10 +-
 src/drivers/terrasolid/Reader.cpp   |    4 +-
 test/unit/CMakeLists.txt            |    1 +
 test/unit/FileUtilsTest.cpp         |  130 ++++++++++++++++++++++++++++
 test/unit/LasWriterTest.cpp         |   25 ++--
 test/unit/LiblasWriterTest.cpp      |   29 +++---
 test/unit/OCITest.cpp               |    1 -
 test/unit/PipelineManagerTest.cpp   |    6 +-
 test/unit/PipelineReaderTest.cpp    |    6 +-
 test/unit/PipelineWriterTest.cpp    |    5 +-
 test/unit/PointBufferTest.cpp       |    1 -
 test/unit/SpatialReferenceTest.cpp  |   22 ++--
 test/unit/StageFactoryTest.cpp      |    9 +-
 test/unit/StreamManagerTest.cpp     |   26 ++--
 test/unit/Support.cpp               |   30 +++---
 test/unit/UtilsTest.cpp             |   36 -------
 test/unit/XMLSchemaTest.cpp         |    4 +-
 39 files changed, 518 insertions(+), 284 deletions(-)

diffs (truncated from 1811 to 300 lines):

diff -r 0893210f7f19 -r 04ee96a6899c apps/pc2pc.cpp
--- a/apps/pc2pc.cpp	Mon Aug 01 16:54:55 2011 -0700
+++ b/apps/pc2pc.cpp	Tue Aug 02 10:31:31 2011 -0700
@@ -13,6 +13,7 @@
 #include <iostream>
 
 #include <pdal/exceptions.hpp>
+#include <pdal/FileUtils.hpp>
 //#include <pdal/pdal_config.hpp>
 //#include <pdal/Bounds.hpp>
 //#include <pdal/Color.hpp>
@@ -112,13 +113,13 @@
 
 int Application_pc2pc::execute()
 {
-    if (!Utils::fileExists(m_inputFile))
+    if (!FileUtils::fileExists(m_inputFile))
     {
         runtimeError("file not found: " + m_inputFile);
         return 1;
     }
 
-    std::ostream* ofs = Utils::createFile(m_outputFile);
+    std::ostream* ofs = FileUtils::createFile(m_outputFile);
 
     if (hasOption("native"))
     {
@@ -312,7 +313,7 @@
         writer.write(numPoints);
     }
 
-    Utils::closeFile(ofs);
+    FileUtils::closeFile(ofs);
 
     return 0;
 }
diff -r 0893210f7f19 -r 04ee96a6899c apps/pcinfo.cpp
--- a/apps/pcinfo.cpp	Mon Aug 01 16:54:55 2011 -0700
+++ b/apps/pcinfo.cpp	Tue Aug 02 10:31:31 2011 -0700
@@ -12,7 +12,7 @@
 
 #include <pdal/drivers/las/Reader.hpp>
 #include <pdal/drivers/liblas/Reader.hpp>
-#include <pdal/Utils.hpp>
+#include <pdal/FileUtils.hpp>
 #ifdef PDAL_HAVE_MRSID
 #include <pdal/drivers/mrsid/Reader.hpp>
 #endif
@@ -74,7 +74,7 @@
 
 int Application_pcinfo::execute()
 {
-    if (!Utils::fileExists(m_inputFile))
+    if (!FileUtils::fileExists(m_inputFile))
     {
         runtimeError("file not found: " + m_inputFile);
         return 1;
diff -r 0893210f7f19 -r 04ee96a6899c apps/pcpipeline.cpp
--- a/apps/pcpipeline.cpp	Mon Aug 01 16:54:55 2011 -0700
+++ b/apps/pcpipeline.cpp	Tue Aug 02 10:31:31 2011 -0700
@@ -36,6 +36,7 @@
 
 #include <pdal/PipelineReader.hpp>
 #include <pdal/PipelineManager.hpp>
+#include <pdal/FileUtils.hpp>
 
 #include "Application.hpp"
 
@@ -91,7 +92,7 @@
 
 int Application_pcpipeline::execute()
 {
-    if (!Utils::fileExists(m_inputFile))
+    if (!FileUtils::fileExists(m_inputFile))
     {
         runtimeError("file not found: " + m_inputFile);
         return 1;
diff -r 0893210f7f19 -r 04ee96a6899c include/pdal/FileUtils.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/pdal/FileUtils.hpp	Tue Aug 02 10:31:31 2011 -0700
@@ -0,0 +1,89 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#ifndef INCLUDED_FILEUTILS_HPP
+#define INCLUDED_FILEUTILS_HPP
+
+#include <pdal/pdal.hpp>
+
+#include <string>
+#include <cassert>
+#include <stdexcept>
+#include <cmath>
+#include <ostream>
+#include <istream>
+
+namespace pdal
+{
+
+// this is a static class -- do not instantiate]
+class PDAL_DLL FileUtils
+{
+public:
+    // open existing file for reading
+    static std::istream* openFile(std::string const& filename, bool asBinary=true);
+
+    // open new file for writing
+    static std::ostream* createFile(std::string const& filename, bool asBinary=true);
+
+    static void closeFile(std::ostream* ofs);
+    static void closeFile(std::istream* ifs);
+
+    static bool deleteFile(const std::string& filename);
+    static void renameFile(const std::string& dest, const std::string& src);
+    static bool fileExists(const std::string& filename);
+    static boost::uintmax_t fileSize(const std::string& filename);
+
+    // return current working dir
+    static std::string getcwd();
+
+    // if the filename is an absolute path, just return it
+    // otherwise, make it absolute (relative to current working dir) and return that
+    static std::string toAbsolutePath(const std::string& filename);
+
+    // if the filename is an absolute path, just return it
+    // otherwise, make it absolute (relative to base dir) and return that
+    // 
+    // note: if base dir is not absolute, first make it absolute via toAbsolutePath(base)
+    static std::string toAbsolutePath(const std::string& filename, const std::string base);
+
+private:
+    FileUtils& operator=(const FileUtils&); // not implemented
+    FileUtils(const FileUtils&); // not implemented;
+};
+
+
+} // namespace pdal
+
+#endif
diff -r 0893210f7f19 -r 04ee96a6899c include/pdal/Utils.hpp
--- a/include/pdal/Utils.hpp	Mon Aug 01 16:54:55 2011 -0700
+++ b/include/pdal/Utils.hpp	Tue Aug 02 10:31:31 2011 -0700
@@ -76,20 +76,6 @@
         return true;
     }
 
-    // open existing file for reading
-    static std::istream* openFile(std::string const& filename, bool asBinary=true);
-
-    // open new file for writing
-    static std::ostream* createFile(std::string const& filename, bool asBinary=true);
-
-    static void closeFile(std::ostream* ofs);
-    static void closeFile(std::istream* ifs);
-
-    static bool deleteFile(const std::string& filename);
-    static void renameFile(const std::string& dest, const std::string& src);
-    static bool fileExists(const std::string& filename);
-    static boost::uintmax_t fileSize(const std::string& filename);
-
     template<class T>
     static inline void write_field(boost::uint8_t*& dest, T v)
     {
diff -r 0893210f7f19 -r 04ee96a6899c include/pdal/XMLSchema.hpp
--- a/include/pdal/XMLSchema.hpp	Mon Aug 01 16:54:55 2011 -0700
+++ b/include/pdal/XMLSchema.hpp	Tue Aug 02 10:31:31 2011 -0700
@@ -37,7 +37,6 @@
 
 #include <pdal/pdal.hpp>
 #include <pdal/Schema.hpp>
-#include <pdal/Utils.hpp>
 #include <pdal/SchemaLayout.hpp>
 #include <pdal/Dimension.hpp>
 #include <pdal/DimensionLayout.hpp>
diff -r 0893210f7f19 -r 04ee96a6899c src/CMakeLists.txt
--- a/src/CMakeLists.txt	Mon Aug 01 16:54:55 2011 -0700
+++ b/src/CMakeLists.txt	Tue Aug 02 10:31:31 2011 -0700
@@ -29,6 +29,7 @@
   ${PDAL_HEADERS_DIR}/Color.hpp
   ${PDAL_HEADERS_DIR}/Dimension.hpp
   ${PDAL_HEADERS_DIR}/DimensionLayout.hpp
+  ${PDAL_HEADERS_DIR}/FileUtils.hpp
   ${PDAL_HEADERS_DIR}/Filter.hpp
   ${PDAL_HEADERS_DIR}/FilterIterator.hpp
   ${PDAL_HEADERS_DIR}/MetadataRecord.hpp
@@ -67,6 +68,7 @@
   Color.cpp
   Dimension.cpp
   DimensionLayout.cpp
+  FileUtils.cpp
   Filter.cpp
   FilterIterator.cpp
   MetadataRecord.cpp
diff -r 0893210f7f19 -r 04ee96a6899c src/Dimension.cpp
--- a/src/Dimension.cpp	Mon Aug 01 16:54:55 2011 -0700
+++ b/src/Dimension.cpp	Tue Aug 02 10:31:31 2011 -0700
@@ -42,7 +42,6 @@
 #include <pdal/Dimension.hpp>
 
 #include <pdal/exceptions.hpp>
-#include <pdal/Utils.hpp>
 
 namespace pdal
 {
diff -r 0893210f7f19 -r 04ee96a6899c src/FileUtils.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/FileUtils.cpp	Tue Aug 02 10:31:31 2011 -0700
@@ -0,0 +1,165 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#include <pdal/FileUtils.hpp>
+
+#include <boost/iostreams/device/file.hpp>
+#include <boost/iostreams/stream.hpp>
+#include <boost/filesystem.hpp>
+//
+#include <pdal/exceptions.hpp>
+
+namespace pdal
+{
+
+
+std::istream* FileUtils::openFile(std::string const& filename, bool asBinary)
+{
+    if (!FileUtils::fileExists(filename))
+        throw pdal_error("File not found: " + filename);
+
+    std::ios::openmode mode = std::ios::in;
+    if (asBinary)
+      mode |= std::ios::binary;
+
+    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;
+}


More information about the Liblas-commits mailing list