diff --git a/include/pdal/Stage.hpp b/include/pdal/Stage.hpp index b76eaf6e40..449887181a 100644 --- a/include/pdal/Stage.hpp +++ b/include/pdal/Stage.hpp @@ -114,7 +114,6 @@ class PDAL_DLL Stage virtual std::string getName() const = 0; const std::vector& getInputs() const { return m_inputs; } - std::vector findStage(std::string name); virtual Options getDefaultOptions() { return Options(); } static Dimension::IdList getDefaultDimensions() diff --git a/include/pdal/util/FileUtils.hpp b/include/pdal/util/FileUtils.hpp index 2150ac3550..c2de62afd5 100644 --- a/include/pdal/util/FileUtils.hpp +++ b/include/pdal/util/FileUtils.hpp @@ -57,71 +57,63 @@ namespace pdal { -// this is a static class -- do not instantiate] -class PDAL_DLL FileUtils +namespace FileUtils { -public: // open existing file for reading - static std::istream* openFile(std::string const& filename, - bool asBinary=true); + 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); + std::ostream* createFile(std::string const& filename, bool asBinary=true); - static bool directoryExists(std::string const& dirname); - static bool createDirectory(std::string const& dirname); - static void deleteDirectory(std::string const& dirname); + bool directoryExists(std::string const& dirname); + bool createDirectory(std::string const& dirname); + void deleteDirectory(std::string const& dirname); - static void closeFile(std::ostream* ofs); - static void closeFile(std::istream* ifs); + void closeFile(std::ostream* ofs); + 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 uintmax_t fileSize(const std::string& filename); + bool deleteFile(const std::string& filename); + void renameFile(const std::string& dest, const std::string& src); + bool fileExists(const std::string& filename); + uintmax_t fileSize(const std::string& filename); // reads a file into a text string for you - static std::string readFileIntoString(const std::string& filename); + std::string readFileIntoString(const std::string& filename); // return current working dir // the result will always have a trailing '/' - static std::string getcwd(); + std::string getcwd(); // return the file component of the given path, // e.g. "d:/foo/bar/a.c" -> "a.c" - static std::string getFilename(const std::string& path); + std::string getFilename(const std::string& path); // return the directory component of the given path, // e.g. "d:/foo/bar/a.c" -> "d:/foo/bar" // the result will always have a trailing '/' - static std::string getDirectory(const std::string& path); + std::string getDirectory(const std::string& path); // returns true iff the path is not relative - static bool isAbsolutePath(const std::string& path); + 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); + 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, + std::string toAbsolutePath(const std::string& filename, const std::string base); - static std::string readFileAsString(std::string const& filename); - static void fileTimes(const std::string& filename, struct tm *createTime, + std::string readFileAsString(std::string const& filename); + void fileTimes(const std::string& filename, struct tm *createTime, struct tm *modTime); -private: - static std::string addTrailingSlash(std::string path); - - FileUtils& operator=(const FileUtils&); // not implemented - FileUtils(const FileUtils&); // not implemented; -}; + std::string extension(const std::string& filename); +} } // namespace pdal diff --git a/src/KernelSupport.cpp b/src/KernelSupport.cpp index a8340121ca..5b7f9f4172 100644 --- a/src/KernelSupport.cpp +++ b/src/KernelSupport.cpp @@ -34,9 +34,6 @@ #include -#include -#include - #include #include #include @@ -56,7 +53,7 @@ PipelineManagerPtr KernelSupport::makePipeline(const std::string& inputFile) PipelineReader pipeReader(*output); pipeReader.readPipeline(std::cin); } - else if (boost::filesystem::extension(inputFile) == ".xml") + else if (FileUtils::extension(inputFile) == ".xml") { PipelineReader pipeReader(*output); pipeReader.readPipeline(inputFile); @@ -116,13 +113,10 @@ ShellScriptCallback::ShellScriptCallback( if (command.size()) { m_command = command[0]; - if (command.size() == 3) - { - major_tick = boost::lexical_cast(command[1]); - minor_tick = boost::lexical_cast(command[2]); - } - else if (command.size() == 2) - major_tick = boost::lexical_cast(command[1]); + if (command.size() > 1) + Utils::fromString(command[1], major_tick); + if (command.size() > 2) + Utils::fromString(command[2], minor_tick); } PercentageCallback(major_tick, minor_tick); } @@ -138,10 +132,11 @@ void ShellScriptCallback::callback() m_done = true; else if (currPerc >= m_lastMajorPerc + 10.0) { + std::ostringstream cmd; std::string output; - Utils::run_shell_command(m_command + " " + - boost::lexical_cast(static_cast(currPerc)), - output); + + cmd << m_command << " " << (int)currPerc; + Utils::run_shell_command(cmd.str(), output); m_lastMajorPerc = currPerc; m_lastMinorPerc = currPerc; } diff --git a/src/PipelineReader.cpp b/src/PipelineReader.cpp index 189bddc1a5..a9aeb6f686 100644 --- a/src/PipelineReader.cpp +++ b/src/PipelineReader.cpp @@ -41,8 +41,6 @@ #include #include -#include -#include #ifndef _WIN32 #include @@ -161,7 +159,7 @@ Option PipelineReader::parseElement_Option(const ptree& tree) std::string name = attrs["name"]; std::string value = tree.get_value(); - boost::algorithm::trim(value); + Utils::trim(value); Option option(name, value); // filenames in the XML are fixed up as follows: @@ -352,7 +350,7 @@ void PipelineReader::parse_attributes(map_t& attrs, const ptree& tree) { std::string name = iter->first; std::string value = tree.get(name); - boost::algorithm::trim(value); + Utils::trim(value); attrs[name] = value; } diff --git a/src/PipelineWriter.cpp b/src/PipelineWriter.cpp index 43b3778414..76a74be193 100644 --- a/src/PipelineWriter.cpp +++ b/src/PipelineWriter.cpp @@ -42,8 +42,6 @@ #include #include -#include -#include #include #include @@ -142,7 +140,7 @@ void PipelineWriter::writePipeline(const std::string& filename) const const xml_parser::xml_writer_settings settings(' ', 4); #endif - if (boost::iequals(filename, "STDOUT")) + if (Utils::iequals(filename, "STDOUT")) xml_parser::write_xml(std::cout, tree); else xml_parser::write_xml(filename, tree, std::locale(), settings); diff --git a/src/PointView.cpp b/src/PointView.cpp index b9b7b7cb83..b9bf97b77d 100644 --- a/src/PointView.cpp +++ b/src/PointView.cpp @@ -37,8 +37,6 @@ #include #include -#include - namespace pdal { diff --git a/src/Stage.cpp b/src/Stage.cpp index a77b0fcaea..171d6f2311 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -362,27 +362,6 @@ void Stage::setSpatialReference(MetadataNode& m, } } -std::vector Stage::findStage(std::string name) -{ - std::vector output; - - if (boost::iequals(getName(), name)) - output.push_back(this); - - for (auto const& stage : m_inputs) - { - if (boost::iequals(stage->getName(), name)) - output.push_back(stage); - if (stage->getInputs().size()) - { - auto hits = stage->findStage(name); - if (hits.size()) - output.insert(output.end(), hits.begin(), hits.end()); - } - } - - return output; -} std::ostream& operator<<(std::ostream& ostr, const Stage& stage) { diff --git a/src/StageFactory.cpp b/src/StageFactory.cpp index d68e0288b4..daf4d92173 100644 --- a/src/StageFactory.cpp +++ b/src/StageFactory.cpp @@ -34,6 +34,7 @@ #include #include +#include // filters #include @@ -74,8 +75,6 @@ #include #include -#include - #include #include #include // for funcptr @@ -90,7 +89,7 @@ std::string StageFactory::inferReaderDriver(const std::string& filename) if (Utils::iequals(http, "http")) return "readers.greyhound"; - std::string ext = boost::filesystem::extension(filename); + std::string ext = FileUtils::extension(filename); std::map drivers; drivers["bin"] = "readers.terrasolid"; drivers["bpf"] = "readers.bpf"; @@ -125,7 +124,7 @@ std::string StageFactory::inferReaderDriver(const std::string& filename) std::string StageFactory::inferWriterDriver(const std::string& filename) { - std::string ext = Utils::tolower(boost::filesystem::extension(filename)); + std::string ext = Utils::tolower(FileUtils::extension(filename)); std::map drivers; drivers["bpf"] = "writers.bpf"; @@ -162,7 +161,7 @@ std::string StageFactory::inferWriterDriver(const std::string& filename) pdal::Options StageFactory::inferWriterOptionsChanges( const std::string& filename) { - std::string ext = boost::filesystem::extension(filename); + std::string ext = FileUtils::extension(filename); ext = Utils::tolower(ext); Options options; diff --git a/src/util/FileUtils.cpp b/src/util/FileUtils.cpp index 8c2e9597f0..2a7d5a6dff 100644 --- a/src/util/FileUtils.cpp +++ b/src/util/FileUtils.cpp @@ -61,14 +61,24 @@ bool isStdout(std::string filename) Utils::toupper(filename) == "STDOUT"; } +string addTrailingSlash(string path) +{ + if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') + path += "/"; + return path; +} + } // unnamed namespace -istream* FileUtils::openFile(string const& filename, bool asBinary) +namespace FileUtils +{ + +istream *openFile(string const& filename, bool asBinary) { if (isStdin(filename)) return &cin; - if (!FileUtils::fileExists(filename)) + if (!fileExists(filename)) throw pdal_error(std::string("File '") + filename + "' not found"); ios::openmode mode = ios::in; @@ -85,7 +95,7 @@ istream* FileUtils::openFile(string const& filename, bool asBinary) } -ostream* FileUtils::createFile(string const& filename, bool asBinary) +ostream *createFile(string const& filename, bool asBinary) { if (isStdout(filename)) return &cout; @@ -104,25 +114,24 @@ ostream* FileUtils::createFile(string const& filename, bool asBinary) } -bool FileUtils::directoryExists(std::string const& dirname) +bool directoryExists(std::string const& dirname) { return boost::filesystem::exists(dirname); -} +} - -bool FileUtils::createDirectory(std::string const& dirname) +bool createDirectory(std::string const& dirname) { return boost::filesystem::create_directory(dirname); } -void FileUtils::deleteDirectory(std::string const& dirname) +void deleteDirectory(std::string const& dirname) { boost::filesystem::remove_all(dirname); } -void FileUtils::closeFile(ostream *out) +void closeFile(ostream *out) { // An ofstream is closeable and deletable, but // an ostream like &std::cout isn't. @@ -137,7 +146,7 @@ void FileUtils::closeFile(ostream *out) } -void FileUtils::closeFile(istream* in) +void closeFile(istream* in) { // An ifstream is closeable and deletable, but // an istream like &std::cin isn't. @@ -152,7 +161,7 @@ void FileUtils::closeFile(istream* in) } -bool FileUtils::deleteFile(const string& file) +bool deleteFile(const string& file) { if (!fileExists(file)) return false; @@ -161,13 +170,13 @@ bool FileUtils::deleteFile(const string& file) } -void FileUtils::renameFile(const string& dest, const string& src) +void renameFile(const string& dest, const string& src) { boost::filesystem::rename(src, dest); } -bool FileUtils::fileExists(const string& name) +bool fileExists(const string& name) { // filename may actually be a greyhound uri + pipelineId std::string http = name.substr(0, 4); @@ -180,32 +189,24 @@ bool FileUtils::fileExists(const string& name) } -uintmax_t FileUtils::fileSize(const string& file) +uintmax_t fileSize(const string& file) { return boost::filesystem::file_size(file); } -string FileUtils::readFileIntoString(const string& filename) +string readFileIntoString(const string& filename) { - istream* stream = FileUtils::openFile(filename, false); + istream* stream = openFile(filename, false); assert(stream); string str((istreambuf_iterator(*stream)), istreambuf_iterator()); - FileUtils::closeFile(stream); + closeFile(stream); return str; } -string FileUtils::addTrailingSlash(string path) -{ - if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') - path += "/"; - return path; -} - - -string FileUtils::getcwd() +string getcwd() { const boost::filesystem::path p = boost::filesystem::current_path(); return addTrailingSlash(p.string()); @@ -214,7 +215,7 @@ string FileUtils::getcwd() /*** // Non-boost alternative. Requires file existence. -string FileUtils::toAbsolutePath(const string& filename) +string toAbsolutePath(const string& filename) { std::string result; @@ -233,7 +234,7 @@ string FileUtils::toAbsolutePath(const string& filename) // if the filename is an absolute path, just return it // otherwise, make it absolute (relative to current working dir) and return that -string FileUtils::toAbsolutePath(const string& filename) +string toAbsolutePath(const string& filename) { #if BOOST_VERSION >= 104600 && BOOST_FILESYSTEM_VERSION >= 3 @@ -251,7 +252,7 @@ string FileUtils::toAbsolutePath(const string& filename) // // note: if base dir is not absolute, first make it absolute via // toAbsolutePath(base) -string FileUtils::toAbsolutePath(const string& filename, const string base) +string toAbsolutePath(const string& filename, const string base) { const string newbase = toAbsolutePath(base); @@ -264,7 +265,7 @@ string FileUtils::toAbsolutePath(const string& filename, const string base) return p.string(); } -string FileUtils::getFilename(const string& path) +string getFilename(const string& path) { #ifdef _WIN32 char pathsep = '\\'; @@ -279,7 +280,7 @@ string FileUtils::getFilename(const string& path) } // Get the directory part of a filename. -string FileUtils::getDirectory(const string& path) +string getDirectory(const string& path) { const boost::filesystem::path dir = boost::filesystem::path(path).parent_path(); @@ -288,7 +289,7 @@ string FileUtils::getDirectory(const string& path) // Determine if the path is an absolute path -bool FileUtils::isAbsolutePath(const string& path) +bool isAbsolutePath(const string& path) { #if BOOST_VERSION >= 104600 && BOOST_FILESYSTEM_VERSION >= 3 return boost::filesystem::path(path).is_absolute(); @@ -298,7 +299,7 @@ bool FileUtils::isAbsolutePath(const string& path) } -void FileUtils::fileTimes(const std::string& filename, +void fileTimes(const std::string& filename, struct tm *createTime, struct tm *modTime) { #ifdef WIN32 @@ -321,9 +322,9 @@ void FileUtils::fileTimes(const std::string& filename, } -string FileUtils::readFileAsString(string const& filename) +string readFileAsString(string const& filename) { - if (!FileUtils::fileExists(filename)) + if (!fileExists(filename)) { ostringstream oss; oss << filename << " does not exist"; @@ -331,7 +332,7 @@ string FileUtils::readFileAsString(string const& filename) } istream::pos_type size; - istream* input = FileUtils::openFile(filename, true); + istream* input = openFile(filename, true); if (input->good()) { @@ -351,8 +352,19 @@ string FileUtils::readFileAsString(string const& filename) } return output; } - FileUtils::closeFile(input); + closeFile(input); return string(); } +std::string extension(const std::string& filename) +{ + auto idx = filename.find_last_of('.'); + if (idx == std::string::npos) + return std::string(); + return filename.substr(idx); +} + +} // namespace FileUtils + } // namespace pdal + diff --git a/src/util/Utils.cpp b/src/util/Utils.cpp index d1dae07080..28f663bdeb 100644 --- a/src/util/Utils.cpp +++ b/src/util/Utils.cpp @@ -32,9 +32,6 @@ * OF SUCH DAMAGE. ****************************************************************************/ -#include -#include - #include #include @@ -520,22 +517,6 @@ int Utils::portable_pclose(FILE* fp) } -// BUG: -// Under unix, the pclose() operation causes the boost unit test system -// to produce a fatal error iff the process started by popen returns a -// nonzero status code. For this reason, I've put all the "negative" -// cmd line app tests under #ifdef PDAL_COMPILER_MSVC. -// -// This problem shows up on mpg's Ubuntu 11.4 machine (gcc 4.5.2, boost 1.47.0) -// as well as on Hobu's machine. - -// Boost's unit test system has a flag on the execution monitor that catches -// all signals -- p_catch_system_errors, and throws unittest errors when -// it sees them. We can use --catch_system_errors=no as part of the invocation, -// or manually turn them off in the execution monitor. -- hobu 7/12/2012 -// boost::unit_test::unit_test_monitor.p_catch_system_errors.set (false); -// #include - int Utils::run_shell_command(const string& cmd, string& output) { const int maxbuf = 4096; @@ -543,7 +524,7 @@ int Utils::run_shell_command(const string& cmd, string& output) output = ""; - const char* gdal_debug = ::pdal::Utils::getenv("CPL_DEBUG"); + const char* gdal_debug = Utils::getenv("CPL_DEBUG"); if (gdal_debug == 0) { pdal::Utils::putenv("CPL_DEBUG=OFF"); diff --git a/test/unit/FileUtilsTest.cpp b/test/unit/FileUtilsTest.cpp index 8a40708bfd..16869815b7 100644 --- a/test/unit/FileUtilsTest.cpp +++ b/test/unit/FileUtilsTest.cpp @@ -169,3 +169,12 @@ TEST(FileUtilsTest, filename) std::string filename = "/foo//bar//baz.c"; EXPECT_EQ(FileUtils::getFilename(filename), "baz.c"); } + +TEST(FileUtilsTest, extension) +{ + EXPECT_EQ(FileUtils::extension("/foo//bar//baz.c"), ".c"); + EXPECT_EQ(FileUtils::extension("foobar"), ""); + EXPECT_EQ(FileUtils::extension("/foo/bar"), ""); + EXPECT_EQ(FileUtils::extension("/fo.o/b.ar.baz23"), ".baz23"); +} +