core/systems/LibrarySys.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Mar 28 06:21:34 2008 +0000 (2008-03-28)
branchsourcemod-1.1.0
changeset 1951 c7b4237fff50
parent 1508 398538bce59b
child 1738 cb3be0b31d99
permissions -rw-r--r--
Changed env var used in include paths: SOURCEMM -> SORCEMM16
Removed some unneccessary build configuartations in some project files
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdio.h>
    33 #include <stdarg.h>
    34 #include <string.h>
    35 #include <sm_platform.h>
    36 #include "sm_stringutil.h"
    37 #include "LibrarySys.h"
    38 
    39 LibrarySystem g_LibSys;
    40 
    41 CLibrary::~CLibrary()
    42 {
    43 	if (m_lib)
    44 	{
    45 #if defined PLATFORM_WINDOWS
    46 		FreeLibrary(m_lib);
    47 #elif defined PLATFORM_POSIX
    48 		dlclose(m_lib);
    49 #endif
    50 		m_lib = NULL;
    51 	}
    52 }
    53 
    54 CLibrary::CLibrary(LibraryHandle me)
    55 {
    56 	m_lib = me;
    57 }
    58 
    59 void CLibrary::CloseLibrary()
    60 {
    61 	delete this;
    62 }
    63 
    64 void *CLibrary::GetSymbolAddress(const char *symname)
    65 {
    66 #if defined PLATFORM_WINDOWS
    67 	return GetProcAddress(m_lib, symname);
    68 #elif defined PLATFORM_POSIX
    69 	return dlsym(m_lib, symname);
    70 #endif
    71 }
    72 
    73 
    74 /********************
    75  ** Directory Code **
    76  ********************/
    77 
    78 
    79 CDirectory::CDirectory(const char *path)
    80 {
    81 #if defined PLATFORM_WINDOWS
    82 	char newpath[PLATFORM_MAX_PATH];
    83 	snprintf(newpath, sizeof(newpath), "%s\\*.*", path);
    84 	m_dir = FindFirstFileA(newpath, &m_fd);
    85 	if (!IsValid())
    86 	{
    87 		m_fd.cFileName[0] = '\0';
    88 	}
    89 #elif defined PLATFORM_POSIX
    90 	m_dir = opendir(path);
    91 	if (IsValid())
    92 	{
    93 		/* :TODO: we need to read past "." and ".."! */
    94 		ep = readdir(m_dir);
    95 		snprintf(m_origpath, PLATFORM_MAX_PATH, "%s", path);
    96 	} else {
    97 		ep = NULL;
    98 	}
    99 #endif
   100 }
   101 
   102 CDirectory::~CDirectory()
   103 {
   104 	if (IsValid())
   105 	{
   106 #if defined PLATFORM_WINDOWS
   107 		FindClose(m_dir);
   108 #elif defined PLATFORM_POSIX
   109 		closedir(m_dir);
   110 #endif
   111 	}
   112 }
   113 
   114 void CDirectory::NextEntry()
   115 {
   116 #if defined PLATFORM_WINDOWS
   117 	if (FindNextFile(m_dir, &m_fd) == 0)
   118 	{
   119 		FindClose(m_dir);
   120 		m_dir = INVALID_HANDLE_VALUE;
   121 	}
   122 #elif defined PLATFORM_POSIX
   123 	if ((ep=readdir(m_dir)) == NULL)
   124 	{
   125 		closedir(m_dir);
   126 		m_dir = NULL;
   127 	}
   128 #endif
   129 }
   130 
   131 bool CDirectory::IsEntryValid()
   132 {
   133 	return IsValid();
   134 }
   135 
   136 bool CDirectory::IsEntryDirectory()
   137 {
   138 #if defined PLATFORM_WINDOWS
   139 	return ((m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
   140 #elif defined PLATFORM_LINUX
   141 	char temppath[PLATFORM_MAX_PATH];
   142 	snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
   143 	return g_LibSys.IsPathDirectory(temppath);
   144 #endif
   145 }
   146 
   147 bool CDirectory::IsEntryFile()
   148 {
   149 #if defined PLATFORM_WINDOWS
   150 	return !(m_fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE));
   151 #elif defined PLATFORM_POSIX
   152 	char temppath[PLATFORM_MAX_PATH];
   153 	snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
   154 	return g_LibSys.IsPathFile(temppath);
   155 #endif
   156 }
   157 
   158 const char *CDirectory::GetEntryName()
   159 {
   160 #if defined PLATFORM_WINDOWS
   161 	return m_fd.cFileName;
   162 #elif defined PLATFORM_LINUX
   163 	return ep ? ep->d_name : "";
   164 #endif
   165 }
   166 
   167 bool CDirectory::MoreFiles()
   168 {
   169 	return IsValid();
   170 }
   171 
   172 bool CDirectory::IsValid()
   173 {
   174 #if defined PLATFORM_WINDOWS
   175 	return (m_dir != INVALID_HANDLE_VALUE);
   176 #elif defined PLATFORM_LINUX
   177 	return (m_dir != NULL);
   178 #endif
   179 }
   180 
   181 
   182 /*************************
   183  ** Library System Code **
   184  *************************/
   185 
   186 
   187 bool LibrarySystem::PathExists(const char *path)
   188 {
   189 #if defined PLATFORM_WINDOWS
   190 	DWORD attr = GetFileAttributesA(path);
   191 
   192 	return (attr != INVALID_FILE_ATTRIBUTES);
   193 #elif defined PLATFORM_POSIX
   194 	struct stat s;
   195 
   196 	return (stat(path, &s) == 0);
   197 #endif
   198 }
   199 
   200 bool LibrarySystem::IsPathFile(const char *path)
   201 {
   202 #if defined PLATFORM_WINDOWS
   203 	DWORD attr = GetFileAttributes(path);
   204 
   205 	if (attr == INVALID_FILE_ATTRIBUTES)
   206 	{
   207 		return false;
   208 	}
   209 
   210 	if (attr & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE))
   211 	{
   212 		return false;
   213 	}
   214 
   215 	return true;
   216 #elif defined PLATFORM_LINUX
   217 	struct stat s;
   218 
   219 	if (stat(path, &s) != 0)
   220 	{
   221 		return false;
   222 	}
   223 
   224 	return S_ISREG(s.st_mode) ? true : false;
   225 #endif
   226 }
   227 
   228 bool LibrarySystem::IsPathDirectory(const char *path)
   229 {
   230 #if defined PLATFORM_WINDOWS
   231 	DWORD attr = GetFileAttributes(path);
   232 
   233 	if (attr == INVALID_FILE_ATTRIBUTES)
   234 	{
   235 		return false;
   236 	}
   237 
   238 	if (attr & FILE_ATTRIBUTE_DIRECTORY)
   239 	{
   240 		return true;
   241 	}
   242 
   243 #elif defined PLATFORM_LINUX
   244 	struct stat s;
   245 
   246 	if (stat(path, &s) != 0)
   247 	{
   248 		return false;
   249 	}
   250 
   251 	if (S_ISDIR(s.st_mode))
   252 	{
   253 		return true;
   254 	}
   255 #endif
   256 
   257 	return false;
   258 }
   259 
   260 IDirectory *LibrarySystem::OpenDirectory(const char *path)
   261 {
   262 	CDirectory *dir = new CDirectory(path);
   263 
   264 	if (!dir->IsValid())
   265 	{
   266 		delete dir;
   267 		return NULL;
   268 	}
   269 
   270 	return dir;
   271 }
   272 
   273 void LibrarySystem::GetPlatformError(char *error, size_t maxlength)
   274 {
   275 	if (error && maxlength)
   276 	{
   277 #if defined PLATFORM_WINDOWS
   278 		DWORD dw = GetLastError();
   279 		FormatMessageA(
   280 			FORMAT_MESSAGE_FROM_SYSTEM,
   281 			NULL,
   282 			dw,
   283 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   284 			(LPSTR)error,
   285 			maxlength,
   286 			NULL);
   287 #elif defined PLATFORM_POSIX
   288 		snprintf(error, maxlength, "%s", strerror(errno));
   289 #endif
   290 	}
   291 }
   292 
   293 void LibrarySystem::CloseDirectory(IDirectory *dir)
   294 {
   295 	delete dir;
   296 }
   297 
   298 ILibrary *LibrarySystem::OpenLibrary(const char *path, char *error, size_t maxlength)
   299 {
   300 	LibraryHandle lib;
   301 #if defined PLATFORM_WINDOWS
   302 	lib = LoadLibraryA(path);
   303 	if (!lib)
   304 	{
   305 		GetPlatformError(error, maxlength);
   306 		return false;
   307 	}
   308 #elif defined PLATFORM_POSIX
   309 	lib = dlopen(path, RTLD_NOW);
   310 	if (!lib)
   311 	{
   312 		GetPlatformError(error, maxlength);
   313 		return false;
   314 	}
   315 #endif
   316 
   317 	return new CLibrary(lib);
   318 }
   319 
   320 size_t LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
   321 {
   322 	va_list ap;
   323 	va_start(ap, fmt);
   324 	size_t mylen = vsnprintf(buffer, len, fmt, ap);
   325 	va_end(ap);
   326 
   327 	if (mylen >= len)
   328 	{
   329 		mylen = len - 1;
   330 		buffer[mylen] = '\0';
   331 	}
   332 
   333 	for (size_t i=0; i<mylen; i++)
   334 	{
   335 		if (buffer[i] == PLATFORM_SEP_ALTCHAR)
   336 		{
   337 			buffer[i] = PLATFORM_SEP_CHAR;
   338 		}
   339 	}
   340 
   341 	return mylen;
   342 }
   343 
   344 const char *LibrarySystem::GetFileExtension(const char *filename)
   345 {
   346 	size_t end = strlen(filename) - 1;
   347 
   348 	for (size_t i = end; i >= 0; i--)
   349 	{
   350 		if (i > end)
   351 		{
   352 			break;
   353 		}
   354 
   355 		if (filename[i] == PLATFORM_SEP_CHAR || filename[i] == PLATFORM_SEP_ALTCHAR)
   356 		{
   357 			break;
   358 		}
   359 
   360 		if (filename[i] == '.' && i != end)
   361 		{
   362 			return &filename[++i];
   363 		}
   364 	}
   365 
   366 	return NULL;
   367 }
   368 
   369 bool LibrarySystem::CreateFolder(const char *path)
   370 {
   371 #if defined PLATFORM_WINDOWS
   372 	return (mkdir(path) != -1);
   373 #elif defined PLATFORM_POSIX
   374 	return (mkdir(path, 0775) != -1);
   375 #endif
   376 }
   377 
   378 size_t LibrarySystem::GetFileFromPath(char *buffer, size_t maxlength, const char *path)
   379 {
   380 	size_t length = strlen(path);
   381 
   382 	for (size_t i = length - 1;
   383 		 i >= 0 && i <= length - 1;
   384 		 i--)
   385 	{
   386 		if (path[i] == '/' 
   387 #if defined PLATFORM_WINDOWS
   388 			|| path[i] == '\\'
   389 #endif
   390 			)
   391 		{
   392 			return UTIL_Format(buffer, maxlength, "%s", &path[i+1]);
   393 		}
   394 	}
   395 
   396 	/* We scanned and found no path separator */
   397 	return UTIL_Format(buffer, maxlength, "%s", path);
   398 }