[Liblas-commits] hg-main-tree: added isAbs(), getDir()

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Aug 2 14:23:13 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/7631e208ea3a
changeset: 979:7631e208ea3a
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Aug 02 11:15:53 2011 -0700
description:
added isAbs(), getDir()
Subject: hg-main-tree: added relative-path fixups

details:   http://hg.libpc.orghg-main-tree/rev/5a54d7b91c91
changeset: 980:5a54d7b91c91
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Aug 02 11:16:21 2011 -0700
description:
added relative-path fixups
Subject: hg-main-tree: disable file comparison, as no longer workable

details:   http://hg.libpc.orghg-main-tree/rev/3082b49b8975
changeset: 981:3082b49b8975
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Aug 02 11:23:05 2011 -0700
description:
disable file comparison, as no longer workable

diffstat:

 include/pdal/FileUtils.hpp       |   6 +++++
 include/pdal/PipelineReader.hpp  |   1 +
 src/FileUtils.cpp                |  18 +++++++++++++++-
 src/PipelineReader.cpp           |  28 +++++++++++++++++++++++++++
 test/data/pipeline_read.xml      |   2 +-
 test/data/pipeline_write.xml     |   2 +-
 test/unit/FileUtilsTest.cpp      |  41 ++++++++++++++++++++++++++++++++++-----
 test/unit/PipelineWriterTest.cpp |   8 +++---
 8 files changed, 92 insertions(+), 14 deletions(-)

diffs (240 lines):

diff -r 04ee96a6899c -r 3082b49b8975 include/pdal/FileUtils.hpp
--- a/include/pdal/FileUtils.hpp	Tue Aug 02 10:31:31 2011 -0700
+++ b/include/pdal/FileUtils.hpp	Tue Aug 02 11:23:05 2011 -0700
@@ -68,6 +68,12 @@
     // return current working dir
     static std::string getcwd();
 
+    // return the directory component of the given path, e.g. "d:/foo/bar/a.c" -> "d:/foo/bar"
+    static std::string getDirectory(const std::string& path);
+
+    // returns true iff the path is not relative
+    static bool isAbsolutePath(const std::string& path);
+
     // 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);
diff -r 04ee96a6899c -r 3082b49b8975 include/pdal/PipelineReader.hpp
--- a/include/pdal/PipelineReader.hpp	Tue Aug 02 10:31:31 2011 -0700
+++ b/include/pdal/PipelineReader.hpp	Tue Aug 02 11:23:05 2011 -0700
@@ -85,6 +85,7 @@
     bool m_isDebug;
     boost::uint8_t m_verboseLevel;
     Options m_baseOptions;
+    std::string m_inputXmlFile;
 
     PipelineReader& operator=(const PipelineReader&); // not implemented
     PipelineReader(const PipelineReader&); // not implemented
diff -r 04ee96a6899c -r 3082b49b8975 src/FileUtils.cpp
--- a/src/FileUtils.cpp	Tue Aug 02 10:31:31 2011 -0700
+++ b/src/FileUtils.cpp	Tue Aug 02 11:23:05 2011 -0700
@@ -156,10 +156,24 @@
 // note: if base dir is not absolute, first make it absolute via toAbsolutePath(base)
 std::string FileUtils::toAbsolutePath(const std::string& filename, const std::string base)
 {
-    std::string newbase = toAbsolutePath(base);
-    boost::filesystem::path p = boost::filesystem::absolute(filename, newbase);
+    const std::string newbase = toAbsolutePath(base);
+    const boost::filesystem::path p = boost::filesystem::absolute(filename, newbase);
     return p.generic_string();
 }
 
 
+std::string FileUtils::getDirectory(const std::string& path)
+{
+    const boost::filesystem::path dir = boost::filesystem::path(path).parent_path();
+    const std::string str = dir.generic_string();
+    return str;
+}
+
+
+bool FileUtils::isAbsolutePath(const std::string& path)
+{
+    return boost::filesystem::path(path).is_absolute();
+}
+
+
 } // namespace pdal
diff -r 04ee96a6899c -r 3082b49b8975 src/PipelineReader.cpp
--- a/src/PipelineReader.cpp	Tue Aug 02 10:31:31 2011 -0700
+++ b/src/PipelineReader.cpp	Tue Aug 02 11:23:05 2011 -0700
@@ -40,6 +40,7 @@
 #include <pdal/Reader.hpp>
 #include <pdal/Writer.hpp>
 #include <pdal/Options.hpp>
+#include <pdal/FileUtils.hpp>
 
 #include <pdal/drivers/las/Reader.hpp>
 #include <pdal/drivers/las/Writer.hpp>
@@ -48,6 +49,7 @@
 
 #include <boost/property_tree/xml_parser.hpp>
 #include <boost/optional.hpp>
+#include <boost/filesystem.hpp>
 
 namespace pdal
 {
@@ -158,6 +160,24 @@
 
     Option<std::string> option(tree);
 
+    // filenames in the XML are fixed up as follows:
+    //   - if absolute path, leave it alone
+    //   - if relative path, make it absolute using the XML file's directory
+    // The toAbsolutePath function does exactly that magic for us.
+    if (option.getName() == "filename")
+    {
+        const std::string oldpath = option.getValue();
+        if (!FileUtils::isAbsolutePath(oldpath))
+        {
+            const std::string abspath = FileUtils::toAbsolutePath(m_inputXmlFile);
+            const std::string absdir = FileUtils::getDirectory(abspath);
+            const std::string newpath = FileUtils::toAbsolutePath(oldpath, absdir);
+            assert(FileUtils::isAbsolutePath(newpath));
+            const Option<std::string> option2(option.getName(), newpath, option.getDescription());
+            option = option2;
+        }
+    }
+
     return option;
 }
 
