Replace some LibraySys usage with AMTL primitives

This commit is contained in:
Arkshine 2015-09-30 21:02:19 +02:00
parent f976861e21
commit f22dc769f4
2 changed files with 22 additions and 192 deletions

View File

@ -9,6 +9,9 @@
#include "CLibrarySys.h" #include "CLibrarySys.h"
#include <amxmodx.h> #include <amxmodx.h>
#include <amtl/os/am-fsutil.h>
#include <amtl/os/am-path.h>
#include <amtl/os/am-system-errors.h>
LibrarySystem g_LibSys; LibrarySystem g_LibSys;
@ -100,9 +103,9 @@ bool CDirectory::IsEntryDirectory()
#elif defined PLATFORM_POSIX #elif defined PLATFORM_POSIX
char temppath[PLATFORM_MAX_PATH]; char temppath[PLATFORM_MAX_PATH];
UTIL_Format(temppath, sizeof(temppath) - 1, "%s/%s", m_origpath, GetEntryName()); UTIL_Format(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
return g_LibSys.IsPathDirectory(temppath); return ke::file::IsDirectory(temppath);
#endif #endif
} }
@ -116,9 +119,9 @@ bool CDirectory::IsEntryFile()
#elif defined PLATFORM_POSIX #elif defined PLATFORM_POSIX
char temppath[PLATFORM_MAX_PATH]; char temppath[PLATFORM_MAX_PATH];
UTIL_Format(temppath, sizeof(temppath) - 1, "%s/%s", m_origpath, GetEntryName()); UTIL_Format(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
return g_LibSys.IsPathFile(temppath); return ke::file::IsDirectory(temppath);
#endif #endif
} }
@ -158,26 +161,8 @@ bool CDirectory::IsValid()
/* Library Code */ /* Library Code */
/****************/ /****************/
CLibrary::~CLibrary() CLibrary::CLibrary(ke::Ref<ke::SharedLib> lib) : lib_(lib)
{ {}
if (m_lib)
{
#if defined PLATFORM_WINDOWS
FreeLibrary(m_lib);
#elif defined PLATFORM_POSIX
dlclose(m_lib);
#endif
m_lib = nullptr;
}
}
CLibrary::CLibrary(LibraryHandle me)
{
m_lib = me;
}
void CLibrary::CloseLibrary() void CLibrary::CloseLibrary()
{ {
@ -186,14 +171,7 @@ void CLibrary::CloseLibrary()
void *CLibrary::GetSymbolAddress(const char* symname) void *CLibrary::GetSymbolAddress(const char* symname)
{ {
#if defined PLATFORM_WINDOWS return lib_->lookup(symname);
return GetProcAddress(m_lib, symname);
#elif defined PLATFORM_POSIX
return dlsym(m_lib, symname);
#endif
} }
@ -203,83 +181,17 @@ void *CLibrary::GetSymbolAddress(const char* symname)
bool LibrarySystem::PathExists(const char *path) bool LibrarySystem::PathExists(const char *path)
{ {
#if defined PLATFORM_WINDOWS return ke::file::PathExists(path);
DWORD attr = GetFileAttributesA(path);
return (attr != INVALID_FILE_ATTRIBUTES);
#elif defined PLATFORM_POSIX
struct stat s;
return (stat(path, &s) == 0);
#endif
} }
bool LibrarySystem::IsPathFile(const char* path) bool LibrarySystem::IsPathFile(const char* path)
{ {
#if defined PLATFORM_WINDOWS return ke::file::IsFile(path);
DWORD attr = GetFileAttributes(path);
if (attr == INVALID_FILE_ATTRIBUTES)
{
return false;
}
if (attr & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE))
{
return false;
}
return true;
#elif defined PLATFORM_POSIX
struct stat s;
if (stat(path, &s) != 0)
{
return false;
}
return S_ISREG(s.st_mode) ? true : false;
#endif
} }
bool LibrarySystem::IsPathDirectory(const char* path) bool LibrarySystem::IsPathDirectory(const char* path)
{ {
#if defined PLATFORM_WINDOWS return ke::file::IsDirectory(path);
DWORD attr = GetFileAttributes(path);
if (attr == INVALID_FILE_ATTRIBUTES)
{
return false;
}
if (attr & FILE_ATTRIBUTE_DIRECTORY)
{
return true;
}
#elif defined PLATFORM_POSIX
struct stat s;
if (stat(path, &s) != 0)
{
return false;
}
if (S_ISDIR(s.st_mode))
{
return true;
}
#endif
return false;
} }
CDirectory *LibrarySystem::OpenDirectory(const char* path) CDirectory *LibrarySystem::OpenDirectory(const char* path)
@ -297,22 +209,10 @@ CDirectory *LibrarySystem::OpenDirectory(const char* path)
CLibrary* LibrarySystem::OpenLibrary(const char* path, char* error, size_t maxlength) CLibrary* LibrarySystem::OpenLibrary(const char* path, char* error, size_t maxlength)
{ {
#if defined PLATFORM_WINDOWS ke::Ref<ke::SharedLib> lib = ke::SharedLib::Open(path, error, maxlength);
LibraryHandle lib = LoadLibrary(path);
#elif defined PLATFORM_POSIX
LibraryHandle lib = dlopen(path, RTLD_NOW);
#endif
if (!lib) if (!lib)
{ {
if (error && maxlength)
{
GetLoaderError(error, maxlength);
}
return nullptr; return nullptr;
} }
@ -335,48 +235,13 @@ void LibrarySystem::GetPlatformErrorEx(int code, char* error, size_t maxlength)
{ {
if (error && maxlength) if (error && maxlength)
{ {
#if defined PLATFORM_WINDOWS ke::FormatSystemErrorCode(code, error, maxlength);
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
(DWORD)code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)error,
maxlength,
nullptr) == 0)
{
UTIL_Format(error, maxlength, "error code %08x", code);
}
#elif defined PLATFORM_LINUX
const char *ae = strerror_r(code, error, maxlength);
if (ae != error)
{
UTIL_Format(error, maxlength, "%s", ae);
}
#elif defined PLATFORM_POSIX
strerror_r(code, error, maxlength);
#endif
} }
} }
void LibrarySystem::GetLoaderError(char* buffer, size_t maxlength) void LibrarySystem::GetLoaderError(char* buffer, size_t maxlength)
{ {
#if defined PLATFORM_WINDOWS ke::FormatSystemError(buffer, maxlength);
GetPlatformError(buffer, maxlength);
#elif defined PLATFORM_POSIX
if (buffer && maxlength)
{
strncopy(buffer, dlerror(), maxlength);
}
#endif
} }
void LibrarySystem::CloseDirectory(CDirectory *dir) void LibrarySystem::CloseDirectory(CDirectory *dir)
@ -388,23 +253,9 @@ size_t LibrarySystem::PathFormat(char* buffer, size_t len, const char* fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
size_t mylen = vsnprintf(buffer, len, fmt, ap); size_t mylen = ke::path::FormatVa(buffer, len, fmt, ap);
va_end(ap); va_end(ap);
if (mylen >= len)
{
mylen = len - 1;
buffer[mylen] = '\0';
}
for (size_t i = 0; i < mylen; i++)
{
if (buffer[i] == PLATFORM_SEP_ALTCHAR)
{
buffer[i] = PLATFORM_SEP_CHAR;
}
}
return mylen; return mylen;
} }
@ -414,23 +265,9 @@ char* LibrarySystem::PathFormat(const char* fmt, ...)
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
size_t mylen = vsnprintf(buffer, sizeof(buffer), fmt, ap); ke::path::FormatVa(buffer, sizeof(buffer), fmt, ap);
va_end(ap); va_end(ap);
if (mylen >= sizeof(buffer))
{
mylen = sizeof(buffer) - 1;
buffer[mylen] = '\0';
}
for (size_t i = 0; i < mylen; i++)
{
if (buffer[i] == PLATFORM_SEP_ALTCHAR)
{
buffer[i] = PLATFORM_SEP_CHAR;
}
}
return buffer; return buffer;
} }
@ -466,14 +303,7 @@ const char* LibrarySystem::GetFileExtension(const char* filename)
bool LibrarySystem::CreateFolder(const char* path) bool LibrarySystem::CreateFolder(const char* path)
{ {
#if defined PLATFORM_WINDOWS return ke::file::CreateDirectory(path, 0775);
return (mkdir(path) != -1);
#elif defined PLATFORM_POSIX
return (mkdir(path, 0775) != -1);
#endif
} }
size_t LibrarySystem::GetFileFromPath(char* buffer, size_t maxlength, const char* path) size_t LibrarySystem::GetFileFromPath(char* buffer, size_t maxlength, const char* path)

View File

@ -13,6 +13,7 @@
#include "amx.h" // cell #include "amx.h" // cell
#include <interface.h> // Interface (HLSDK) #include <interface.h> // Interface (HLSDK)
#include <amtl/am-utility.h> // AutoPtr #include <amtl/am-utility.h> // AutoPtr
#include <amtl/os/am-shared-library.h>
#define PLATFORM_WINDOWNS_NAME "windows" #define PLATFORM_WINDOWNS_NAME "windows"
#define PLATFORM_LINUX_NAME "linux" #define PLATFORM_LINUX_NAME "linux"
@ -124,8 +125,7 @@ class CLibrary
{ {
public: public:
CLibrary(LibraryHandle me); CLibrary(ke::Ref<ke::SharedLib> lib);
~CLibrary();
public: public:
@ -134,7 +134,7 @@ class CLibrary
private: private:
LibraryHandle m_lib; ke::Ref<ke::SharedLib> lib_;
}; };
class LibrarySystem class LibrarySystem