1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/* REminiscence - Flashback interpreter
* Copyright (C) 2005-2011 Gregory Montoir
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#endif
#include "fs.h"
struct FileSystem_impl {
FileSystem_impl() :
_fileList(0), _fileCount(0), _filePathLen(0) {
}
~FileSystem_impl() {
for (int i = 0; i < _fileCount; ++i) {
free(_fileList[i]);
}
free(_fileList);
}
void setRootDirectory(const char *dir) {
_filePathLen = strlen(dir) + 1;
buildFileListFromDirectory(dir);
}
const char *findFilePath(const char *file) {
const int len = strlen(file);
for (int i = 0; i < _fileCount; ++i) {
const char *filePath = _fileList[i];
const int filePathLen = strlen(filePath);
if (filePathLen > len && strcasecmp(filePath + filePathLen - len, file) == 0) {
return filePath;
}
}
return 0;
}
void addFileToList(const char *filePath) {
_fileList = (char **)realloc(_fileList, (_fileCount + 1) * sizeof(char *));
if (_fileList) {
_fileList[_fileCount] = strdup(filePath);
++_fileCount;
}
}
void buildFileListFromDirectory(const char *dir);
char **_fileList;
int _fileCount;
int _filePathLen;
};
#ifdef _WIN32
void FileSystem_impl::buildFileListFromDirectory(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) {
buildFileListFromDirectory(filePath);
} else {
addFileToList(filePath);
}
} while (FindNextFile(h, &findData));
FindClose(h);
}
}
#else
void FileSystem_impl::buildFileListFromDirectory(const char *dir) {
DIR *d = opendir(dir);
if (d) {
dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') {
continue;
}
char filePath[512];
snprintf(filePath, sizeof(filePath), "%s/%s", dir, de->d_name);
struct stat st;
if (stat(filePath, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
buildFileListFromDirectory(filePath);
} else {
addFileToList(filePath);
}
}
}
closedir(d);
}
}
#endif
FileSystem::FileSystem(const char *dataPath) {
_impl = new FileSystem_impl;
_impl->setRootDirectory(dataPath);
}
FileSystem::~FileSystem() {
delete _impl;
}
const char *FileSystem::findPath(const char *filename) {
return _impl->findFilePath(filename);
}
|