Skip to content
Snippets Groups Projects
Commit 38e6af5f authored by Tom Fischer's avatar Tom Fischer
Browse files

Using logog logging within the class FileFinder.

parent 79d43af7
No related branches found
No related tags found
No related merge requests found
...@@ -15,12 +15,15 @@ ...@@ -15,12 +15,15 @@
#ifndef FILEFINDER_H #ifndef FILEFINDER_H
#define FILEFINDER_H #define FILEFINDER_H
#include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <list> #include <vector>
namespace BaseLib { // ThirdParty/logog
#include "logog/include/logog.hpp"
namespace BaseLib
{
/** /**
* FileFinder stores a list of directories and will return the complete path * FileFinder stores a list of directories and will return the complete path
* for a given filename if the corresponding file is found in any of these * for a given filename if the corresponding file is found in any of these
...@@ -30,40 +33,48 @@ class FileFinder ...@@ -30,40 +33,48 @@ class FileFinder
{ {
public: public:
/// Constructor /// Constructor
FileFinder() {}; FileFinder() {}
/** /**
* \brief Adds another directory to the search-space. * \brief Adds another directory to the search-space.
* If the given directory does not end with a slash one will be appended. * If the given directory does not end with a slash one will be appended.
*/ */
void addDirectory(std::string dir) void addDirectory(std::string const& dir)
{ {
if (dir[dir.size()-1] != '/') dir.append("/"); if (dir[dir.size() - 1] != '/')
_directories.push_back(dir); _directories.push_back(std::string(dir + "/"));
}; else
_directories.push_back(dir);
}
/** /**
* Given a filename, this method will return the complete path where this file can be found. * 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 * If the file is located in more than one of the directories in the search list, only the
* first location will be returned. * first location will be returned.
*/ */
std::string getPath(std::string filename) const std::string getPath(std::string const& filename) const
{ {
if (_directories.empty()) std::cout << "Error: FileFinder::getPath() -- directory list is empty." << std::endl; if (_directories.empty())
for (std::list<std::string>::const_iterator it = _directories.begin(); it != _directories.end(); ++it) ERR("FileFinder::getPath(): No directories set.");
for (std::vector<std::string>::const_iterator it = _directories.begin(); it
!= _directories.end(); ++it)
{ {
std::string testDir(*it); std::string testDir(*it);
std::ifstream is(testDir.append(filename).c_str()); std::ifstream is(testDir.append(filename).c_str());
if (is.good()) return testDir; if (is.good())
{
is.close();
return testDir;
}
} }
std::cout << "Error: FileFinder::getPath() -- file not found." << std::endl; ERR("FileFinder::getPath(): File not found.");
return filename; return filename;
}; }
private: private:
std::list<std::string> _directories; std::vector<std::string> _directories;
}; };
} // end namespace BaseLib } // end namespace BaseLib
#endif // FILEFINDER_H #endif // FILEFINDER_H
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