diff options
Diffstat (limited to 'source/OSSupport')
-rw-r--r-- | source/OSSupport/File.cpp | 91 | ||||
-rw-r--r-- | source/OSSupport/File.h | 30 | ||||
-rw-r--r-- | source/OSSupport/IsThread.cpp | 14 | ||||
-rw-r--r-- | source/OSSupport/IsThread.h | 3 | ||||
-rw-r--r-- | source/OSSupport/ListenThread.cpp | 5 | ||||
-rw-r--r-- | source/OSSupport/MakeDir.cpp | 25 | ||||
-rw-r--r-- | source/OSSupport/MakeDir.h | 16 |
7 files changed, 117 insertions, 67 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; diff --git a/source/OSSupport/File.h b/source/OSSupport/File.h index 8a057afa8..cfb3a2019 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,14 +95,33 @@ 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); /// Deletes a file, returns true if successful static bool Delete(const AString & a_FileName); - /// 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); + /// Renames a file or folder, returns true if successful. May fail if dest already exists (libc-dependant)! + static bool Rename(const AString & a_OrigPath, const AString & a_NewPath); + + /// 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); + + /// Creates a new folder with the specified name. Returns true if successful. Path may be relative or absolute + static bool CreateFolder(const AString & a_FolderPath); + + // tolua_end int Printf(const char * a_Fmt, ...); @@ -107,7 +131,7 @@ private: #else HANDLE m_File; #endif -} ; +} ; // tolua_export diff --git a/source/OSSupport/IsThread.cpp b/source/OSSupport/IsThread.cpp index d5fbfcf19..e1ef84c17 100644 --- a/source/OSSupport/IsThread.cpp +++ b/source/OSSupport/IsThread.cpp @@ -53,11 +53,7 @@ static void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName) cIsThread::cIsThread(const AString & iThreadName) : m_ThreadName(iThreadName), m_ShouldTerminate(false), - #ifdef _WIN32 - m_Handle(NULL) - #else // _WIN32 - m_HasStarted(false) - #endif // else _WIN32 + m_Handle(NULL) { } @@ -77,9 +73,9 @@ cIsThread::~cIsThread() bool cIsThread::Start(void) { + ASSERT(m_Handle == NULL); // Has already started one thread? + #ifdef _WIN32 - ASSERT(m_Handle == NULL); // Has already started one thread? - // Create the thread suspended, so that the mHandle variable is valid in the thread procedure DWORD ThreadID = 0; m_Handle = CreateThread(NULL, 0, thrExecute, this, CREATE_SUSPENDED, &ThreadID); @@ -99,14 +95,11 @@ bool cIsThread::Start(void) #endif // _DEBUG and _MSC_VER #else // _WIN32 - ASSERT(!m_HasStarted); - if (pthread_create(&m_Handle, NULL, thrExecute, this)) { LOGERROR("ERROR: Could not create thread \"%s\", !", m_ThreadName.c_str()); return false; } - m_HasStarted = true; #endif // else _WIN32 return true; @@ -158,7 +151,6 @@ bool cIsThread::Wait(void) LOGD("Thread %s finished", m_ThreadName.c_str()); #endif // LOGD - m_HasStarted = false; return (res == 0); #endif // else _WIN32 } diff --git a/source/OSSupport/IsThread.h b/source/OSSupport/IsThread.h index 9b7f0b73e..2ea8bf6f9 100644 --- a/source/OSSupport/IsThread.h +++ b/source/OSSupport/IsThread.h @@ -48,7 +48,7 @@ public: /// Returns the OS-dependent thread ID for the caller's thread static unsigned long GetCurrentID(void); -private: +protected: AString m_ThreadName; #ifdef _WIN32 @@ -66,7 +66,6 @@ private: #else // _WIN32 pthread_t m_Handle; - bool m_HasStarted; static void * thrExecute(void * a_Param) { diff --git a/source/OSSupport/ListenThread.cpp b/source/OSSupport/ListenThread.cpp index 0890aabc8..ba3198764 100644 --- a/source/OSSupport/ListenThread.cpp +++ b/source/OSSupport/ListenThread.cpp @@ -224,7 +224,10 @@ void cListenThread::Execute(void) if (itr->IsValid() && FD_ISSET(itr->GetSocket(), &fdRead)) { cSocket Client = (m_Family == cSocket::IPv4) ? itr->AcceptIPv4() : itr->AcceptIPv6(); - m_Callback.OnConnectionAccepted(Client); + if (Client.IsValid()) + { + m_Callback.OnConnectionAccepted(Client); + } } } // for itr - m_Sockets[] } // while (!m_ShouldTerminate) diff --git a/source/OSSupport/MakeDir.cpp b/source/OSSupport/MakeDir.cpp deleted file mode 100644 index 10ccfe9ec..000000000 --- a/source/OSSupport/MakeDir.cpp +++ /dev/null @@ -1,25 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#include "MakeDir.h" - - - - - -void cMakeDir::MakeDir(const AString & a_Directory) -{ -#ifdef _WIN32 - SECURITY_ATTRIBUTES Attrib; - Attrib.nLength = sizeof(SECURITY_ATTRIBUTES); - Attrib.lpSecurityDescriptor = NULL; - Attrib.bInheritHandle = false; - ::CreateDirectory( (FILE_IO_PREFIX + a_Directory).c_str(), &Attrib); -#else - mkdir( (FILE_IO_PREFIX + a_Directory).c_str(), S_IRWXU | S_IRWXG | S_IRWXO); -#endif -} - - - - diff --git a/source/OSSupport/MakeDir.h b/source/OSSupport/MakeDir.h deleted file mode 100644 index e66cf1071..000000000 --- a/source/OSSupport/MakeDir.h +++ /dev/null @@ -1,16 +0,0 @@ - -#pragma once - - - - - -class cMakeDir -{ -public: - static void MakeDir(const AString & a_Directory); -}; - - - - |