2012-03-24 22:39:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <string>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "file/file_util.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-04-12 13:18:43 +00:00
|
|
|
bool writeStringToFile(bool text_file, const std::string &str, const char *filename)
|
2012-03-24 22:39:19 +00:00
|
|
|
{
|
2012-05-08 20:29:28 +00:00
|
|
|
FILE *f = fopen(filename, text_file ? "w" : "wb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
size_t len = str.size();
|
|
|
|
if (len != fwrite(str.data(), 1, str.size(), f))
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetSize(FILE *f)
|
|
|
|
{
|
2012-05-08 20:29:28 +00:00
|
|
|
// can't use off_t here because it can be 32-bit
|
|
|
|
uint64_t pos = ftell(f);
|
|
|
|
if (fseek(f, 0, SEEK_END) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint64_t size = ftell(f);
|
2012-03-31 09:16:13 +00:00
|
|
|
// Reset the seek position to where it was when we started.
|
2012-05-08 20:29:28 +00:00
|
|
|
if ((size != pos) && (fseek(f, pos, SEEK_SET) != 0)) {
|
2012-03-31 09:16:13 +00:00
|
|
|
// Should error here
|
2012-05-08 20:29:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return size;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadFileToString(bool text_file, const char *filename, std::string &str)
|
|
|
|
{
|
2012-05-08 20:29:28 +00:00
|
|
|
FILE *f = fopen(filename, text_file ? "r" : "rb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
size_t len = (size_t)GetSize(f);
|
|
|
|
char *buf = new char[len + 1];
|
|
|
|
buf[fread(buf, 1, len, f)] = 0;
|
|
|
|
str = std::string(buf, len);
|
|
|
|
fclose(f);
|
|
|
|
delete [] buf;
|
|
|
|
return true;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DIR_SEP "/"
|
2012-10-28 10:37:10 +00:00
|
|
|
#define DIR_SEP_CHR '\\'
|
2012-03-24 22:39:19 +00:00
|
|
|
|
|
|
|
#ifndef METRO
|
|
|
|
|
2012-10-28 10:37:10 +00:00
|
|
|
// Remove any ending forward slashes from directory paths
|
|
|
|
// Modifies argument.
|
|
|
|
static void stripTailDirSlashes(std::string &fname)
|
|
|
|
{
|
|
|
|
if (fname.length() > 1)
|
|
|
|
{
|
|
|
|
size_t i = fname.length() - 1;
|
|
|
|
while (fname[i] == DIR_SEP_CHR)
|
|
|
|
fname[i--] = '\0';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if file filename exists
|
|
|
|
bool exists(const std::string &filename)
|
|
|
|
{
|
|
|
|
struct stat64 file_info;
|
|
|
|
|
|
|
|
std::string copy(filename);
|
|
|
|
stripTailDirSlashes(copy);
|
|
|
|
|
|
|
|
int result = stat64(copy.c_str(), &file_info);
|
|
|
|
|
|
|
|
return (result == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if filename is a directory
|
|
|
|
bool isDirectory(const std::string &filename)
|
|
|
|
{
|
|
|
|
struct stat64 file_info;
|
|
|
|
|
|
|
|
std::string copy(filename);
|
|
|
|
stripTailDirSlashes(copy);
|
|
|
|
|
|
|
|
int result = stat64(copy.c_str(), &file_info);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
WLOG("IsDirectory: stat failed on %s", filename.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_ISDIR(file_info.st_mode);
|
|
|
|
}
|
|
|
|
|
2012-09-17 19:21:34 +00:00
|
|
|
size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files) {
|
2012-05-08 20:29:28 +00:00
|
|
|
size_t foundEntries = 0;
|
2012-03-24 22:39:19 +00:00
|
|
|
#ifdef _WIN32
|
2012-05-08 20:29:28 +00:00
|
|
|
// Find the first file in the directory.
|
|
|
|
WIN32_FIND_DATA ffd;
|
2012-03-24 22:39:19 +00:00
|
|
|
#ifdef UNICODE
|
2012-05-08 20:29:28 +00:00
|
|
|
HANDLE hFind = FindFirstFile((std::wstring(directory) + "\\*").c_str(), &ffd);
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-05-08 20:29:28 +00:00
|
|
|
HANDLE hFind = FindFirstFile((std::string(directory) + "\\*").c_str(), &ffd);
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
2012-09-17 19:21:34 +00:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE) {
|
2012-05-08 20:29:28 +00:00
|
|
|
FindClose(hFind);
|
2012-09-17 19:21:34 +00:00
|
|
|
return 0;
|
2012-05-08 20:29:28 +00:00
|
|
|
}
|
|
|
|
// windows loop
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const std::string virtualName(ffd.cFileName);
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-07-16 22:00:48 +00:00
|
|
|
struct dirent dirent, *result = NULL;
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2012-07-16 22:00:48 +00:00
|
|
|
DIR *dirp = opendir(directory);
|
|
|
|
if (!dirp)
|
|
|
|
return 0;
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2012-07-16 22:00:48 +00:00
|
|
|
// non windows loop
|
|
|
|
while (!readdir_r(dirp, &dirent, &result) && result)
|
|
|
|
{
|
|
|
|
const std::string virtualName(result->d_name);
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
2012-05-08 20:29:28 +00:00
|
|
|
// check for "." and ".."
|
|
|
|
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
|
|
|
|
((virtualName[0] == '.') && (virtualName[1] == '.') &&
|
|
|
|
(virtualName[2] == '\0')))
|
|
|
|
continue;
|
2012-10-28 10:37:10 +00:00
|
|
|
|
|
|
|
// Remove dotfiles (should be made optional?)
|
|
|
|
if (virtualName[0] == '.')
|
|
|
|
continue;
|
2012-09-17 19:21:34 +00:00
|
|
|
FileInfo info;
|
2012-10-28 10:37:10 +00:00
|
|
|
info.name = virtualName;
|
|
|
|
info.fullName = std::string(directory) + "/" + virtualName;
|
|
|
|
info.isDirectory = isDirectory(info.fullName);
|
2012-09-17 19:21:34 +00:00
|
|
|
files->push_back(info);
|
2012-03-24 22:39:19 +00:00
|
|
|
#ifdef _WIN32
|
2012-05-08 20:29:28 +00:00
|
|
|
} while (FindNextFile(hFind, &ffd) != 0);
|
|
|
|
FindClose(hFind);
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-05-08 20:29:28 +00:00
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
closedir(dirp);
|
|
|
|
#endif
|
2012-05-08 20:29:28 +00:00
|
|
|
return foundEntries;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deleteFile(const char *file)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-05-08 20:29:28 +00:00
|
|
|
if (!DeleteFile(file)) {
|
|
|
|
ELOG("Error deleting %s: %i", file, GetLastError());
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-05-08 20:29:28 +00:00
|
|
|
int err = unlink(file);
|
|
|
|
if (err) {
|
|
|
|
ELOG("Error unlinking %s: %i", file, err);
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
2012-03-31 09:16:13 +00:00
|
|
|
#endif
|
2012-03-31 20:07:33 +00:00
|
|
|
|
|
|
|
std::string getDir(const std::string &path)
|
|
|
|
{
|
2012-10-28 10:37:10 +00:00
|
|
|
if (path == "/")
|
|
|
|
return path;
|
2012-05-08 20:29:28 +00:00
|
|
|
int n = path.size() - 1;
|
|
|
|
while (n >= 0 && path[n] != '\\' && path[n] != '/')
|
|
|
|
n--;
|
|
|
|
std::string cutpath = path.substr(0, n);
|
|
|
|
for (size_t i = 0; i < cutpath.size(); i++)
|
|
|
|
{
|
|
|
|
if (cutpath[i] == '\\') cutpath[i] = '/';
|
|
|
|
}
|
2012-10-28 10:47:26 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (!cutpath.size()) {
|
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
#endif
|
2012-05-08 20:29:28 +00:00
|
|
|
return cutpath;
|
2012-09-17 19:21:34 +00:00
|
|
|
}
|