@@ -426,6 +446,8 @@
 
 void PipelineReader::readWriterPipeline(const std::string& filename)
 {
+    m_inputXmlFile = filename;
+
     boost::property_tree::ptree tree;
     boost::property_tree::xml_parser::read_xml(filename, tree);
 
@@ -439,12 +461,16 @@
 
     (void)parseElement_WriterPipeline(subtree);
 
+    m_inputXmlFile = "";
+
     return;
 }
 
 
 void PipelineReader::readReaderPipeline(const std::string& filename)
 {
+    m_inputXmlFile = filename;
+
     boost::property_tree::ptree tree;
     boost::property_tree::xml_parser::read_xml(filename, tree);
 
@@ -458,6 +484,8 @@
 
     (void)parseElement_ReaderPipeline(subtree);
 
+    m_inputXmlFile = "";
+
     return;
 }
 
diff -r 04ee96a6899c -r 3082b49b8975 test/data/pipeline_read.xml
--- a/test/data/pipeline_read.xml	Tue Aug 02 10:31:31 2011 -0700
+++ b/test/data/pipeline_read.xml	Tue Aug 02 11:23:05 2011 -0700
@@ -10,7 +10,7 @@
                 <Type>drivers.las.reader</Type>
                 <Option>
                     <Name>filename</Name>
-                    <Value>../../test/data/1.2-with-color.las</Value>
+                    <Value>1.2-with-color.las</Value>
                 </Option>
             </Reader>
         </Filter>
diff -r 04ee96a6899c -r 3082b49b8975 test/data/pipeline_write.xml
--- a/test/data/pipeline_write.xml	Tue Aug 02 10:31:31 2011 -0700
+++ b/test/data/pipeline_write.xml	Tue Aug 02 11:23:05 2011 -0700
@@ -17,7 +17,7 @@
                 <Type>drivers.las.reader</Type>
                 <Option>
                     <Name>filename</Name>
-                    <Value>../../test/data/1.2-with-color.las</Value>
+                    <Value>1.2-with-color.las</Value>
                 </Option>
             </Reader>
         </Filter>
diff -r 04ee96a6899c -r 3082b49b8975 test/unit/FileUtilsTest.cpp
--- a/test/unit/FileUtilsTest.cpp	Tue Aug 02 10:31:31 2011 -0700
+++ b/test/unit/FileUtilsTest.cpp	Tue Aug 02 11:23:05 2011 -0700
@@ -91,18 +91,19 @@
 }
 
 
+#ifdef PDAL_COMPILER_MSVC
+static const std::string drive = "A:";
+#else
+static const std::string drive = "";
+#endif
+
+
 BOOST_AUTO_TEST_CASE(test_toAbsolutePath)
 {
     using namespace std;
 
     const string root = FileUtils::getcwd();
 
-#ifdef PDAL_COMPILER_MSVC
-    const string drive = "A:";
-#else
-    const string drive = "";
-#endif
-
     // check 1-arg version: make absolute when file is relative, via current working dir
     const string a = FileUtils::toAbsolutePath("foo.txt");
     BOOST_CHECK(a == root + "/" + "foo.txt");
@@ -127,4 +128,32 @@
 }
 
 
+BOOST_AUTO_TEST_CASE(test_getDirectory)
+{
+    // test absolute case
+    const std::string a = FileUtils::getDirectory(drive + "/a/b/foo.txt");
+    BOOST_CHECK(a == drive+"/a/b");
+
+    // test relative case
+    const std::string b = FileUtils::getDirectory("a/b/foo.txt");
+    BOOST_CHECK(b == "a/b");
+
+    return;
+}
+
+
+BOOST_AUTO_TEST_CASE(test_isAbsolute)
+{
+    // test absolute case
+    const bool a = FileUtils::isAbsolutePath(drive + "/a/b/foo.txt");
+    BOOST_CHECK(a);
+
+    // test relative case
+    const bool b = FileUtils::isAbsolutePath("a/b/foo.txt");
+    BOOST_CHECK(!b);
+
+    return;
+}
+
+
 BOOST_AUTO_TEST_SUITE_END()
diff -r 04ee96a6899c -r 3082b49b8975 test/unit/PipelineWriterTest.cpp
--- a/test/unit/PipelineWriterTest.cpp	Tue Aug 02 10:31:31 2011 -0700
+++ b/test/unit/PipelineWriterTest.cpp	Tue Aug 02 11:23:05 2011 -0700
@@ -61,10 +61,10 @@
 
     FileUtils::deleteFile("out.las");
 
-    bool filesSame = Support::compare_text_files("test.xml", Support::datapath("pipeline_write.xml"));
-    BOOST_CHECK(filesSame);
-
-    if (filesSame)
+    // BUG: can't compare the files, since the filename paths are now absolute, not relative
+    ////bool filesSame = Support::compare_text_files("test.xml", Support::datapath("pipeline_write.xml"));
+    ////BOOST_CHECK(filesSame);
+    ////if (filesSame)
     {
         FileUtils::deleteFile("test.xml");
     }


More information about the Liblas-commits mailing list