diff options
Diffstat (limited to 'source/OSSupport/File.cpp')
-rw-r--r-- | source/OSSupport/File.cpp | 91 |
1 files changed, 82 insertions, 9 deletions
diff --git a/source/OSSupport/File.cpp b/source/OSSupport/File.cpp index cc0916711..d2eea498a 100644 --- a/source/OSSupport/File.cpp +++ b/source/OSSupport/File.cpp @@ -6,13 +6,12 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "File.h" +#include <fstream> - -/// Simple constructor - creates an unopened file object, use Open() to open / create a real file cFile::cFile(void) : #ifdef USE_STDIO_FILE m_File(NULL) @@ -27,7 +26,6 @@ cFile::cFile(void) : -/// Constructs and opens / creates the file specified, use IsOpen() to check for success cFile::cFile(const AString & iFileName, eMode iMode) : #ifdef USE_STDIO_FILE m_File(NULL) @@ -42,7 +40,6 @@ cFile::cFile(const AString & iFileName, eMode iMode) : -/// Auto-closes the file, if open cFile::~cFile() { if (IsOpen()) @@ -134,7 +131,6 @@ bool cFile::IsEOF(void) const -/// Reads up to iNumBytes bytes into iBuffer, returns the number of bytes actually read, or -1 on failure; asserts if not open int cFile::Read (void * iBuffer, int iNumBytes) { ASSERT(IsOpen()); @@ -151,7 +147,6 @@ int cFile::Read (void * iBuffer, int iNumBytes) -/// Writes up to iNumBytes bytes from iBuffer, returns the number of bytes actually written, or -1 on failure; asserts if not open int cFile::Write(const void * iBuffer, int iNumBytes) { ASSERT(IsOpen()); @@ -169,7 +164,6 @@ int cFile::Write(const void * iBuffer, int iNumBytes) -/// Seeks to iPosition bytes from file start, returns old position or -1 for failure int cFile::Seek (int iPosition) { ASSERT(IsOpen()); @@ -191,7 +185,6 @@ int cFile::Seek (int iPosition) -/// Returns the current position (bytes from file start) int cFile::Tell (void) const { ASSERT(IsOpen()); @@ -208,7 +201,6 @@ int cFile::Tell (void) const -/// Returns the size of file, in bytes, or -1 for failure; asserts if not open int cFile::GetSize(void) const { ASSERT(IsOpen()); @@ -287,6 +279,87 @@ bool cFile::Rename(const AString & a_OrigFileName, const AString & a_NewFileName +bool cFile::Copy(const AString & a_SrcFileName, const AString & a_DstFileName) +{ + #ifdef _WIN32 + return (CopyFile(a_SrcFileName.c_str(), a_DstFileName.c_str(), true) != 0); + #else + // Other OSs don't have a direct CopyFile equivalent, do it the harder way: + std::ifstream src(a_SrcFileName.c_str(), std::ios::binary); + std::ofstream dst(a_DstFileName.c_str(), std::ios::binary); + if (dst.good()) + { + dst << src.rdbuf(); + return true; + } + else + { + return false; + } + #endif +} + + + + + +bool cFile::IsFolder(const AString & a_Path) +{ + #ifdef _WIN32 + DWORD FileAttrib = GetFileAttributes(a_Path.c_str()); + return ((FileAttrib != INVALID_FILE_ATTRIBUTES) && ((FileAttrib & FILE_ATTRIBUTE_DIRECTORY) != 0)); + #else + struct stat st; + return ((stat(a_Path.c_str(), &st) == 0) && S_ISDIR(st.st_mode)); + #endif +} + + + + + +bool cFile::IsFile(const AString & a_Path) +{ + #ifdef _WIN32 + DWORD FileAttrib = GetFileAttributes(a_Path.c_str()); + return ((FileAttrib != INVALID_FILE_ATTRIBUTES) && ((FileAttrib & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0)); + #else + struct stat st; + return ((stat(a_Path.c_str(), &st) == 0) && S_ISREG(st.st_mode)); + #endif +} + + + + + +int cFile::GetSize(const AString & a_FileName) +{ + struct stat st; + if (stat(a_FileName.c_str(), &st) == 0) + { + return st.st_size; + } + return -1; +} + + + + + +bool cFile::CreateFolder(const AString & a_FolderPath) +{ + #ifdef _WIN32 + return (CreateDirectory(a_FolderPath.c_str(), NULL) != 0); + #else + return (mkdir(a_FolderPath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0); + #endif +} + + + + + int cFile::Printf(const char * a_Fmt, ...) { AString buf; |