1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/systems/LibrarySys.cpp Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,428 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include <sys/stat.h>
1.36 +#include <stdio.h>
1.37 +#include <stdarg.h>
1.38 +#include <string.h>
1.39 +#include <sm_platform.h>
1.40 +#include "sm_stringutil.h"
1.41 +#include "LibrarySys.h"
1.42 +
1.43 +LibrarySystem g_LibSys;
1.44 +
1.45 +CLibrary::~CLibrary()
1.46 +{
1.47 + if (m_lib)
1.48 + {
1.49 +#if defined PLATFORM_WINDOWS
1.50 + FreeLibrary(m_lib);
1.51 +#elif defined PLATFORM_POSIX
1.52 + dlclose(m_lib);
1.53 +#endif
1.54 + m_lib = NULL;
1.55 + }
1.56 +}
1.57 +
1.58 +CLibrary::CLibrary(LibraryHandle me)
1.59 +{
1.60 + m_lib = me;
1.61 +}
1.62 +
1.63 +void CLibrary::CloseLibrary()
1.64 +{
1.65 + delete this;
1.66 +}
1.67 +
1.68 +void *CLibrary::GetSymbolAddress(const char *symname)
1.69 +{
1.70 +#if defined PLATFORM_WINDOWS
1.71 + return GetProcAddress(m_lib, symname);
1.72 +#elif defined PLATFORM_POSIX
1.73 + return dlsym(m_lib, symname);
1.74 +#endif
1.75 +}
1.76 +
1.77 +
1.78 +/********************
1.79 + ** Directory Code **
1.80 + ********************/
1.81 +
1.82 +
1.83 +CDirectory::CDirectory(const char *path)
1.84 +{
1.85 +#if defined PLATFORM_WINDOWS
1.86 + char newpath[PLATFORM_MAX_PATH];
1.87 + snprintf(newpath, sizeof(newpath), "%s\\*.*", path);
1.88 + m_dir = FindFirstFileA(newpath, &m_fd);
1.89 + if (!IsValid())
1.90 + {
1.91 + m_fd.cFileName[0] = '\0';
1.92 + }
1.93 +#elif defined PLATFORM_POSIX
1.94 + m_dir = opendir(path);
1.95 + if (IsValid())
1.96 + {
1.97 + /* :TODO: we need to read past "." and ".."! */
1.98 + ep = readdir(m_dir);
1.99 + snprintf(m_origpath, PLATFORM_MAX_PATH, "%s", path);
1.100 + } else {
1.101 + ep = NULL;
1.102 + }
1.103 +#endif
1.104 +}
1.105 +
1.106 +CDirectory::~CDirectory()
1.107 +{
1.108 + if (IsValid())
1.109 + {
1.110 +#if defined PLATFORM_WINDOWS
1.111 + FindClose(m_dir);
1.112 +#elif defined PLATFORM_POSIX
1.113 + closedir(m_dir);
1.114 +#endif
1.115 + }
1.116 +}
1.117 +
1.118 +void CDirectory::NextEntry()
1.119 +{
1.120 +#if defined PLATFORM_WINDOWS
1.121 + if (FindNextFile(m_dir, &m_fd) == 0)
1.122 + {
1.123 + FindClose(m_dir);
1.124 + m_dir = INVALID_HANDLE_VALUE;
1.125 + }
1.126 +#elif defined PLATFORM_POSIX
1.127 + if ((ep=readdir(m_dir)) == NULL)
1.128 + {
1.129 + closedir(m_dir);
1.130 + m_dir = NULL;
1.131 + }
1.132 +#endif
1.133 +}
1.134 +
1.135 +bool CDirectory::IsEntryValid()
1.136 +{
1.137 + return IsValid();
1.138 +}
1.139 +
1.140 +bool CDirectory::IsEntryDirectory()
1.141 +{
1.142 +#if defined PLATFORM_WINDOWS
1.143 + return ((m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
1.144 +#elif defined PLATFORM_LINUX
1.145 + char temppath[PLATFORM_MAX_PATH];
1.146 + snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
1.147 + return g_LibSys.IsPathDirectory(temppath);
1.148 +#endif
1.149 +}
1.150 +
1.151 +bool CDirectory::IsEntryFile()
1.152 +{
1.153 +#if defined PLATFORM_WINDOWS
1.154 + return !(m_fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE));
1.155 +#elif defined PLATFORM_POSIX
1.156 + char temppath[PLATFORM_MAX_PATH];
1.157 + snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
1.158 + return g_LibSys.IsPathFile(temppath);
1.159 +#endif
1.160 +}
1.161 +
1.162 +const char *CDirectory::GetEntryName()
1.163 +{
1.164 +#if defined PLATFORM_WINDOWS
1.165 + return m_fd.cFileName;
1.166 +#elif defined PLATFORM_LINUX
1.167 + return ep ? ep->d_name : "";
1.168 +#endif
1.169 +}
1.170 +
1.171 +bool CDirectory::MoreFiles()
1.172 +{
1.173 + return IsValid();
1.174 +}
1.175 +
1.176 +bool CDirectory::IsValid()
1.177 +{
1.178 +#if defined PLATFORM_WINDOWS
1.179 + return (m_dir != INVALID_HANDLE_VALUE);
1.180 +#elif defined PLATFORM_LINUX
1.181 + return (m_dir != NULL);
1.182 +#endif
1.183 +}
1.184 +
1.185 +
1.186 +/*************************
1.187 + ** Library System Code **
1.188 + *************************/
1.189 +
1.190 +
1.191 +bool LibrarySystem::PathExists(const char *path)
1.192 +{
1.193 +#if defined PLATFORM_WINDOWS
1.194 + DWORD attr = GetFileAttributesA(path);
1.195 +
1.196 + return (attr != INVALID_FILE_ATTRIBUTES);
1.197 +#elif defined PLATFORM_POSIX
1.198 + struct stat s;
1.199 +
1.200 + return (stat(path, &s) == 0);
1.201 +#endif
1.202 +}
1.203 +
1.204 +bool LibrarySystem::IsPathFile(const char *path)
1.205 +{
1.206 +#if defined PLATFORM_WINDOWS
1.207 + DWORD attr = GetFileAttributes(path);
1.208 +
1.209 + if (attr == INVALID_FILE_ATTRIBUTES)
1.210 + {
1.211 + return false;
1.212 + }
1.213 +
1.214 + if (attr & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE))
1.215 + {
1.216 + return false;
1.217 + }
1.218 +
1.219 + return true;
1.220 +#elif defined PLATFORM_LINUX
1.221 + struct stat s;
1.222 +
1.223 + if (stat(path, &s) != 0)
1.224 + {
1.225 + return false;
1.226 + }
1.227 +
1.228 + return S_ISREG(s.st_mode) ? true : false;
1.229 +#endif
1.230 +}
1.231 +
1.232 +bool LibrarySystem::IsPathDirectory(const char *path)
1.233 +{
1.234 +#if defined PLATFORM_WINDOWS
1.235 + DWORD attr = GetFileAttributes(path);
1.236 +
1.237 + if (attr == INVALID_FILE_ATTRIBUTES)
1.238 + {
1.239 + return false;
1.240 + }
1.241 +
1.242 + if (attr & FILE_ATTRIBUTE_DIRECTORY)
1.243 + {
1.244 + return true;
1.245 + }
1.246 +
1.247 +#elif defined PLATFORM_LINUX
1.248 + struct stat s;
1.249 +
1.250 + if (stat(path, &s) != 0)
1.251 + {
1.252 + return false;
1.253 + }
1.254 +
1.255 + if (S_ISDIR(s.st_mode))
1.256 + {
1.257 + return true;
1.258 + }
1.259 +#endif
1.260 +
1.261 + return false;
1.262 +}
1.263 +
1.264 +IDirectory *LibrarySystem::OpenDirectory(const char *path)
1.265 +{
1.266 + CDirectory *dir = new CDirectory(path);
1.267 +
1.268 + if (!dir->IsValid())
1.269 + {
1.270 + delete dir;
1.271 + return NULL;
1.272 + }
1.273 +
1.274 + return dir;
1.275 +}
1.276 +
1.277 +void LibrarySystem::GetPlatformError(char *error, size_t maxlength)
1.278 +{
1.279 + if (error && maxlength)
1.280 + {
1.281 +#if defined PLATFORM_WINDOWS
1.282 + DWORD dw = GetLastError();
1.283 + FormatMessageA(
1.284 + FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
1.285 + NULL,
1.286 + dw,
1.287 + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1.288 + (LPSTR)error,
1.289 + maxlength,
1.290 + NULL);
1.291 +#elif defined PLATFORM_POSIX
1.292 + snprintf(error, maxlength, "%s", strerror(errno));
1.293 +#endif
1.294 + }
1.295 +}
1.296 +
1.297 +void LibrarySystem::CloseDirectory(IDirectory *dir)
1.298 +{
1.299 + delete dir;
1.300 +}
1.301 +
1.302 +ILibrary *LibrarySystem::OpenLibrary(const char *path, char *error, size_t maxlength)
1.303 +{
1.304 + LibraryHandle lib;
1.305 +#if defined PLATFORM_WINDOWS
1.306 + lib = LoadLibraryA(path);
1.307 + if (!lib)
1.308 + {
1.309 + GetPlatformError(error, maxlength);
1.310 + return false;
1.311 + }
1.312 +#elif defined PLATFORM_POSIX
1.313 + lib = dlopen(path, RTLD_NOW);
1.314 + if (!lib)
1.315 + {
1.316 + GetPlatformError(error, maxlength);
1.317 + return false;
1.318 + }
1.319 +#endif
1.320 +
1.321 + return new CLibrary(lib);
1.322 +}
1.323 +
1.324 +size_t LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1.325 +{
1.326 + va_list ap;
1.327 + va_start(ap, fmt);
1.328 + size_t mylen = vsnprintf(buffer, len, fmt, ap);
1.329 + va_end(ap);
1.330 +
1.331 + if (mylen >= len)
1.332 + {
1.333 + mylen = len - 1;
1.334 + buffer[mylen] = '\0';
1.335 + }
1.336 +
1.337 + for (size_t i=0; i<mylen; i++)
1.338 + {
1.339 + if (buffer[i] == PLATFORM_SEP_ALTCHAR)
1.340 + {
1.341 + buffer[i] = PLATFORM_SEP_CHAR;
1.342 + }
1.343 + }
1.344 +
1.345 + return mylen;
1.346 +}
1.347 +
1.348 +const char *LibrarySystem::GetFileExtension(const char *filename)
1.349 +{
1.350 + size_t end = strlen(filename) - 1;
1.351 +
1.352 + for (size_t i = end; i >= 0; i--)
1.353 + {
1.354 + if (i > end)
1.355 + {
1.356 + break;
1.357 + }
1.358 +
1.359 + if (filename[i] == PLATFORM_SEP_CHAR || filename[i] == PLATFORM_SEP_ALTCHAR)
1.360 + {
1.361 + break;
1.362 + }
1.363 +
1.364 + if (filename[i] == '.' && i != end)
1.365 + {
1.366 + return &filename[++i];
1.367 + }
1.368 + }
1.369 +
1.370 + return NULL;
1.371 +}
1.372 +
1.373 +bool LibrarySystem::CreateFolder(const char *path)
1.374 +{
1.375 +#if defined PLATFORM_WINDOWS
1.376 + return (mkdir(path) != -1);
1.377 +#elif defined PLATFORM_POSIX
1.378 + return (mkdir(path, 0775) != -1);
1.379 +#endif
1.380 +}
1.381 +
1.382 +size_t LibrarySystem::GetFileFromPath(char *buffer, size_t maxlength, const char *path)
1.383 +{
1.384 + size_t length = strlen(path);
1.385 +
1.386 + for (size_t i = length - 1;
1.387 + i >= 0 && i <= length - 1;
1.388 + i--)
1.389 + {
1.390 + if (path[i] == '/'
1.391 +#if defined PLATFORM_WINDOWS
1.392 + || path[i] == '\\'
1.393 +#endif
1.394 + )
1.395 + {
1.396 + return UTIL_Format(buffer, maxlength, "%s", &path[i+1]);
1.397 + }
1.398 + }
1.399 +
1.400 + /* We scanned and found no path separator */
1.401 + return UTIL_Format(buffer, maxlength, "%s", path);
1.402 +}
1.403 +
1.404 +bool LibrarySystem::FileTime(const char *path, FileTimeType type, time_t *pTime)
1.405 +{
1.406 +#ifdef PLATFORM_WINDOWS
1.407 + struct _stat s;
1.408 + if (_stat(path, &s) != 0)
1.409 +#elif defined PLATFORM_POSIX
1.410 + struct stat s;
1.411 + if (stat(path, &s) != 0)
1.412 +#endif
1.413 + {
1.414 + return false;
1.415 + }
1.416 +
1.417 + if (type == FileTime_LastAccess)
1.418 + {
1.419 + *pTime = s.st_atime;
1.420 + }
1.421 + else if (type == FileTime_Created)
1.422 + {
1.423 + *pTime = s.st_ctime;
1.424 + }
1.425 + else if (type == FileTime_LastChange)
1.426 + {
1.427 + *pTime = s.st_mtime;
1.428 + }
1.429 +
1.430 + return true;
1.431 +}