diff options
author | Marius Kintel <marius@kintel.net> | 2013-05-09 09:02:31 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2013-05-09 09:02:31 (GMT) |
commit | 0e938364fea8c3ab07b06491fd21cb34a403bf99 (patch) | |
tree | 89fea62a9478331937d4fc35a116fe44f517332b /src/fileutils.cc | |
parent | db8ad9631d838fba26cab298ca9de26f0cfe3a77 (diff) |
Search for included files first in the same location as the including module, then in the document root as a compatibility fallback. Fixes #217
Diffstat (limited to 'src/fileutils.cc')
-rw-r--r-- | src/fileutils.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/fileutils.cc b/src/fileutils.cc new file mode 100644 index 0000000..b844b4a --- /dev/null +++ b/src/fileutils.cc @@ -0,0 +1,35 @@ +#include "fileutils.h" +#include "printutils.h" + +#include <boost/filesystem.hpp> +namespace fs = boost::filesystem; +#include "boosty.h" + +/*! + Returns the absolute path to the given filename, unless it's empty. + If the file isn't found in the given path, the fallback path will be + used to be backwards compatible with <= 2013.01 (see issue #217). +*/ +std::string lookup_file(const std::string &filename, + const std::string &path, const std::string &fallbackpath) +{ + std::string resultfile; + if (!filename.empty() && !boosty::is_absolute(fs::path(filename))) { + fs::path absfile; + if (!path.empty()) absfile = boosty::absolute(fs::path(path) / filename); + fs::path absfile_fallback; + if (!fallbackpath.empty()) absfile_fallback = boosty::absolute(fs::path(fallbackpath) / filename); + + if (!fs::exists(absfile) && fs::exists(absfile_fallback)) { + resultfile = absfile_fallback.string(); + PRINTB("WARNING: Imported file (%s) found in document root instead of relative to the importing module. This behavior is deprecated", filename); + } + else { + resultfile = absfile.string(); + } + } + else { + resultfile = filename; + } + return resultfile; +} |