core/systems/LibrarySys.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/systems/LibrarySys.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,398 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2007 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 <stdio.h>
    1.36 -#include <stdarg.h>
    1.37 -#include <string.h>
    1.38 -#include <sm_platform.h>
    1.39 -#include "sm_stringutil.h"
    1.40 -#include "LibrarySys.h"
    1.41 -
    1.42 -LibrarySystem g_LibSys;
    1.43 -
    1.44 -CLibrary::~CLibrary()
    1.45 -{
    1.46 -	if (m_lib)
    1.47 -	{
    1.48 -#if defined PLATFORM_WINDOWS
    1.49 -		FreeLibrary(m_lib);
    1.50 -#elif defined PLATFORM_POSIX
    1.51 -		dlclose(m_lib);
    1.52 -#endif
    1.53 -		m_lib = NULL;
    1.54 -	}
    1.55 -}
    1.56 -
    1.57 -CLibrary::CLibrary(LibraryHandle me)
    1.58 -{
    1.59 -	m_lib = me;
    1.60 -}
    1.61 -
    1.62 -void CLibrary::CloseLibrary()
    1.63 -{
    1.64 -	delete this;
    1.65 -}
    1.66 -
    1.67 -void *CLibrary::GetSymbolAddress(const char *symname)
    1.68 -{
    1.69 -#if defined PLATFORM_WINDOWS
    1.70 -	return GetProcAddress(m_lib, symname);
    1.71 -#elif defined PLATFORM_POSIX
    1.72 -	return dlsym(m_lib, symname);
    1.73 -#endif
    1.74 -}
    1.75 -
    1.76 -
    1.77 -/********************
    1.78 - ** Directory Code **
    1.79 - ********************/
    1.80 -
    1.81 -
    1.82 -CDirectory::CDirectory(const char *path)
    1.83 -{
    1.84 -#if defined PLATFORM_WINDOWS
    1.85 -	char newpath[PLATFORM_MAX_PATH];
    1.86 -	snprintf(newpath, sizeof(newpath), "%s\\*.*", path);
    1.87 -	m_dir = FindFirstFileA(newpath, &m_fd);
    1.88 -	if (!IsValid())
    1.89 -	{
    1.90 -		m_fd.cFileName[0] = '\0';
    1.91 -	}
    1.92 -#elif defined PLATFORM_POSIX
    1.93 -	m_dir = opendir(path);
    1.94 -	if (IsValid())
    1.95 -	{
    1.96 -		/* :TODO: we need to read past "." and ".."! */
    1.97 -		ep = readdir(m_dir);
    1.98 -		snprintf(m_origpath, PLATFORM_MAX_PATH, "%s", path);
    1.99 -	} else {
   1.100 -		ep = NULL;
   1.101 -	}
   1.102 -#endif
   1.103 -}
   1.104 -
   1.105 -CDirectory::~CDirectory()
   1.106 -{
   1.107 -	if (IsValid())
   1.108 -	{
   1.109 -#if defined PLATFORM_WINDOWS
   1.110 -		FindClose(m_dir);
   1.111 -#elif defined PLATFORM_POSIX
   1.112 -		closedir(m_dir);
   1.113 -#endif
   1.114 -	}
   1.115 -}
   1.116 -
   1.117 -void CDirectory::NextEntry()
   1.118 -{
   1.119 -#if defined PLATFORM_WINDOWS
   1.120 -	if (FindNextFile(m_dir, &m_fd) == 0)
   1.121 -	{
   1.122 -		FindClose(m_dir);
   1.123 -		m_dir = INVALID_HANDLE_VALUE;
   1.124 -	}
   1.125 -#elif defined PLATFORM_POSIX
   1.126 -	if ((ep=readdir(m_dir)) == NULL)
   1.127 -	{
   1.128 -		closedir(m_dir);
   1.129 -		m_dir = NULL;
   1.130 -	}
   1.131 -#endif
   1.132 -}
   1.133 -
   1.134 -bool CDirectory::IsEntryValid()
   1.135 -{
   1.136 -	return IsValid();
   1.137 -}
   1.138 -
   1.139 -bool CDirectory::IsEntryDirectory()
   1.140 -{
   1.141 -#if defined PLATFORM_WINDOWS
   1.142 -	return ((m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
   1.143 -#elif defined PLATFORM_LINUX
   1.144 -	char temppath[PLATFORM_MAX_PATH];
   1.145 -	snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
   1.146 -	return g_LibSys.IsPathDirectory(temppath);
   1.147 -#endif
   1.148 -}
   1.149 -
   1.150 -bool CDirectory::IsEntryFile()
   1.151 -{
   1.152 -#if defined PLATFORM_WINDOWS
   1.153 -	return !(m_fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE));
   1.154 -#elif defined PLATFORM_POSIX
   1.155 -	char temppath[PLATFORM_MAX_PATH];
   1.156 -	snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
   1.157 -	return g_LibSys.IsPathFile(temppath);
   1.158 -#endif
   1.159 -}
   1.160 -
   1.161 -const char *CDirectory::GetEntryName()
   1.162 -{
   1.163 -#if defined PLATFORM_WINDOWS
   1.164 -	return m_fd.cFileName;
   1.165 -#elif defined PLATFORM_LINUX
   1.166 -	return ep ? ep->d_name : "";
   1.167 -#endif
   1.168 -}
   1.169 -
   1.170 -bool CDirectory::MoreFiles()
   1.171 -{
   1.172 -	return IsValid();
   1.173 -}
   1.174 -
   1.175 -bool CDirectory::IsValid()
   1.176 -{
   1.177 -#if defined PLATFORM_WINDOWS
   1.178 -	return (m_dir != INVALID_HANDLE_VALUE);
   1.179 -#elif defined PLATFORM_LINUX
   1.180 -	return (m_dir != NULL);
   1.181 -#endif
   1.182 -}
   1.183 -
   1.184 -
   1.185 -/*************************
   1.186 - ** Library System Code **
   1.187 - *************************/
   1.188 -
   1.189 -
   1.190 -bool LibrarySystem::PathExists(const char *path)
   1.191 -{
   1.192 -#if defined PLATFORM_WINDOWS
   1.193 -	DWORD attr = GetFileAttributesA(path);
   1.194 -
   1.195 -	return (attr != INVALID_FILE_ATTRIBUTES);
   1.196 -#elif defined PLATFORM_POSIX
   1.197 -	struct stat s;
   1.198 -
   1.199 -	return (stat(path, &s) == 0);
   1.200 -#endif
   1.201 -}
   1.202 -
   1.203 -bool LibrarySystem::IsPathFile(const char *path)
   1.204 -{
   1.205 -#if defined PLATFORM_WINDOWS
   1.206 -	DWORD attr = GetFileAttributes(path);
   1.207 -
   1.208 -	if (attr == INVALID_FILE_ATTRIBUTES)
   1.209 -	{
   1.210 -		return false;
   1.211 -	}
   1.212 -
   1.213 -	if (attr & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE))
   1.214 -	{
   1.215 -		return false;
   1.216 -	}
   1.217 -
   1.218 -	return true;
   1.219 -#elif defined PLATFORM_LINUX
   1.220 -	struct stat s;
   1.221 -
   1.222 -	if (stat(path, &s) != 0)
   1.223 -	{
   1.224 -		return false;
   1.225 -	}
   1.226 -
   1.227 -	return S_ISREG(s.st_mode) ? true : false;
   1.228 -#endif
   1.229 -}
   1.230 -
   1.231 -bool LibrarySystem::IsPathDirectory(const char *path)
   1.232 -{
   1.233 -#if defined PLATFORM_WINDOWS
   1.234 -	DWORD attr = GetFileAttributes(path);
   1.235 -
   1.236 -	if (attr == INVALID_FILE_ATTRIBUTES)
   1.237 -	{
   1.238 -		return false;
   1.239 -	}
   1.240 -
   1.241 -	if (attr & FILE_ATTRIBUTE_DIRECTORY)
   1.242 -	{
   1.243 -		return true;
   1.244 -	}
   1.245 -
   1.246 -#elif defined PLATFORM_LINUX
   1.247 -	struct stat s;
   1.248 -
   1.249 -	if (stat(path, &s) != 0)
   1.250 -	{
   1.251 -		return false;
   1.252 -	}
   1.253 -
   1.254 -	if (S_ISDIR(s.st_mode))
   1.255 -	{
   1.256 -		return true;
   1.257 -	}
   1.258 -#endif
   1.259 -
   1.260 -	return false;
   1.261 -}
   1.262 -
   1.263 -IDirectory *LibrarySystem::OpenDirectory(const char *path)
   1.264 -{
   1.265 -	CDirectory *dir = new CDirectory(path);
   1.266 -
   1.267 -	if (!dir->IsValid())
   1.268 -	{
   1.269 -		delete dir;
   1.270 -		return NULL;
   1.271 -	}
   1.272 -
   1.273 -	return dir;
   1.274 -}
   1.275 -
   1.276 -void LibrarySystem::GetPlatformError(char *error, size_t maxlength)
   1.277 -{
   1.278 -	if (error && maxlength)
   1.279 -	{
   1.280 -#if defined PLATFORM_WINDOWS
   1.281 -		DWORD dw = GetLastError();
   1.282 -		FormatMessageA(
   1.283 -			FORMAT_MESSAGE_FROM_SYSTEM,
   1.284 -			NULL,
   1.285 -			dw,
   1.286 -			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   1.287 -			(LPSTR)error,
   1.288 -			maxlength,
   1.289 -			NULL);
   1.290 -#elif defined PLATFORM_POSIX
   1.291 -		snprintf(error, maxlength, "%s", strerror(errno));
   1.292 -#endif
   1.293 -	}
   1.294 -}
   1.295 -
   1.296 -void LibrarySystem::CloseDirectory(IDirectory *dir)
   1.297 -{
   1.298 -	delete dir;
   1.299 -}
   1.300 -
   1.301 -ILibrary *LibrarySystem::OpenLibrary(const char *path, char *error, size_t maxlength)
   1.302 -{
   1.303 -	LibraryHandle lib;
   1.304 -#if defined PLATFORM_WINDOWS
   1.305 -	lib = LoadLibraryA(path);
   1.306 -	if (!lib)
   1.307 -	{
   1.308 -		GetPlatformError(error, maxlength);
   1.309 -		return false;
   1.310 -	}
   1.311 -#elif defined PLATFORM_POSIX
   1.312 -	lib = dlopen(path, RTLD_NOW);
   1.313 -	if (!lib)
   1.314 -	{
   1.315 -		GetPlatformError(error, maxlength);
   1.316 -		return false;
   1.317 -	}
   1.318 -#endif
   1.319 -
   1.320 -	return new CLibrary(lib);
   1.321 -}
   1.322 -
   1.323 -size_t LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
   1.324 -{
   1.325 -	va_list ap;
   1.326 -	va_start(ap, fmt);
   1.327 -	size_t mylen = vsnprintf(buffer, len, fmt, ap);
   1.328 -	va_end(ap);
   1.329 -
   1.330 -	if (mylen >= len)
   1.331 -	{
   1.332 -		mylen = len - 1;
   1.333 -		buffer[mylen] = '\0';
   1.334 -	}
   1.335 -
   1.336 -	for (size_t i=0; i<mylen; i++)
   1.337 -	{
   1.338 -		if (buffer[i] == PLATFORM_SEP_ALTCHAR)
   1.339 -		{
   1.340 -			buffer[i] = PLATFORM_SEP_CHAR;
   1.341 -		}
   1.342 -	}
   1.343 -
   1.344 -	return mylen;
   1.345 -}
   1.346 -
   1.347 -const char *LibrarySystem::GetFileExtension(const char *filename)
   1.348 -{
   1.349 -	size_t end = strlen(filename) - 1;
   1.350 -
   1.351 -	for (size_t i = end; i >= 0; i--)
   1.352 -	{
   1.353 -		if (i > end)
   1.354 -		{
   1.355 -			break;
   1.356 -		}
   1.357 -
   1.358 -		if (filename[i] == PLATFORM_SEP_CHAR || filename[i] == PLATFORM_SEP_ALTCHAR)
   1.359 -		{
   1.360 -			break;
   1.361 -		}
   1.362 -
   1.363 -		if (filename[i] == '.' && i != end)
   1.364 -		{
   1.365 -			return &filename[++i];
   1.366 -		}
   1.367 -	}
   1.368 -
   1.369 -	return NULL;
   1.370 -}
   1.371 -
   1.372 -bool LibrarySystem::CreateFolder(const char *path)
   1.373 -{
   1.374 -#if defined PLATFORM_WINDOWS
   1.375 -	return (mkdir(path) != -1);
   1.376 -#elif defined PLATFORM_POSIX
   1.377 -	return (mkdir(path, 0775) != -1);
   1.378 -#endif
   1.379 -}
   1.380 -
   1.381 -size_t LibrarySystem::GetFileFromPath(char *buffer, size_t maxlength, const char *path)
   1.382 -{
   1.383 -	size_t length = strlen(path);
   1.384 -
   1.385 -	for (size_t i = length - 1;
   1.386 -		 i >= 0 && i <= length - 1;
   1.387 -		 i--)
   1.388 -	{
   1.389 -		if (path[i] == '/' 
   1.390 -#if defined PLATFORM_WINDOWS
   1.391 -			|| path[i] == '\\'
   1.392 -#endif
   1.393 -			)
   1.394 -		{
   1.395 -			return UTIL_Format(buffer, maxlength, "%s", &path[i+1]);
   1.396 -		}
   1.397 -	}
   1.398 -
   1.399 -	/* We scanned and found no path separator */
   1.400 -	return UTIL_Format(buffer, maxlength, "%s", path);
   1.401 -}