summaryrefslogtreecommitdiffstats
path: root/source/OSSupport
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2013-10-09 09:38:47 +0200
committermadmaxoft <github@xoft.cz>2013-10-09 09:38:47 +0200
commit2ff882f239f065585ad1b02f12b191bf99dd6626 (patch)
treea1d41562a820d965765b3ef22a0a892097fe27e7 /source/OSSupport
parentFixed warning in cFireSimulator. (diff)
downloadcuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.tar
cuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.tar.gz
cuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.tar.bz2
cuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.tar.lz
cuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.tar.xz
cuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.tar.zst
cuberite-2ff882f239f065585ad1b02f12b191bf99dd6626.zip
Diffstat (limited to 'source/OSSupport')
-rw-r--r--source/OSSupport/File.cpp57
-rw-r--r--source/OSSupport/File.h20
2 files changed, 72 insertions, 5 deletions
diff --git a/source/OSSupport/File.cpp b/source/OSSupport/File.cpp
index a4c9a22f4..16ec00e16 100644
--- a/source/OSSupport/File.cpp
+++ b/source/OSSupport/File.cpp
@@ -151,7 +151,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 +168,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 +189,6 @@ int cFile::Seek (int iPosition)
-/// Returns the current position (bytes from file start)
int cFile::Tell (void) const
{
ASSERT(IsOpen());
@@ -208,7 +205,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 +283,30 @@ 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:
+ ifstream src(a_SrcFileName, ios::binary);
+ ofstream dst(a_DstFileName, ios::binary);
+ if (dst.good())
+ {
+ dst << src.rdbuf();
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ #endif
+}
+
+
+
+
+
bool cFile::IsFolder(const AString & a_Path)
{
#ifdef _WIN32
@@ -302,6 +322,35 @@ bool cFile::IsFolder(const AString & a_Path)
+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;
+}
+
+
+
+
+
int cFile::Printf(const char * a_Fmt, ...)
{
AString buf;
diff --git a/source/OSSupport/File.h b/source/OSSupport/File.h
index d4ea0d3a8..f47bd4041 100644
--- a/source/OSSupport/File.h
+++ b/source/OSSupport/File.h
@@ -41,9 +41,14 @@ Usage:
+// tolua_begin
+
class cFile
{
public:
+
+ // tolua_end
+
#ifdef _WIN32
static const char PathSeparator = '\\';
#else
@@ -90,6 +95,8 @@ public:
/// Reads the file from current position till EOF into an AString; returns the number of bytes read or -1 for error
int ReadRestOfFile(AString & a_Contents);
+ // tolua_begin
+
/// Returns true if the file specified exists
static bool Exists(const AString & a_FileName);
@@ -99,9 +106,20 @@ public:
/// Renames a file, returns true if successful. May fail if dest already exists (libc-dependant)!
static bool Rename(const AString & a_OrigFileName, const AString & a_NewFileName);
+ /// Copies a file, returns true if successful.
+ static bool Copy(const AString & a_SrcFileName, const AString & a_DstFileName);
+
/// Returns true if the specified path is a folder
static bool IsFolder(const AString & a_Path);
+ /// Returns true if the specified path is a regular file
+ static bool IsFile(const AString & a_Path);
+
+ /// Returns the size of the file, or a negative number on error
+ static int GetSize(const AString & a_FileName);
+
+ // tolua_end
+
int Printf(const char * a_Fmt, ...);
private:
@@ -110,7 +128,7 @@ private:
#else
HANDLE m_File;
#endif
-} ;
+} ; // tolua_export