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); 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 qtTranslator;
QTranslator myTranslator; 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); app.installTranslator(&myTranslator);

View file

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

View file

@ -1,23 +1,16 @@
#include "CacheFile.h" #include "CacheFile.h"
CacheFile::CacheFile(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6) #include <QString>
{ #include "DataFile.h"
datapath = (char*)malloc(sizeof(char) * 501);
filepath = (char*)malloc(sizeof(char) * 501);
snprintf(datapath, 500, "./data/cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext); CacheFile::CacheFile(const std::string &module, const std::string &ext, const std::string &tag1, int tag2, int tag3, int tag4, int tag5, int tag6)
snprintf(filepath, 500, "./cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext);
}
CacheFile::~CacheFile()
{ {
free(datapath); 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();
free(filepath);
} }
bool CacheFile::isReadable() bool CacheFile::isReadable()
{ {
FILE* f = fopen(filepath, "rb"); FILE* f = fopen(filepath.c_str(), "rb");
if (f) if (f)
{ {
fclose(f); fclose(f);
@ -25,7 +18,14 @@ bool CacheFile::isReadable()
} }
else else
{ {
FILE* f = fopen(datapath, "rb"); std::string datapath = DataFile::findFile(filepath);
if (datapath.empty())
{
return false;
}
else
{
FILE* f = fopen(datapath.c_str(), "rb");
if (f) if (f)
{ {
fclose(f); fclose(f);
@ -36,11 +36,12 @@ bool CacheFile::isReadable()
return false; return false;
} }
} }
}
} }
bool CacheFile::isWritable() bool CacheFile::isWritable()
{ {
FILE* f = fopen("./cache/.test", "wb"); FILE* f = fopen("cache/.test", "wb");
if (f) if (f)
{ {
fclose(f); fclose(f);
@ -52,16 +53,15 @@ bool CacheFile::isWritable()
} }
} }
const char* CacheFile::getPath() std::string CacheFile::getPath()
{ {
FILE* f = fopen(datapath, "rb"); std::string datapath = DataFile::findFile(filepath);
if (f) if (datapath.empty())
{
fclose(f);
return datapath;
}
else
{ {
return filepath; return filepath;
} }
else
{
return datapath;
}
} }

View file

@ -9,16 +9,14 @@ namespace system {
class SYSTEMSHARED_EXPORT CacheFile class SYSTEMSHARED_EXPORT CacheFile
{ {
public: public:
CacheFile(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6); CacheFile(const std::string &module, const std::string &ext, const std::string &tag1, int tag2, int tag3, int tag4, int tag5, int tag6);
~CacheFile();
bool isReadable(); bool isReadable();
bool isWritable(); bool isWritable();
const char* getPath(); std::string getPath();
private: private:
char* datapath; std::string filepath;
char* 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" #include "system_global.h"
#define logDebug qDebug
#define logWarning qWarning #define logWarning qWarning
#define logError qCritical #define logError qCritical

View file

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