2016-03-20 17:00:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* REminiscence - Flashback interpreter
|
2018-01-20 17:00:00 +01:00
|
|
|
* Copyright (C) 2005-2018 Gregory Montoir (cyx@users.sourceforge.net)
|
2015-08-02 18:00:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
2016-03-20 17:00:00 +01:00
|
|
|
#include <sys/param.h>
|
2015-08-02 18:00:00 +02:00
|
|
|
#endif
|
2017-06-07 18:00:00 +02:00
|
|
|
#ifdef USE_RWOPS
|
|
|
|
#include <SDL_rwops.h>
|
|
|
|
#endif
|
2015-08-02 18:00:00 +02:00
|
|
|
#include "fs.h"
|
2016-03-20 17:00:00 +01:00
|
|
|
#include "util.h"
|
2015-08-02 18:00:00 +02:00
|
|
|
|
|
|
|
struct FileName {
|
|
|
|
char *name;
|
|
|
|
int dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FileSystem_impl {
|
|
|
|
|
|
|
|
char **_dirsList;
|
|
|
|
int _dirsCount;
|
|
|
|
FileName *_filesList;
|
|
|
|
int _filesCount;
|
|
|
|
|
|
|
|
FileSystem_impl() :
|
|
|
|
_dirsList(0), _dirsCount(0), _filesList(0), _filesCount(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~FileSystem_impl() {
|
|
|
|
for (int i = 0; i < _dirsCount; ++i) {
|
|
|
|
free(_dirsList[i]);
|
|
|
|
}
|
|
|
|
free(_dirsList);
|
|
|
|
for (int i = 0; i < _filesCount; ++i) {
|
|
|
|
free(_filesList[i].name);
|
|
|
|
}
|
|
|
|
free(_filesList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRootDirectory(const char *dir) {
|
|
|
|
getPathListFromDirectory(dir);
|
|
|
|
debug(DBG_FILE, "Found %d files and %d directories", _filesCount, _dirsCount);
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:00:00 +02:00
|
|
|
int findPathIndex(const char *name) const {
|
2015-08-02 18:00:00 +02:00
|
|
|
for (int i = 0; i < _filesCount; ++i) {
|
|
|
|
if (strcasecmp(_filesList[i].name, name) == 0) {
|
2017-06-07 18:00:00 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *getPath(const char *name) const {
|
|
|
|
const int i = findPathIndex(name);
|
|
|
|
if (i >= 0) {
|
|
|
|
const char *dir = _dirsList[_filesList[i].dir];
|
|
|
|
const int len = strlen(dir) + 1 + strlen(_filesList[i].name) + 1;
|
|
|
|
char *p = (char *)malloc(len);
|
|
|
|
if (p) {
|
|
|
|
snprintf(p, len, "%s/%s", dir, _filesList[i].name);
|
2015-08-02 18:00:00 +02:00
|
|
|
}
|
2017-06-07 18:00:00 +02:00
|
|
|
return p;
|
2015-08-02 18:00:00 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addPath(const char *dir, const char *name) {
|
|
|
|
int index = -1;
|
|
|
|
for (int i = 0; i < _dirsCount; ++i) {
|
|
|
|
if (strcmp(_dirsList[i], dir) == 0) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index == -1) {
|
|
|
|
_dirsList = (char **)realloc(_dirsList, (_dirsCount + 1) * sizeof(char *));
|
|
|
|
if (_dirsList) {
|
|
|
|
_dirsList[_dirsCount] = strdup(dir);
|
|
|
|
index = _dirsCount;
|
|
|
|
++_dirsCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_filesList = (FileName *)realloc(_filesList, (_filesCount + 1) * sizeof(FileName));
|
|
|
|
if (_filesList) {
|
|
|
|
_filesList[_filesCount].name = strdup(name);
|
|
|
|
_filesList[_filesCount].dir = index;
|
|
|
|
++_filesCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getPathListFromDirectory(const char *dir);
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
void FileSystem_impl::getPathListFromDirectory(const char *dir) {
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
char searchPath[MAX_PATH];
|
|
|
|
snprintf(searchPath, sizeof(searchPath), "%s/*", dir);
|
|
|
|
HANDLE h = FindFirstFile(searchPath, &findData);
|
|
|
|
if (h) {
|
|
|
|
do {
|
|
|
|
if (findData.cFileName[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char filePath[MAX_PATH];
|
|
|
|
snprintf(filePath, sizeof(filePath), "%s/%s", dir, findData.cFileName);
|
|
|
|
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
getPathListFromDirectory(filePath);
|
|
|
|
} else {
|
|
|
|
addPath(dir, findData.cFileName);
|
|
|
|
}
|
|
|
|
} while (FindNextFile(h, &findData));
|
|
|
|
FindClose(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void FileSystem_impl::getPathListFromDirectory(const char *dir) {
|
|
|
|
DIR *d = opendir(dir);
|
|
|
|
if (d) {
|
|
|
|
dirent *de;
|
|
|
|
while ((de = readdir(d)) != NULL) {
|
|
|
|
if (de->d_name[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-20 17:00:00 +01:00
|
|
|
char filePath[MAXPATHLEN];
|
2015-08-02 18:00:00 +02:00
|
|
|
snprintf(filePath, sizeof(filePath), "%s/%s", dir, de->d_name);
|
|
|
|
struct stat st;
|
|
|
|
if (stat(filePath, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
getPathListFromDirectory(filePath);
|
|
|
|
} else {
|
|
|
|
addPath(dir, de->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FileSystem::FileSystem(const char *dataPath) {
|
|
|
|
_impl = new FileSystem_impl;
|
|
|
|
_impl->setRootDirectory(dataPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystem::~FileSystem() {
|
|
|
|
delete _impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FileSystem::findPath(const char *filename) const {
|
2017-06-07 18:00:00 +02:00
|
|
|
return _impl->getPath(filename);
|
2015-08-02 18:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::exists(const char *filename) const {
|
2017-06-07 18:00:00 +02:00
|
|
|
if (_impl->findPathIndex(filename) >= 0) {
|
|
|
|
return true;
|
2015-08-02 18:00:00 +02:00
|
|
|
}
|
2017-06-07 18:00:00 +02:00
|
|
|
#ifdef USE_RWOPS
|
|
|
|
SDL_RWops *rw = SDL_RWFromFile(filename, "rb");
|
|
|
|
if (rw) {
|
|
|
|
SDL_RWclose(rw);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
2015-08-02 18:00:00 +02:00
|
|
|
}
|