From 2ff882f239f065585ad1b02f12b191bf99dd6626 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 9 Oct 2013 09:38:47 +0200 Subject: Added static cFile functions to Lua API. --- source/OSSupport/File.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++---- source/OSSupport/File.h | 20 ++++++++++++++++- 2 files changed, 72 insertions(+), 5 deletions(-) (limited to 'source/OSSupport') 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 -- cgit v1.2.3