Changed File from a class into a namespace, since all its methods were static.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@647 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Maarten ter Huurne 2008-09-23 22:23:38 +00:00
parent 5a3aee5118
commit 851dbcd7d6
2 changed files with 41 additions and 33 deletions

View File

@ -30,7 +30,10 @@
#include <stdlib.h> #include <stdlib.h>
#endif #endif
bool File::Exists(const char *filename) namespace File
{
bool Exists(const char *filename)
{ {
#ifdef _WIN32 #ifdef _WIN32
return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES; return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
@ -41,25 +44,25 @@ bool File::Exists(const char *filename)
#endif #endif
} }
bool File::IsDirectory(const char *filename) bool IsDirectory(const char *filename)
{ {
#ifdef _WIN32 #ifdef _WIN32
return (GetFileAttributes(filename) & FILE_ATTRIBUTE_DIRECTORY) != 0; return (GetFileAttributes(filename) & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else #else
struct stat file_info; struct stat file_info;
int result = stat(filename, &file_info); int result = stat(filename, &file_info);
if (result == 0) if (result == 0)
return S_ISDIR(file_info.st_mode); return S_ISDIR(file_info.st_mode);
else else
return false; return false;
#endif #endif
} }
bool File::Delete(const char *filename) bool Delete(const char *filename)
{ {
if (!File::Exists(filename)) if (!Exists(filename))
return false; return false;
if (File::IsDirectory(filename)) if (IsDirectory(filename))
return false; return false;
#ifdef _WIN32 #ifdef _WIN32
DeleteFile(filename); DeleteFile(filename);
@ -78,7 +81,7 @@ std::string SanitizePath(const char *filename)
return copy; return copy;
} }
void File::Launch(const char *filename) void Launch(const char *filename)
{ {
#ifdef _WIN32 #ifdef _WIN32
std::string win_filename = SanitizePath(filename); std::string win_filename = SanitizePath(filename);
@ -93,7 +96,7 @@ void File::Launch(const char *filename)
#endif #endif
} }
void File::Explore(const char *path) void Explore(const char *path)
{ {
#ifdef _WIN32 #ifdef _WIN32
std::string win_path = SanitizePath(path); std::string win_path = SanitizePath(path);
@ -109,7 +112,7 @@ void File::Explore(const char *path)
} }
// Returns true if successful, or path already exists. // Returns true if successful, or path already exists.
bool File::CreateDir(const char *path) bool CreateDir(const char *path)
{ {
#ifdef _WIN32 #ifdef _WIN32
if (::CreateDirectory(path, NULL)) if (::CreateDirectory(path, NULL))
@ -123,23 +126,25 @@ bool File::CreateDir(const char *path)
PanicAlert("Error creating directory: %i", error); PanicAlert("Error creating directory: %i", error);
return false; return false;
#else #else
if (mkdir(path, 0644) == 0) if (mkdir(path, 0644) == 0)
return true; return true;
int err = errno; int err = errno;
if (err == EEXIST) { if (err == EEXIST)
{
PanicAlert("%s already exists", path); PanicAlert("%s already exists", path);
return true; return true;
} }
PanicAlert("Error creating directory: %s", strerror(err)); PanicAlert("Error creating directory: %s", strerror(err));
return false; return false;
#endif #endif
} }
std::string File::GetUserDirectory() { std::string GetUserDirectory()
{
#ifdef _WIN32 #ifdef _WIN32
char path[MAX_PATH]; char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, path))) if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, path)))
@ -151,11 +156,11 @@ std::string File::GetUserDirectory() {
char *dir = getenv("HOME"); char *dir = getenv("HOME");
if (!dir) if (!dir)
return std::string(""); return std::string("");
return dir; return dir;
#endif #endif
} }
u64 File::GetSize(const char *filename) u64 GetSize(const char *filename)
{ {
FILE *f = fopen(filename, "rb"); FILE *f = fopen(filename, "rb");
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@ -163,3 +168,5 @@ u64 File::GetSize(const char *filename)
fclose(f); fclose(f);
return pos; return pos;
} }
} // namespace

View File

@ -22,17 +22,18 @@
#include "Common.h" #include "Common.h"
class File namespace File
{ {
public:
static bool Exists(const char *filename); bool Exists(const char *filename);
static void Launch(const char *filename); void Launch(const char *filename);
static void Explore(const char *path); void Explore(const char *path);
static bool IsDirectory(const char *filename); bool IsDirectory(const char *filename);
static bool CreateDir(const char *filename); bool CreateDir(const char *filename);
static bool Delete(const char *filename); bool Delete(const char *filename);
static u64 GetSize(const char *filename); u64 GetSize(const char *filename);
static std::string GetUserDirectory(); std::string GetUserDirectory();
};
} // namespace
#endif #endif