Improved data files locator

This commit is contained in:
Michaël Lemaire 2014-09-18 11:39:36 +02:00
parent f869c2a01d
commit 553b6b2896
8 changed files with 151 additions and 50 deletions

View file

@ -39,16 +39,11 @@ int main(int argc, char** argv)
QApplication app(argc, argv);
if (not QFileInfo("./data/.paysages_data").isReadable())
{
QMessageBox::critical(NULL, QObject::tr("Paysages 3D - Data error"), QObject::tr("Application data were not found. Please ensure the software is run from its original directory."));
return 1;
}
QTranslator qtTranslator;
QTranslator myTranslator;
if (myTranslator.load("paysages_" + QLocale::system().name(), "./data/i18n") || myTranslator.load("paysages_" + QLocale::system().name(), "/usr/share/paysages3d/i18n"))
QString i18ndir = QString::fromStdString(DataFile::findDir("i18n"));
if (not i18ndir.isEmpty() and myTranslator.load("paysages_" + QLocale::system().name(), i18ndir))
{
app.installTranslator(&myTranslator);

View file

@ -3,7 +3,7 @@
#include <QColor>
#include <QDialog>
#include <QDir>
#include "DataFile.h"
#include "Color.h"
@ -15,15 +15,7 @@ static inline QColor colorToQColor(Color color)
static inline QString getDataPath(QString path)
{
QFile datafile(QDir("/usr/share/paysages3d/").absoluteFilePath(path));
if (datafile.exists())
{
return datafile.fileName();
}
else
{
return QDir("./data").absoluteFilePath(path);
}
return QString::fromStdString(DataFile::findFile(path.toStdString()));
}
QString getHumanMemory(qint64 memused);

View file

@ -1,23 +1,16 @@
#include "CacheFile.h"
CacheFile::CacheFile(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6)
{
datapath = (char*)malloc(sizeof(char) * 501);
filepath = (char*)malloc(sizeof(char) * 501);
#include <QString>
#include "DataFile.h"
snprintf(datapath, 500, "./data/cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext);
snprintf(filepath, 500, "./cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext);
}
CacheFile::~CacheFile()
CacheFile::CacheFile(const std::string &module, const std::string &ext, const std::string &tag1, int tag2, int tag3, int tag4, int tag5, int tag6)
{
free(datapath);
free(filepath);
filepath = QString("cache/%1-%2-%3-%4-%5-%6-%7.%8").arg(QString::fromStdString(module)).arg(QString::fromStdString(tag1)).arg(tag2).arg(tag3).arg(tag4).arg(tag5).arg(tag6).arg(QString::fromStdString(ext)).toStdString();
}
bool CacheFile::isReadable()
{
FILE* f = fopen(filepath, "rb");
FILE* f = fopen(filepath.c_str(), "rb");
if (f)
{
fclose(f);
@ -25,22 +18,30 @@ bool CacheFile::isReadable()
}
else
{
FILE* f = fopen(datapath, "rb");
if (f)
std::string datapath = DataFile::findFile(filepath);
if (datapath.empty())
{
fclose(f);
return true;
return false;
}
else
{
return false;
FILE* f = fopen(datapath.c_str(), "rb");
if (f)
{
fclose(f);
return true;
}
else
{
return false;
}
}
}
}
bool CacheFile::isWritable()
{
FILE* f = fopen("./cache/.test", "wb");
FILE* f = fopen("cache/.test", "wb");
if (f)
{
fclose(f);
@ -52,16 +53,15 @@ bool CacheFile::isWritable()
}
}
const char* CacheFile::getPath()
std::string CacheFile::getPath()
{
FILE* f = fopen(datapath, "rb");
if (f)
{
fclose(f);
return datapath;
}
else
std::string datapath = DataFile::findFile(filepath);
if (datapath.empty())
{
return filepath;
}
else
{
return datapath;
}
}

View file

@ -9,16 +9,14 @@ namespace system {
class SYSTEMSHARED_EXPORT CacheFile
{
public:
CacheFile(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6);
~CacheFile();
CacheFile(const std::string &module, const std::string &ext, const std::string &tag1, int tag2, int tag3, int tag4, int tag5, int tag6);
bool isReadable();
bool isWritable();
const char* getPath();
std::string getPath();
private:
char* datapath;
char* filepath;
std::string filepath;
};
}

72
src/system/DataFile.cpp Normal file
View file

@ -0,0 +1,72 @@
#include "DataFile.h"
#include "Logs.h"
#include <QDir>
std::string DataFile::findFile(const std::string &relpath)
{
QDir dir(QString::fromStdString(dataDir));
if (dir.exists(QString::fromStdString(relpath)))
{
return dir.absoluteFilePath(QString::fromStdString(relpath)).toStdString();
}
else
{
return "";
}
}
std::string DataFile::findDir(const std::string &relpath)
{
return findFile(relpath);
}
bool DataFile::tryDataDir(const QDir &dir)
{
logDebug("[System] Try data dir %s", dir.absolutePath().toLocal8Bit().data());
return dir.exists("data/.paysages_data");
}
std::string DataFile::locateDataDir()
{
QDir dir = QDir::current();
int i = 0;
// TODO /usr/share/paysages3d/
while (i++ < 100 and not dir.isRoot())
{
if (tryDataDir(dir))
{
return dir.absolutePath().toStdString();
}
QDir dir2(dir.absoluteFilePath("paysages3d"));
if (tryDataDir(dir2))
{
return dir2.absolutePath().toStdString();
}
dir = QDir(QDir(dir.absoluteFilePath("..")).canonicalPath());
}
return "";
}
std::string DataFile::initDataDir()
{
std::string parent = locateDataDir();
if (parent.empty())
{
logWarning("[System] Data files not found");
return parent;
}
else
{
std::string result = QDir(QString::fromStdString(parent)).absoluteFilePath("data").toStdString();
logDebug("[System] Data files found : %s", result.c_str());
return result;
}
}
std::string DataFile::dataDir = DataFile::initDataDir();

41
src/system/DataFile.h Normal file
View file

@ -0,0 +1,41 @@
#ifndef DATAFILE_H
#define DATAFILE_H
#include "system_global.h"
class QDir;
namespace paysages {
namespace system {
/**
* Locator of data files.
*/
class SYSTEMSHARED_EXPORT DataFile
{
public:
/**
* Find a data file.
*
* Return the absolute data path, or an empty string if not found.
*/
static std::string findFile(const std::string &relpath);
/**
* Find a data directory.
*
* Return the absolute data path, or an empty string if not found.
*/
static std::string findDir(const std::string &relpath);
private:
static bool tryDataDir(const QDir &dir);
static std::string locateDataDir();
static std::string initDataDir();
static std::string dataDir;
};
}
}
#endif // DATAFILE_H

View file

@ -3,6 +3,7 @@
#include "system_global.h"
#define logDebug qDebug
#define logWarning qWarning
#define logError qCritical

View file

@ -27,7 +27,8 @@ SOURCES += \
ParallelPool.cpp \
ParallelWorker.cpp \
Semaphore.cpp \
FileSystem.cpp
FileSystem.cpp \
DataFile.cpp
HEADERS += \
system_global.h \
@ -44,7 +45,8 @@ HEADERS += \
ParallelPool.h \
ParallelWorker.h \
Semaphore.h \
FileSystem.h
FileSystem.h \
DataFile.h
unix:!symbian {
maemo5 {