core/LibrarySys.cpp
author Fyren <fyrenmoo@gmail.com>
Sun Mar 01 16:41:44 2009 -0500 (2009-03-01)
changeset 2637 ed7b03e3bf16
parent 2474 1f4345fb0f75
child 2685 aacf5c8b3ac2
permissions -rw-r--r--
Added case insensitivity search to ReplaceString (bug 3639, r=dvander).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 <sys/stat.h>
    33 #include <stdio.h>
    34 #include <stdarg.h>
    35 #include <string.h>
    36 #include <sm_platform.h>
    37 #include "sm_stringutil.h"
    38 #include "LibrarySys.h"
    39 
    40 LibrarySystem g_LibSys;
    41 
    42 CLibrary::~CLibrary()
    43 {
    44 	if (m_lib)
    45 	{
    46 #if defined PLATFORM_WINDOWS
    47 		FreeLibrary(m_lib);
    48 #elif defined PLATFORM_POSIX
    49 		dlclose(m_lib);
    50 #endif
    51 		m_lib = NULL;
    52 	}
    53 }
    54 
    55 CLibrary::CLibrary(LibraryHandle me)
    56 {
    57 	m_lib = me;
    58 }
    59 
    60 void CLibrary::CloseLibrary()
    61 {
    62 	delete this;
    63 }
    64 
    65 void *CLibrary::GetSymbolAddress(const char *symname)
    66 {
    67 #if defined PLATFORM_WINDOWS
    68 	return GetProcAddress(m_lib, symname);
    69 #elif defined PLATFORM_POSIX
    70 	return dlsym(m_lib, symname);
    71 #endif
    72 }
    73 
    74 
    75 /********************
    76  ** Directory Code **
    77  ********************/
    78 
    79 
    80 CDirectory::CDirectory(const char *path)
    81 {
    82 #if defined PLATFORM_WINDOWS
    83 	char newpath[PLATFORM_MAX_PATH];
    84 	snprintf(newpath, sizeof(newpath), "%s\\*.*", path);
    85 	m_dir = FindFirstFileA(newpath, &m_fd);
    86 	if (!IsValid())
    87 	{
    88 		m_fd.cFileName[0] = '\0';
    89 	}
    90 #elif defined PLATFORM_POSIX
    91 	m_dir = opendir(path);
    92 	if (IsValid())
    93 	{
    94 		/* :TODO: we need to read past "." and ".."! */
    95 		ep = readdir(m_dir);
    96 		snprintf(m_origpath, PLATFORM_MAX_PATH, "%s", path);
    97 	} else {
    98 		ep = NULL;
    99 	}
   100 #endif
   101 }
   102 
   103 CDirectory::~CDirectory()
   104 {
   105 	if (IsValid())
   106 	{
   107 #if defined PLATFORM_WINDOWS
   108 		FindClose(m_dir);
   109 #elif defined PLATFORM_POSIX
   110 		closedir(m_dir);
   111 #endif
   112 	}
   113 }
   114 
   115 void CDirectory::NextEntry()
   116 {
   117 #if defined PLATFORM_WINDOWS
   118 	if (FindNextFile(m_dir, &m_fd) == 0)
   119 	{
   120 		FindClose(m_dir);
   121 		m_dir = INVALID_HANDLE_VALUE;
   122 	}
   123 #elif defined PLATFORM_POSIX
   124 	if ((ep=readdir(m_dir)) == NULL)
   125 	{
   126 		closedir(m_dir);
   127 		m_dir = NULL;
   128 	}
   129 #endif
   130 }
   131 
   132 bool CDirectory::IsEntryValid()
   133 {
   134 	return IsValid();
   135 }
   136 
   137 bool CDirectory::IsEntryDirectory()
   138 {
   139 #if defined PLATFORM_WINDOWS
   140 	return ((m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
   141 #elif defined PLATFORM_POSIX
   142 	char temppath[PLATFORM_MAX_PATH];
   143 	snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
   144 	return g_LibSys.IsPathDirectory(temppath);
   145 #endif
   146 }
   147 
   148 bool CDirectory::IsEntryFile()
   149 {
   150 #if defined PLATFORM_WINDOWS
   151 	return !(m_fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE));
   152 #elif defined PLATFORM_POSIX
   153 	char temppath[PLATFORM_MAX_PATH];
   154 	snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
   155 	return g_LibSys.IsPathFile(temppath);
   156 #endif
   157 }
   158 
   159 const char *CDirectory::GetEntryName()
   160 {
   161 #if defined PLATFORM_WINDOWS
   162 	return m_fd.cFileName;
   163 #elif defined PLATFORM_POSIX
   164 	return ep ? ep->d_name : "";
   165 #endif
   166 }
   167 
   168 bool CDirectory::MoreFiles()
   169 {
   170 	return IsValid();
   171 }
   172 
   173 bool CDirectory::IsValid()
   174 {
   175 #if defined PLATFORM_WINDOWS
   176 	return (m_dir != INVALID_HANDLE_VALUE);
   177 #elif defined PLATFORM_POSIX
   178 	return (m_dir != NULL);
   179 #endif
   180 }
   181 
   182 
   183 /*************************
   184  ** Library System Code **
   185  *************************/
   186 
   187 
   188 bool LibrarySystem::PathExists(const char *path)
   189 {
   190 #if defined PLATFORM_WINDOWS
   191 	DWORD attr = GetFileAttributesA(path);
   192 
   193 	return (attr != INVALID_FILE_ATTRIBUTES);
   194 #elif defined PLATFORM_POSIX
   195 	struct stat s;
   196 
   197 	return (stat(path, &s) == 0);
   198 #endif
   199 }
   200 
   201 bool LibrarySystem::IsPathFile(const char *path)
   202 {
   203 #if defined PLATFORM_WINDOWS
   204 	DWORD attr = GetFileAttributes(path);
   205 
   206 	if (attr == INVALID_FILE_ATTRIBUTES)
   207 	{
   208 		return false;
   209 	}
   210 
   211 	if (attr & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE))
   212 	{
   213 		return false;
   214 	}
   215 
   216 	return true;
   217 #elif defined PLATFORM_POSIX
   218 	struct stat s;
   219 
   220 	if (stat(path, &s) != 0)
   221 	{
   222 		return false;
   223 	}
   224 
   225 	return S_ISREG(s.st_mode) ? true : false;
   226 #endif
   227 }
   228 
   229 bool LibrarySystem::IsPathDirectory(const char *path)
   230 {
   231 #if defined PLATFORM_WINDOWS
   232 	DWORD attr = GetFileAttributes(path);
   233 
   234 	if (attr == INVALID_FILE_ATTRIBUTES)
   235 	{
   236 		return false;
   237 	}
   238 
   239 	if (attr & FILE_ATTRIBUTE_DIRECTORY)
   240 	{
   241 		return true;
   242 	}
   243 
   244 #elif defined PLATFORM_LINUX
   245 	struct stat s;
   246 
   247 	if (stat(path, &s) != 0)
   248 	{
   249 		return false;
   250 	}
   251 
   252 	if (S_ISDIR(s.st_mode))
   253 	{
   254 		return true;
   255 	}
   256 #endif
   257 
   258 	return false;
   259 }
   260 
   261 IDirectory *LibrarySystem::OpenDirectory(const char *path)
   262 {
   263 	CDirectory *dir = new CDirectory(path);
   264 
   265 	if (!dir->IsValid())
   266 	{
   267 		delete dir;
   268 		return NULL;
   269 	}
   270 
   271 	return dir;
   272 }
   273 
   274 void LibrarySystem::GetPlatformError(char *error, size_t maxlength)
   275 {
   276 #if defined PLATFORM_WINDOWS
   277 	return GetPlatformErrorEx(GetLastError(), error, maxlength);
   278 #elif defined PLATFORM_POSIX
   279 	return GetPlatformErrorEx(errno, error, maxlength);
   280 #endif
   281 }
   282 
   283 void LibrarySystem::GetPlatformErrorEx(int code, char *error, size_t maxlength)
   284 {
   285 	if (error && maxlength)
   286 	{
   287 #if defined PLATFORM_WINDOWS
   288 		FormatMessageA(
   289 			FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   290 			NULL,
   291 			(DWORD)code,
   292 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   293 			(LPSTR)error,
   294 			maxlength,
   295 			NULL);
   296 #elif defined PLATFORM_LINUX
   297 		const char *ae = strerror_r(code, error, maxlength);
   298 		if (ae != error)
   299 		{
   300 			UTIL_Format(error, maxlength, "%s", ae);
   301 		}
   302 #elif defined PLATFORM_POSIX
   303 		strerror_r(code, error, maxlength);
   304 #endif
   305 	}
   306 }
   307 
   308 void LibrarySystem::GetLoaderError(char *buffer, size_t maxlength)
   309 {
   310 #if defined PLATFORM_WINDOWS
   311 	GetPlatformError(buffer, maxlength);
   312 #elif defined PLATFORM_POSIX
   313 	if (buffer != NULL && maxlength)
   314 	{
   315 		strncopy(buffer, dlerror(), maxlength);
   316 	}
   317 #endif
   318 }
   319 
   320 void LibrarySystem::CloseDirectory(IDirectory *dir)
   321 {
   322 	delete dir;
   323 }
   324 
   325 ILibrary *LibrarySystem::OpenLibrary(const char *path, char *error, size_t maxlength)
   326 {
   327 	LibraryHandle lib;
   328 #if defined PLATFORM_WINDOWS
   329 	lib = LoadLibraryA(path);
   330 #elif defined PLATFORM_POSIX
   331 	lib = dlopen(path, RTLD_NOW);
   332 #endif
   333 
   334 	if (lib == NULL)
   335 	{
   336 		GetLoaderError(error, maxlength);
   337 		return NULL;
   338 	}
   339 
   340 	return new CLibrary(lib);
   341 }
   342 
   343 size_t LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
   344 {
   345 	va_list ap;
   346 	va_start(ap, fmt);
   347 	size_t mylen = vsnprintf(buffer, len, fmt, ap);
   348 	va_end(ap);
   349 
   350 	if (mylen >= len)
   351 	{
   352 		mylen = len - 1;
   353 		buffer[mylen] = '\0';
   354 	}
   355 
   356 	for (size_t i=0; i<mylen; i++)
   357 	{
   358 		if (buffer[i] == PLATFORM_SEP_ALTCHAR)
   359 		{
   360 			buffer[i] = PLATFORM_SEP_CHAR;
   361 		}
   362 	}
   363 
   364 	return mylen;
   365 }
   366 
   367 const char *LibrarySystem::GetFileExtension(const char *filename)
   368 {
   369 	size_t len, end;
   370 
   371 	len = strlen(filename);
   372 
   373 	/* Minimum string length for filename with ext would be 3; example: a.a */
   374 	if (len < 3)
   375 	{
   376 		return NULL;
   377 	}
   378 
   379 	end = len - 1;
   380 
   381 	for (size_t i = end; i <= end; i--)
   382 	{
   383 		if (filename[i] == PLATFORM_SEP_CHAR || filename[i] == PLATFORM_SEP_ALTCHAR)
   384 		{
   385 			break;
   386 		}
   387 
   388 		if (filename[i] == '.' && i != end && i != 0)
   389 		{
   390 			return &filename[++i];
   391 		}
   392 	}
   393 
   394 	return NULL;
   395 }
   396 
   397 bool LibrarySystem::CreateFolder(const char *path)
   398 {
   399 #if defined PLATFORM_WINDOWS
   400 	return (mkdir(path) != -1);
   401 #elif defined PLATFORM_POSIX
   402 	return (mkdir(path, 0775) != -1);
   403 #endif
   404 }
   405 
   406 size_t LibrarySystem::GetFileFromPath(char *buffer, size_t maxlength, const char *path)
   407 {
   408 	size_t length = strlen(path);
   409 
   410 	for (size_t i = length - 1;
   411 		 i >= 0 && i <= length - 1;
   412 		 i--)
   413 	{
   414 		if (path[i] == '/' 
   415 #if defined PLATFORM_WINDOWS
   416 			|| path[i] == '\\'
   417 #endif
   418 			)
   419 		{
   420 			return UTIL_Format(buffer, maxlength, "%s", &path[i+1]);
   421 		}
   422 	}
   423 
   424 	/* We scanned and found no path separator */
   425 	return UTIL_Format(buffer, maxlength, "%s", path);
   426 }
   427 
   428 bool LibrarySystem::FileTime(const char *path, FileTimeType type, time_t *pTime)
   429 {
   430 #ifdef PLATFORM_WINDOWS
   431 	struct _stat s;
   432 	if (_stat(path, &s) != 0)
   433 #elif defined PLATFORM_POSIX
   434 	struct stat s;
   435 	if (stat(path, &s) != 0)
   436 #endif
   437 	{
   438 		return false;
   439 	}
   440 
   441 	if (type == FileTime_LastAccess)
   442 	{
   443 		*pTime = s.st_atime;
   444 	}
   445 	else if (type == FileTime_Created)
   446 	{
   447 		*pTime = s.st_ctime;
   448 	}
   449 	else if (type == FileTime_LastChange)
   450 	{
   451 		*pTime = s.st_mtime;
   452 	}
   453 
   454 	return true;
   455 }