2
#define WIN32_LEAN_AND_MEAN
6
#define strcasecmp _stricmp
8
#define fseeko _fseeki64
9
#define ftello _ftelli64
23
#include "base/logging.h"
24
#include "base/basictypes.h"
25
#include "file/file_util.h"
26
#include "util/text/utf8.h"
28
#if !defined(__linux__) && !defined(_WIN32) && !defined(__QNX__)
34
static inline int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
35
struct dirent *readdir_entry;
37
readdir_entry = readdir(dirp);
38
if (readdir_entry == NULL) {
43
*entry = *readdir_entry;
49
FILE *openCFile(const std::string &filename, const char *mode)
51
#if defined(_WIN32) && defined(UNICODE)
52
return _wfopen(ConvertUTF8ToWString(filename).c_str(), ConvertUTF8ToWString(mode).c_str());
54
return fopen(filename.c_str(), mode);
58
bool writeStringToFile(bool text_file, const std::string &str, const char *filename)
60
FILE *f = openCFile(filename, text_file ? "w" : "wb");
63
size_t len = str.size();
64
if (len != fwrite(str.data(), 1, str.size(), f))
73
bool writeDataToFile(bool text_file, const void* data, const unsigned int size, const char *filename)
75
FILE *f = openCFile(filename, text_file ? "w" : "wb");
79
if (len != fwrite(data, 1, len, f))
88
uint64_t GetSize(FILE *f)
90
// This will only support 64-bit when large file support is available.
91
// That won't be the case on some versions of Android, at least.
92
#if defined(ANDROID) || (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS < 64)
95
off64_t pos = lseek64(fd, 0, SEEK_CUR);
96
off64_t size = lseek64(fd, 0, SEEK_END);
97
if (size != pos && lseek64(fd, pos, SEEK_SET) != pos) {
103
uint64_t pos = ftello(f);
104
if (fseek(f, 0, SEEK_END) != 0) {
107
uint64_t size = ftello(f);
108
// Reset the seek position to where it was when we started.
109
if (size != pos && fseeko(f, pos, SEEK_SET) != 0) {
110
// Should error here.
117
bool readFileToString(bool text_file, const char *filename, std::string &str)
119
FILE *f = openCFile(filename, text_file ? "r" : "rb");
122
size_t len = (size_t)GetSize(f);
123
char *buf = new char[len + 1];
124
buf[fread(buf, 1, len, f)] = 0;
125
str = std::string(buf, len);
132
bool readDataFromFile(bool text_file, unsigned char* &data, const unsigned int size, const char *filename)
134
FILE *f = openCFile(filename, text_file ? "r" : "rb");
137
size_t len = (size_t)GetSize(f);
142
data[fread(data, 1, size, f)] = 0;
147
// Returns true if filename is a directory
148
bool isDirectory(const std::string &filename) {
150
getFileInfo(filename.c_str(), &info);
151
return info.isDirectory;
154
bool getFileInfo(const char *path, FileInfo *fileInfo) {
155
// TODO: Expand relative paths?
156
fileInfo->fullName = path;
159
WIN32_FILE_ATTRIBUTE_DATA attrs;
160
if (!GetFileAttributesExW(ConvertUTF8ToWString(path).c_str(), GetFileExInfoStandard, &attrs)) {
162
fileInfo->isDirectory = false;
163
fileInfo->exists = false;
166
fileInfo->size = (uint64_t)attrs.nFileSizeLow | ((uint64_t)attrs.nFileSizeHigh << 32);
167
fileInfo->isDirectory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
168
fileInfo->isWritable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
169
fileInfo->exists = true;
171
struct stat64 file_info;
173
std::string copy(path);
175
int result = stat64(copy.c_str(), &file_info);
178
WLOG("IsDirectory: stat failed on %s", path);
179
fileInfo->exists = false;
183
fileInfo->isDirectory = S_ISDIR(file_info.st_mode);
184
fileInfo->isWritable = false;
185
fileInfo->size = file_info.st_size;
186
fileInfo->exists = true;
187
// HACK: approximation
188
if (file_info.st_mode & 0200)
189
fileInfo->isWritable = true;
194
std::string getFileExtension(const std::string &fn) {
195
int pos = (int)fn.rfind(".");
196
if (pos < 0) return "";
197
std::string ext = fn.substr(pos+1);
198
for (size_t i = 0; i < ext.size(); i++) {
199
ext[i] = tolower(ext[i]);
204
bool FileInfo::operator <(const FileInfo &other) const {
205
if (isDirectory && !other.isDirectory)
207
else if (!isDirectory && other.isDirectory)
209
if (strcasecmp(name.c_str(), other.name.c_str()) < 0)
215
size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files, const char *filter, int flags) {
216
size_t foundEntries = 0;
217
std::set<std::string> filters;
221
if (*filter == ':') {
225
tmp.push_back(*filter);
233
// Find the first file in the directory.
237
HANDLE hFind = FindFirstFile((ConvertUTF8ToWString(directory) + L"\\*").c_str(), &ffd);
239
HANDLE hFind = FindFirstFile((std::string(directory) + "\\*").c_str(), &ffd);
241
if (hFind == INVALID_HANDLE_VALUE) {
248
const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName);
250
struct dirent_large { struct dirent entry; char padding[FILENAME_MAX+1]; };
251
struct dirent_large diren;
252
struct dirent *result = NULL;
254
//std::string directoryWithSlash = directory;
255
//if (directoryWithSlash.back() != '/')
256
// directoryWithSlash += "/";
258
DIR *dirp = opendir(directory);
262
while (!readdir_r(dirp, (dirent*) &diren, &result) && result)
264
const std::string virtualName(result->d_name);
266
// check for "." and ".."
267
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
268
((virtualName[0] == '.') && (virtualName[1] == '.') &&
269
(virtualName[2] == '\0')))
272
// Remove dotfiles (optional with flag.)
273
if (!(flags & GETFILES_GETHIDDEN) && virtualName[0] == '.')
277
info.name = virtualName;
278
std::string dir = directory;
280
// Only append a slash if there isn't one on the end.
281
size_t lastSlash = dir.find_last_of("/");
282
if (lastSlash != (dir.length() - 1))
285
info.fullName = dir + virtualName;
286
info.isDirectory = isDirectory(info.fullName);
289
if (!info.isDirectory) {
290
std::string ext = getFileExtension(info.fullName);
292
if (filters.find(ext) == filters.end())
298
files->push_back(info);
301
} while (FindNextFile(hFind, &ffd) != 0);
308
std::sort(files->begin(), files->end());
313
// Returns a vector with the device names
314
std::vector<std::string> getWindowsDrives()
316
std::vector<std::string> drives;
318
const DWORD buffsize = GetLogicalDriveStrings(0, NULL);
319
std::vector<TCHAR> buff(buffsize);
320
if (GetLogicalDriveStrings(buffsize, buff.data()) == buffsize - 1)
322
auto drive = buff.data();
325
std::string str(ConvertWStringToUTF8(drive));
326
str.pop_back(); // we don't want the final backslash
328
drives.push_back(str);
330
// advance to next drive