66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
#include "DataFile.h"
|
|
|
|
#include "Logs.h"
|
|
#include <filesystem>
|
|
|
|
using namespace std::filesystem;
|
|
|
|
string DataFile::findFile(const string &relpath) {
|
|
if (dataDir.empty()) {
|
|
dataDir = initDataDir();
|
|
}
|
|
|
|
auto absPath = path(dataDir) / path(relpath);
|
|
if (exists(absPath)) {
|
|
return absPath;
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
string DataFile::findDir(const string &relpath) {
|
|
return findFile(relpath);
|
|
}
|
|
|
|
bool DataFile::tryDataDir(const string &dir) {
|
|
const auto abs_path = absolute(path(dir));
|
|
Logs::debug("System") << "Try data dir " << abs_path << endl;
|
|
return exists(abs_path / path("data") / path(".paysages_data"));
|
|
}
|
|
|
|
string DataFile::locateDataDir() {
|
|
auto dir = absolute(current_path());
|
|
int i = 0;
|
|
|
|
// TODO /usr/share/paysages3d/
|
|
|
|
while (i++ < 100 and dir.has_parent_path()) {
|
|
if (tryDataDir(dir)) {
|
|
return dir;
|
|
}
|
|
|
|
const auto dir2 = dir / path("paysages3d");
|
|
if (tryDataDir(dir2)) {
|
|
return dir2;
|
|
}
|
|
|
|
dir = dir.parent_path();
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
string DataFile::initDataDir() {
|
|
string parent = locateDataDir();
|
|
if (parent.empty()) {
|
|
Logs::warning("System") << "Data files not found" << endl;
|
|
return parent;
|
|
} else {
|
|
string result = parent / path("data");
|
|
Logs::debug("System") << "Data files found : " << result << endl;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
string DataFile::dataDir;
|