Skip to content
Snippets Groups Projects
Commit 33a2d946 authored by Norihiro Watanabe's avatar Norihiro Watanabe
Browse files

add FileFinder constructor taking a path and move definitions to cpp

parent 48a61d63
No related branches found
No related tags found
No related merge requests found
/**
* \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
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment