2017-07-02 20:53:03 +01:00
|
|
|
#include "StdAfx.h"
|
2021-01-02 21:08:55 +00:00
|
|
|
#include "frontends/common2/resources.h"
|
2017-07-02 20:53:03 +01:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2020-07-12 09:55:51 +01:00
|
|
|
#include <libgen.h>
|
2017-07-02 20:53:03 +01:00
|
|
|
|
|
|
|
#include "Log.h"
|
2020-10-09 19:13:42 +01:00
|
|
|
#include "config.h"
|
2017-07-02 20:53:03 +01:00
|
|
|
|
2020-07-12 09:55:51 +01:00
|
|
|
namespace
|
|
|
|
{
|
2020-07-12 15:53:53 +01:00
|
|
|
|
|
|
|
bool dirExists(const std::string & folder)
|
|
|
|
{
|
|
|
|
struct stat stdbuf;
|
|
|
|
|
|
|
|
if (stat(folder.c_str(), &stdbuf) == 0 && S_ISDIR(stdbuf.st_mode))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:31:15 +01:00
|
|
|
std::string getResourcePathImpl()
|
2020-07-12 09:55:51 +01:00
|
|
|
{
|
2020-12-11 19:38:34 +00:00
|
|
|
std::vector<std::string> paths;
|
|
|
|
|
2020-07-12 09:55:51 +01:00
|
|
|
char self[1024] = {0};
|
|
|
|
const int ch = readlink("/proc/self/exe", self, sizeof(self));
|
|
|
|
if (ch != -1)
|
|
|
|
{
|
|
|
|
const char * path = dirname(self);
|
2020-07-12 15:53:53 +01:00
|
|
|
|
|
|
|
// case 1: run from the build folder
|
2020-12-11 19:38:34 +00:00
|
|
|
paths.emplace_back(std::string(path) + '/'+ ROOT_PATH);
|
2020-07-12 15:53:53 +01:00
|
|
|
// case 2: run from the installation folder
|
2020-12-11 19:38:34 +00:00
|
|
|
paths.emplace_back(std::string(path) + '/'+ SHARE_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
// case 3: use the source folder
|
|
|
|
paths.emplace_back(CMAKE_SOURCE_DIR);
|
|
|
|
|
|
|
|
for (const std::string & path : paths)
|
|
|
|
{
|
|
|
|
const std::string resourcePath = path + "/resource/";
|
|
|
|
if (dirExists(resourcePath))
|
2020-07-12 15:53:53 +01:00
|
|
|
{
|
2020-12-11 19:38:34 +00:00
|
|
|
return resourcePath;
|
2020-07-12 15:53:53 +01:00
|
|
|
}
|
2020-07-12 09:55:51 +01:00
|
|
|
}
|
2020-12-11 19:38:34 +00:00
|
|
|
|
|
|
|
throw std::runtime_error("Cannot found the resource path");
|
2020-07-12 09:55:51 +01:00
|
|
|
}
|
|
|
|
|
2020-10-09 10:31:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & getResourcePath()
|
|
|
|
{
|
|
|
|
static const std::string path = getResourcePathImpl();
|
|
|
|
return path;
|
2020-07-12 09:55:51 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:13:26 +01:00
|
|
|
HRSRC FindResource(void *, const char * filename, const char *)
|
2017-07-02 20:53:03 +01:00
|
|
|
{
|
|
|
|
HRSRC result;
|
|
|
|
|
2020-07-07 20:13:26 +01:00
|
|
|
if (filename)
|
2017-07-02 20:53:03 +01:00
|
|
|
{
|
2020-10-09 10:31:15 +01:00
|
|
|
const std::string path = getResourcePath() + filename;
|
2017-07-02 20:53:03 +01:00
|
|
|
|
|
|
|
int fd = open(path.c_str(), O_RDONLY);
|
|
|
|
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
struct stat stdbuf;
|
|
|
|
if ((fstat(fd, &stdbuf) == 0) && S_ISREG(stdbuf.st_mode))
|
|
|
|
{
|
|
|
|
const off_t size = stdbuf.st_size;
|
|
|
|
result.data.resize(size);
|
|
|
|
ssize_t rd = read(fd, result.data.data(), size);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.data.empty())
|
|
|
|
{
|
2020-07-07 20:13:26 +01:00
|
|
|
LogFileOutput("FindResource: could not load resource %s\n", filename);
|
2017-07-02 20:53:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|