core/metamod_util.cpp
changeset 539 db914129ae01
parent 523 6f1630519b81
child 589 8e97a1e960a5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/metamod_util.cpp	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,428 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ======================================================
     1.7 + * Metamod:Source
     1.8 + * Copyright (C) 2004-2008 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 ((((unsigned)path1[pos1] & 0x80) && path1[pos1] != path2[pos2])
   1.228 +			||
   1.229 +			!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2])
   1.230 +			)
   1.231 +		{
   1.232 +			return false;
   1.233 +		}
   1.234 +
   1.235 +	#ifdef WIN32
   1.236 +		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   1.237 +	#else
   1.238 +		if (path1[pos1] != path2[pos2])
   1.239 +	#endif
   1.240 +		{
   1.241 +			return false;
   1.242 +		}
   1.243 +
   1.244 +		pos1++;
   1.245 +		pos2++;
   1.246 +	}
   1.247 +}
   1.248 +
   1.249 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   1.250 +{
   1.251 +	va_list ap;
   1.252 +
   1.253 +	va_start(ap, fmt);
   1.254 +	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   1.255 +	va_end(ap);
   1.256 +
   1.257 +	if (len >= maxlength)
   1.258 +	{
   1.259 +		len = maxlength - 1;
   1.260 +		buffer[len] = '\0';
   1.261 +	}
   1.262 +
   1.263 +	return len;
   1.264 +}
   1.265 +
   1.266 +size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
   1.267 +{
   1.268 +	size_t len = vsnprintf(buffer, maxlength, fmt, params);
   1.269 +
   1.270 +	if (len >= maxlength)
   1.271 +	{
   1.272 +		len = maxlength - 1;
   1.273 +		buffer[len] = '\0';
   1.274 +	}
   1.275 +
   1.276 +	return len;
   1.277 +}
   1.278 +
   1.279 +inline bool pathchar_isalpha(char a)
   1.280 +{
   1.281 +	return (((a & 1<<7) == 0) && isalpha(a));
   1.282 +}
   1.283 +
   1.284 +inline bool pathchar_sep(char a)
   1.285 +{
   1.286 +#if defined WIN32
   1.287 +	return (a == '/' || a == '\\');
   1.288 +#elif defined __linux__
   1.289 +	return (a == '/');
   1.290 +#endif
   1.291 +}
   1.292 +
   1.293 +inline bool pathstr_isabsolute(const char *str)
   1.294 +{
   1.295 +#if defined WIN32
   1.296 +	return (pathchar_isalpha(str[0]) 
   1.297 +		&& str[1] == ':' 
   1.298 +		&& pathchar_sep(str[2]));
   1.299 +#elif defined __linux__
   1.300 +	return (str[0] == '/');
   1.301 +#endif
   1.302 +}
   1.303 +
   1.304 +inline bool pathchar_cmp(char a, char b)
   1.305 +{
   1.306 +#if defined WIN32
   1.307 +	if (pathchar_isalpha(a) && pathchar_isalpha(b))
   1.308 +	{
   1.309 +		return (tolower(a) == tolower(b));
   1.310 +	}
   1.311 +	/* Either path separator is acceptable */
   1.312 +	if (pathchar_sep(a))
   1.313 +	{
   1.314 +		return pathchar_sep(b);
   1.315 +	}
   1.316 +#endif
   1.317 +	return (a == b);
   1.318 +}
   1.319 +
   1.320 +/**
   1.321 + * @brief Forms a relative path given two absolute paths.
   1.322 + *
   1.323 + * @param buffer		Buffer to store relative path in.
   1.324 + * @param maxlength		Maximum length of the output buffer.
   1.325 + * @param relTo			Destination folder to use as a working directory.
   1.326 + *						Final folder name should not be pathchar-terminated.
   1.327 + * @param relFrom		Source file or folder to use as a target.
   1.328 + * @return				True on success, false on failure.
   1.329 + */
   1.330 +bool UTIL_Relatize(char buffer[],
   1.331 +				   size_t maxlength,
   1.332 +				   const char *relTo,
   1.333 +				   const char *relFrom)
   1.334 +{
   1.335 +	/* We don't allow relative paths in here, force
   1.336 +	 * the user to resolve these himself!
   1.337 +	 */
   1.338 +	if (!pathstr_isabsolute(relTo)
   1.339 +		|| !pathstr_isabsolute(relFrom))
   1.340 +	{
   1.341 +		return false;
   1.342 +	}
   1.343 +
   1.344 +#if defined WIN32
   1.345 +	/* Relative paths across drives are not possible */
   1.346 +	if (!pathchar_cmp(relTo[0], relFrom[0]))
   1.347 +	{
   1.348 +		return false;
   1.349 +	}
   1.350 +	/* Get rid of the drive and semicolon part */
   1.351 +	relTo = &relTo[2];
   1.352 +	relFrom = &relFrom[2];
   1.353 +#endif
   1.354 +
   1.355 +	/* Eliminate the common root between the paths */
   1.356 +	const char *rootTo = NULL;
   1.357 +	const char *rootFrom = NULL;
   1.358 +	while (*relTo != '\0' && *relFrom != '\0')
   1.359 +	{
   1.360 +		/* If we get to a new path sequence, start over */
   1.361 +		if (pathchar_sep(*relTo)
   1.362 +			&& pathchar_sep(*relFrom))
   1.363 +		{
   1.364 +			rootTo = relTo;
   1.365 +			rootFrom = relFrom;
   1.366 +		/* If the paths don't compare, stop looking for a common root */
   1.367 +		} else if (!pathchar_cmp(*relTo, *relFrom)) {
   1.368 +			break;
   1.369 +		}
   1.370 +		relTo++;
   1.371 +		relFrom++;
   1.372 +	}
   1.373 +
   1.374 +	/* NULLs shouldn't happen! */
   1.375 +	if (rootTo == NULL
   1.376 +		|| rootFrom == NULL)
   1.377 +	{
   1.378 +		return false;
   1.379 +	}
   1.380 +
   1.381 +	size_t numLevels = 0;
   1.382 +
   1.383 +	/* The root case is special! 
   1.384 +	 * Don't count anything from it.
   1.385 +	 */
   1.386 +	if (*(rootTo + 1) != '\0')
   1.387 +	{
   1.388 +		/* Search for how many levels we need to go up.
   1.389 +	 	 * Since the root pointer points to a '/', we increment
   1.390 +		 * the initial pointer by one.
   1.391 +		 */
   1.392 +		while (*rootTo != '\0')
   1.393 +		{
   1.394 +			if (pathchar_sep(*rootTo))
   1.395 +			{
   1.396 +				/* Check for an improper trailing slash,
   1.397 +				 * just to be nice even though the user 
   1.398 +				 * should NOT have done this!
   1.399 +				 */
   1.400 +				if (*(rootTo + 1) == '\0')
   1.401 +				{
   1.402 +					break;
   1.403 +				}
   1.404 +				numLevels++;
   1.405 +			}
   1.406 +			rootTo++;
   1.407 +		}
   1.408 +	}
   1.409 +
   1.410 +	/* Now build the new relative path. */
   1.411 +	size_t len, total = 0;
   1.412 +	while (numLevels--)
   1.413 +	{
   1.414 +		len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
   1.415 +		if (len >= maxlength - total)
   1.416 +		{
   1.417 +			/* Not enough space in the buffer */
   1.418 +			return false;
   1.419 +		}
   1.420 +		total += len;
   1.421 +	}
   1.422 +
   1.423 +	/* Add the absolute path. */
   1.424 +	len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
   1.425 +	if (len >= maxlength - total)
   1.426 +	{
   1.427 +		return false;
   1.428 +	}
   1.429 +
   1.430 +	return true;
   1.431 +}