core/LibrarySys.cpp
changeset 2411 8dfaa3d730b5
parent 2289 44045ead61cd
child 2471 194be299b700
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/LibrarySys.cpp	Sun Nov 16 16:06:18 2008 -0800
     1.3 @@ -0,0 +1,441 @@
     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 +		UTIL_Format(error, maxlength, "%s", strerror(errno));
   1.293 +#endif
   1.294 +	}
   1.295 +}
   1.296 +
   1.297 +void LibrarySystem::GetLoaderError(char *buffer, size_t maxlength)
   1.298 +{
   1.299 +#if defined PLATFORM_WINDOWS
   1.300 +	GetPlatformError(buffer, maxlength);
   1.301 +#elif defined PLATFORM_POSIX
   1.302 +	if (buffer != NULL && maxlength)
   1.303 +	{
   1.304 +		strncopy(buffer, dlerror(), maxlength);
   1.305 +	}
   1.306 +#endif
   1.307 +}
   1.308 +
   1.309 +void LibrarySystem::CloseDirectory(IDirectory *dir)
   1.310 +{
   1.311 +	delete dir;
   1.312 +}
   1.313 +
   1.314 +ILibrary *LibrarySystem::OpenLibrary(const char *path, char *error, size_t maxlength)
   1.315 +{
   1.316 +	LibraryHandle lib;
   1.317 +#if defined PLATFORM_WINDOWS
   1.318 +	lib = LoadLibraryA(path);
   1.319 +#elif defined PLATFORM_POSIX
   1.320 +	lib = dlopen(path, RTLD_NOW);
   1.321 +#endif
   1.322 +
   1.323 +	if (lib == NULL)
   1.324 +	{
   1.325 +		GetLoaderError(error, maxlength);
   1.326 +		return NULL;
   1.327 +	}
   1.328 +
   1.329 +	return new CLibrary(lib);
   1.330 +}
   1.331 +
   1.332 +size_t LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
   1.333 +{
   1.334 +	va_list ap;
   1.335 +	va_start(ap, fmt);
   1.336 +	size_t mylen = vsnprintf(buffer, len, fmt, ap);
   1.337 +	va_end(ap);
   1.338 +
   1.339 +	if (mylen >= len)
   1.340 +	{
   1.341 +		mylen = len - 1;
   1.342 +		buffer[mylen] = '\0';
   1.343 +	}
   1.344 +
   1.345 +	for (size_t i=0; i<mylen; i++)
   1.346 +	{
   1.347 +		if (buffer[i] == PLATFORM_SEP_ALTCHAR)
   1.348 +		{
   1.349 +			buffer[i] = PLATFORM_SEP_CHAR;
   1.350 +		}
   1.351 +	}
   1.352 +
   1.353 +	return mylen;
   1.354 +}
   1.355 +
   1.356 +const char *LibrarySystem::GetFileExtension(const char *filename)
   1.357 +{
   1.358 +	size_t len, end;
   1.359 +
   1.360 +	len = strlen(filename);
   1.361 +
   1.362 +	/* Minimum string length for filename with ext would be 3; example: a.a */
   1.363 +	if (len < 3)
   1.364 +	{
   1.365 +		return NULL;
   1.366 +	}
   1.367 +
   1.368 +	end = len - 1;
   1.369 +
   1.370 +	for (size_t i = end; i <= end; i--)
   1.371 +	{
   1.372 +		if (filename[i] == PLATFORM_SEP_CHAR || filename[i] == PLATFORM_SEP_ALTCHAR)
   1.373 +		{
   1.374 +			break;
   1.375 +		}
   1.376 +
   1.377 +		if (filename[i] == '.' && i != end && i != 0)
   1.378 +		{
   1.379 +			return &filename[++i];
   1.380 +		}
   1.381 +	}
   1.382 +
   1.383 +	return NULL;
   1.384 +}
   1.385 +
   1.386 +bool LibrarySystem::CreateFolder(const char *path)
   1.387 +{
   1.388 +#if defined PLATFORM_WINDOWS
   1.389 +	return (mkdir(path) != -1);
   1.390 +#elif defined PLATFORM_POSIX
   1.391 +	return (mkdir(path, 0775) != -1);
   1.392 +#endif
   1.393 +}
   1.394 +
   1.395 +size_t LibrarySystem::GetFileFromPath(char *buffer, size_t maxlength, const char *path)
   1.396 +{
   1.397 +	size_t length = strlen(path);
   1.398 +
   1.399 +	for (size_t i = length - 1;
   1.400 +		 i >= 0 && i <= length - 1;
   1.401 +		 i--)
   1.402 +	{
   1.403 +		if (path[i] == '/' 
   1.404 +#if defined PLATFORM_WINDOWS
   1.405 +			|| path[i] == '\\'
   1.406 +#endif
   1.407 +			)
   1.408 +		{
   1.409 +			return UTIL_Format(buffer, maxlength, "%s", &path[i+1]);
   1.410 +		}
   1.411 +	}
   1.412 +
   1.413 +	/* We scanned and found no path separator */
   1.414 +	return UTIL_Format(buffer, maxlength, "%s", path);
   1.415 +}
   1.416 +
   1.417 +bool LibrarySystem::FileTime(const char *path, FileTimeType type, time_t *pTime)
   1.418 +{
   1.419 +#ifdef PLATFORM_WINDOWS
   1.420 +	struct _stat s;
   1.421 +	if (_stat(path, &s) != 0)
   1.422 +#elif defined PLATFORM_POSIX
   1.423 +	struct stat s;
   1.424 +	if (stat(path, &s) != 0)
   1.425 +#endif
   1.426 +	{
   1.427 +		return false;
   1.428 +	}
   1.429 +
   1.430 +	if (type == FileTime_LastAccess)
   1.431 +	{
   1.432 +		*pTime = s.st_atime;
   1.433 +	}
   1.434 +	else if (type == FileTime_Created)
   1.435 +	{
   1.436 +		*pTime = s.st_ctime;
   1.437 +	}
   1.438 +	else if (type == FileTime_LastChange)
   1.439 +	{
   1.440 +		*pTime = s.st_mtime;
   1.441 +	}
   1.442 +
   1.443 +	return true;
   1.444 +}