diff --git a/BaseLib/FileFinder.cpp b/BaseLib/FileFinder.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46aca453f5d9209fc5c28a7e9679c75072d79e68 --- /dev/null +++ b/BaseLib/FileFinder.cpp @@ -0,0 +1,66 @@ +/** + * \file + * \author Karsten Rink + * \date 2010-10-26 + * \brief Definition of the FileFinder class. + * + * \copyright + * Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#include "FileFinder.h" + +#include <fstream> + +#include <logog/include/logog.hpp> + + +namespace BaseLib +{ + +FileFinder::FileFinder() +{ + addDirectory("."); +} + +FileFinder::FileFinder(std::string const& dir) +{ + addDirectory("."); + addDirectory(dir); +} + +void FileFinder::addDirectory(std::string const& dir) +{ + if (dir.empty()) + return; + + if (dir[dir.size() - 1] != '/') + _directories.push_back(std::string(dir + "/")); + else + _directories.push_back(dir); +} + +std::string FileFinder::getPath(std::string const& filename) const +{ + if (_directories.empty()) + ERR("FileFinder::getPath(): No directories set."); + + for (auto const& dir : _directories) + { + std::string testDir(dir); + std::ifstream is(testDir.append(filename).c_str()); + if (is.good()) + { + is.close(); + return testDir; + } + } + ERR("FileFinder::getPath(): File not found."); + return filename; +} + +} // end namespace BaseLib diff --git a/BaseLib/FileFinder.h b/BaseLib/FileFinder.h index e4e3af4b034bea2c009166fae907334b273fb236..23ede19833e8531fb1a865efc27ca6b5cc0f0434 100644 --- a/BaseLib/FileFinder.h +++ b/BaseLib/FileFinder.h @@ -15,12 +15,9 @@ #ifndef FILEFINDER_H #define FILEFINDER_H -#include <fstream> #include <string> #include <vector> -#include "BuildInfo.h" -#include <logog/include/logog.hpp> namespace BaseLib { @@ -32,52 +29,28 @@ namespace BaseLib class FileFinder { public: - /// Constructor - FileFinder() - { - addDirectory("."); - addDirectory(BuildInfo::source_path + "/GeoLib/IO/XmlIO"); - addDirectory(BuildInfo::source_path + "/Applications/FileIO/XmlIO"); - } + /// Constructor having current directory as the search-space + FileFinder(); + + /** + * Construct with the given directory paths in addition to current directory + * + * @param dirs a vector of directory paths to the search-space + */ + explicit FileFinder(std::string const& dir); /** * \brief Adds another directory to the search-space. * If the given directory does not end with a slash one will be appended. */ - void addDirectory(std::string const& dir) - { - if (dir.empty()) - return; - - if (dir[dir.size() - 1] != '/') - _directories.push_back(std::string(dir + "/")); - else - _directories.push_back(dir); - } + void addDirectory(std::string const& dir); /** * Given a filename, this method will return the complete path where this file can be found. * If the file is located in more than one of the directories in the search list, only the * first location will be returned. */ - std::string getPath(std::string const& filename) const - { - if (_directories.empty()) - ERR("FileFinder::getPath(): No directories set."); - - for (auto it = _directories.begin(); it != _directories.end(); ++it) - { - std::string testDir(*it); - std::ifstream is(testDir.append(filename).c_str()); - if (is.good()) - { - is.close(); - return testDir; - } - } - ERR("FileFinder::getPath(): File not found."); - return filename; - } + std::string getPath(std::string const& filename) const; private: std::vector<std::string> _directories;