sourcemm/metamod_util.cpp
branchsourcemm-1.6.0
changeset 428 40a9d2f61121
parent 370 4dc28ab1c722
child 429 cadecde4b802
     1.1 --- a/sourcemm/metamod_util.cpp	Thu Oct 04 19:46:28 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,425 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * ======================================================
     1.7 - * Metamod:Source
     1.8 - * Copyright (C) 2004-2007 AlliedModders LLC and authors.
     1.9 - * All rights reserved.
    1.10 - * ======================================================
    1.11 - *
    1.12 - * This software is provided 'as-is', without any express or implied warranty.
    1.13 - * In no event will the authors be held liable for any damages arising from 
    1.14 - * the use of this software.
    1.15 - * 
    1.16 - * Permission is granted to anyone to use this software for any purpose, 
    1.17 - * including commercial applications, and to alter it and redistribute it 
    1.18 - * freely, subject to the following restrictions:
    1.19 - *
    1.20 - * 1. The origin of this software must not be misrepresented; you must not 
    1.21 - * claim that you wrote the original software. If you use this software in a 
    1.22 - * product, an acknowledgment in the product documentation would be 
    1.23 - * appreciated but is not required.
    1.24 - * 2. Altered source versions must be plainly marked as such, and must not be
    1.25 - * misrepresented as being the original software.
    1.26 - * 3. This notice may not be removed or altered from any source distribution.
    1.27 - *
    1.28 - * Version: $Id$
    1.29 - */
    1.30 -
    1.31 -#include <ctype.h>
    1.32 -#include <string.h>
    1.33 -#include <stdio.h>
    1.34 -#include "metamod_util.h"
    1.35 -#include "metamod_oslink.h"
    1.36 -
    1.37 -/**
    1.38 - * @brief Utility functions
    1.39 - * @file util.cpp
    1.40 - */
    1.41 -
    1.42 -const char *UTIL_GetExtension(const char *file)
    1.43 -{
    1.44 -	int len = strlen(file);
    1.45 -	int i = 0;
    1.46 -
    1.47 -	for (i = len - 1; i >= 0; i--)
    1.48 -	{
    1.49 -		if (file[i] == '/' || file[i] == '\\')
    1.50 -		{
    1.51 -			return NULL;
    1.52 -		}
    1.53 -
    1.54 -		if ((file[i] == '.') && (i != len - 1))
    1.55 -		{
    1.56 -			return (const char *)&(file[i + 1]);
    1.57 -		}
    1.58 -	}
    1.59 -
    1.60 -	return NULL;
    1.61 -}
    1.62 -
    1.63 -void UTIL_TrimLeft(char *buffer)
    1.64 -{
    1.65 -	/* Let's think of this as our iterator */
    1.66 -	char *i = buffer;
    1.67 -
    1.68 -	/* Make sure the buffer isn't null */
    1.69 -	if (i && *i)
    1.70 -	{
    1.71 -		/* Add up number of whitespace characters */
    1.72 -		while(isspace((unsigned char) *i))
    1.73 -		{
    1.74 -			i++;
    1.75 -		}
    1.76 -
    1.77 -		/* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
    1.78 -		if (i != buffer)
    1.79 -		{
    1.80 -			memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
    1.81 -		}
    1.82 -	}
    1.83 -}
    1.84 -
    1.85 -void UTIL_TrimRight(char *buffer)
    1.86 -{
    1.87 -	/* Make sure buffer isn't null */
    1.88 -	if (buffer)
    1.89 -	{
    1.90 -		size_t len = strlen(buffer);
    1.91 -
    1.92 -		/* Loop through buffer backwards while replacing whitespace chars with null chars */
    1.93 -		for (size_t i = len - 1; i >= 0; i--)
    1.94 -		{
    1.95 -			if (isspace((unsigned char) buffer[i]))
    1.96 -			{
    1.97 -				buffer[i] = '\0';
    1.98 -			} else {
    1.99 -				break;
   1.100 -			}
   1.101 -		}
   1.102 -	}
   1.103 -}
   1.104 -
   1.105 -/* :TODO: this should skip string literals */
   1.106 -void UTIL_TrimComments(char *buffer)
   1.107 -{
   1.108 -	int num_sc = 0;
   1.109 -	size_t len = strlen(buffer);
   1.110 -	if (buffer)
   1.111 -	{
   1.112 -		for (int i = len - 1; i >= 0; i--)
   1.113 -		{
   1.114 -			if (buffer[i] == '/')
   1.115 -			{
   1.116 -				if (++num_sc >= 2 && i==0)
   1.117 -				{
   1.118 -					buffer[i] = '\0';
   1.119 -					return;
   1.120 -				}
   1.121 -			} else {
   1.122 -				if (num_sc >= 2)
   1.123 -				{
   1.124 -					buffer[i] = '\0';
   1.125 -					return;
   1.126 -				}
   1.127 -				num_sc = 0;
   1.128 -			}
   1.129 -			/* size_t won't go below 0, manually break out */
   1.130 -			if (i == 0)
   1.131 -			{
   1.132 -				break;
   1.133 -			}
   1.134 -		}
   1.135 -	}
   1.136 -}
   1.137 -
   1.138 -void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   1.139 -{
   1.140 -	size_t start;
   1.141 -	size_t len = strlen(str);
   1.142 -
   1.143 -	for (start = 0; start < len; start++)
   1.144 -	{
   1.145 -		if (!isspace(str[start]))
   1.146 -		{
   1.147 -			break;
   1.148 -		}
   1.149 -	}
   1.150 -
   1.151 -	size_t end;
   1.152 -	for (end = start; end < len; end++)
   1.153 -	{
   1.154 -		if (isspace(str[end]))
   1.155 -		{
   1.156 -			break;
   1.157 -		}
   1.158 -	}
   1.159 -	
   1.160 -	size_t i, c = 0;
   1.161 -	for (i = start; i < end; i++, c++)
   1.162 -	{
   1.163 -		if (c >= len1)
   1.164 -		{
   1.165 -			break;
   1.166 -		}
   1.167 -		buf1[c] = str[i];
   1.168 -	}
   1.169 -	buf1[c] = '\0';
   1.170 -
   1.171 -	for (start = end; start < len; start++)
   1.172 -	{
   1.173 -		if (!isspace(str[start]))
   1.174 -		{
   1.175 -			break;
   1.176 -		}
   1.177 -	}
   1.178 -
   1.179 -	for (c = 0; start < len; start++, c++)
   1.180 -	{
   1.181 -		if (c >= len2)
   1.182 -		{
   1.183 -			break;
   1.184 -		}
   1.185 -		buf2[c] = str[start];
   1.186 -	}
   1.187 -	buf2[c] = '\0';
   1.188 -}
   1.189 -
   1.190 -bool UTIL_PathCmp(const char *path1, const char *path2)
   1.191 -{
   1.192 -	size_t pos1 = 0, pos2 = 0;
   1.193 -
   1.194 -	while (true)
   1.195 -	{
   1.196 -		if (path1[pos1] == '\0' || path2[pos2] == '\0')
   1.197 -		{
   1.198 -			return (path1[pos1] == path2[pos2]);
   1.199 -		}
   1.200 -
   1.201 -		if (path1[pos1] == PATH_SEP_CHAR)
   1.202 -		{
   1.203 -			if (path2[pos2] != PATH_SEP_CHAR)
   1.204 -			{
   1.205 -				return false;
   1.206 -			}
   1.207 -
   1.208 -			/* Look for extra path chars */
   1.209 -			while (path1[++pos1])
   1.210 -			{
   1.211 -				if (path1[pos1] != PATH_SEP_CHAR)
   1.212 -				{
   1.213 -					break;
   1.214 -				}
   1.215 -			}
   1.216 -			while (path2[++pos2])
   1.217 -			{
   1.218 -				if (path2[pos2] != PATH_SEP_CHAR)
   1.219 -				{
   1.220 -					break;
   1.221 -				}
   1.222 -			}
   1.223 -			continue;
   1.224 -		}
   1.225 -
   1.226 -		/* If we're at a different non-alphanumeric, the next character MUST match */
   1.227 -		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
   1.228 -		{
   1.229 -			return false;
   1.230 -		}
   1.231 -
   1.232 -	#ifdef WIN32
   1.233 -		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   1.234 -	#else
   1.235 -		if (path1[pos1] != path2[pos2])
   1.236 -	#endif
   1.237 -		{
   1.238 -			return false;
   1.239 -		}
   1.240 -
   1.241 -		pos1++;
   1.242 -		pos2++;
   1.243 -	}
   1.244 -}
   1.245 -
   1.246 -size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   1.247 -{
   1.248 -	va_list ap;
   1.249 -
   1.250 -	va_start(ap, fmt);
   1.251 -	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   1.252 -	va_end(ap);
   1.253 -
   1.254 -	if (len >= maxlength)
   1.255 -	{
   1.256 -		len = maxlength - 1;
   1.257 -		buffer[len] = '\0';
   1.258 -	}
   1.259 -
   1.260 -	return len;
   1.261 -}
   1.262 -
   1.263 -size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
   1.264 -{
   1.265 -	size_t len = vsnprintf(buffer, maxlength, fmt, params);
   1.266 -
   1.267 -	if (len >= maxlength)
   1.268 -	{
   1.269 -		len = maxlength - 1;
   1.270 -		buffer[len] = '\0';
   1.271 -	}
   1.272 -
   1.273 -	return len;
   1.274 -}
   1.275 -
   1.276 -inline bool pathchar_isalpha(char a)
   1.277 -{
   1.278 -	return (((a & 1<<7) == 0) && isalpha(a));
   1.279 -}
   1.280 -
   1.281 -inline bool pathchar_sep(char a)
   1.282 -{
   1.283 -#if defined WIN32
   1.284 -	return (a == '/' || a == '\\');
   1.285 -#elif defined __linux__
   1.286 -	return (a == '/');
   1.287 -#endif
   1.288 -}
   1.289 -
   1.290 -inline bool pathstr_isabsolute(const char *str)
   1.291 -{
   1.292 -#if defined WIN32
   1.293 -	return (pathchar_isalpha(str[0]) 
   1.294 -		&& str[1] == ':' 
   1.295 -		&& pathchar_sep(str[2]));
   1.296 -#elif defined __linux__
   1.297 -	return (str[0] == '/');
   1.298 -#endif
   1.299 -}
   1.300 -
   1.301 -inline bool pathchar_cmp(char a, char b)
   1.302 -{
   1.303 -#if defined WIN32
   1.304 -	if (pathchar_isalpha(a) && pathchar_isalpha(b))
   1.305 -	{
   1.306 -		return (tolower(a) == tolower(b));
   1.307 -	}
   1.308 -	/* Either path separator is acceptable */
   1.309 -	if (pathchar_sep(a))
   1.310 -	{
   1.311 -		return pathchar_sep(b);
   1.312 -	}
   1.313 -#endif
   1.314 -	return (a == b);
   1.315 -}
   1.316 -
   1.317 -/**
   1.318 - * @brief Forms a relative path given two absolute paths.
   1.319 - *
   1.320 - * @param buffer		Buffer to store relative path in.
   1.321 - * @param maxlength		Maximum length of the output buffer.
   1.322 - * @param relTo			Destination folder to use as a working directory.
   1.323 - *						Final folder name should not be pathchar-terminated.
   1.324 - * @param relFrom		Source file or folder to use as a target.
   1.325 - * @return				True on success, false on failure.
   1.326 - */
   1.327 -bool UTIL_Relatize(char buffer[],
   1.328 -				   size_t maxlength,
   1.329 -				   const char *relTo,
   1.330 -				   const char *relFrom)
   1.331 -{
   1.332 -	/* We don't allow relative paths in here, force
   1.333 -	 * the user to resolve these himself!
   1.334 -	 */
   1.335 -	if (!pathstr_isabsolute(relTo)
   1.336 -		|| !pathstr_isabsolute(relFrom))
   1.337 -	{
   1.338 -		return false;
   1.339 -	}
   1.340 -
   1.341 -#if defined WIN32
   1.342 -	/* Relative paths across drives are not possible */
   1.343 -	if (!pathchar_cmp(relTo[0], relFrom[0]))
   1.344 -	{
   1.345 -		return false;
   1.346 -	}
   1.347 -	/* Get rid of the drive and semicolon part */
   1.348 -	relTo = &relTo[2];
   1.349 -	relFrom = &relFrom[2];
   1.350 -#endif
   1.351 -
   1.352 -	/* Eliminate the common root between the paths */
   1.353 -	const char *rootTo = NULL;
   1.354 -	const char *rootFrom = NULL;
   1.355 -	while (*relTo != '\0' && *relFrom != '\0')
   1.356 -	{
   1.357 -		/* If we get to a new path sequence, start over */
   1.358 -		if (pathchar_sep(*relTo)
   1.359 -			&& pathchar_sep(*relFrom))
   1.360 -		{
   1.361 -			rootTo = relTo;
   1.362 -			rootFrom = relFrom;
   1.363 -		/* If the paths don't compare, stop looking for a common root */
   1.364 -		} else if (!pathchar_cmp(*relTo, *relFrom)) {
   1.365 -			break;
   1.366 -		}
   1.367 -		relTo++;
   1.368 -		relFrom++;
   1.369 -	}
   1.370 -
   1.371 -	/* NULLs shouldn't happen! */
   1.372 -	if (rootTo == NULL
   1.373 -		|| rootFrom == NULL)
   1.374 -	{
   1.375 -		return false;
   1.376 -	}
   1.377 -
   1.378 -	size_t numLevels = 0;
   1.379 -
   1.380 -	/* The root case is special! 
   1.381 -	 * Don't count anything from it.
   1.382 -	 */
   1.383 -	if (*(rootTo + 1) != '\0')
   1.384 -	{
   1.385 -		/* Search for how many levels we need to go up.
   1.386 -	 	 * Since the root pointer points to a '/', we increment
   1.387 -		 * the initial pointer by one.
   1.388 -		 */
   1.389 -		while (*rootTo != '\0')
   1.390 -		{
   1.391 -			if (pathchar_sep(*rootTo))
   1.392 -			{
   1.393 -				/* Check for an improper trailing slash,
   1.394 -				 * just to be nice even though the user 
   1.395 -				 * should NOT have done this!
   1.396 -				 */
   1.397 -				if (*(rootTo + 1) == '\0')
   1.398 -				{
   1.399 -					break;
   1.400 -				}
   1.401 -				numLevels++;
   1.402 -			}
   1.403 -			rootTo++;
   1.404 -		}
   1.405 -	}
   1.406 -
   1.407 -	/* Now build the new relative path. */
   1.408 -	size_t len, total = 0;
   1.409 -	while (numLevels--)
   1.410 -	{
   1.411 -		len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
   1.412 -		if (len >= maxlength - total)
   1.413 -		{
   1.414 -			/* Not enough space in the buffer */
   1.415 -			return false;
   1.416 -		}
   1.417 -		total += len;
   1.418 -	}
   1.419 -
   1.420 -	/* Add the absolute path. */
   1.421 -	len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
   1.422 -	if (len >= maxlength - total)
   1.423 -	{
   1.424 -		return false;
   1.425 -	}
   1.426 -
   1.427 -	return true;
   1.428 -}