core-legacy/util.cpp
author David Anderson <dvander@alliedmods.net>
Mon Nov 18 11:57:04 2013 -0800 (2013-11-18)
changeset 884 ae6d28eabea0
parent 747 9563de0165f7
permissions -rw-r--r--
Push build.
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2008 Metamod:Source Development Team
     3  * No warranties of any kind
     4  *
     5  * License: zlib/libpng
     6  *
     7  * Author(s): David "BAILOPAN" Anderson
     8  * ============================
     9  */
    10 
    11 #include <ctype.h>
    12 #include <string.h>
    13 #include <stdio.h>
    14 #include "sourcemm.h"
    15 #include "util.h"
    16 #include "oslink.h"
    17 
    18 /**
    19  * @brief Utility functions
    20  * @file util.cpp
    21  */
    22 
    23 const char *UTIL_GetExtension(const char *file)
    24 {
    25 	int len = strlen(file);
    26 	int i = 0;
    27 
    28 	for (i = len - 1; i >= 0; i--)
    29 	{
    30 		if (file[i] == '/' || file[i] == '\\')
    31 		{
    32 			return NULL;
    33 		}
    34 
    35 		if ((file[i] == '.') && (i != len - 1))
    36 		{
    37 			return (const char *)&(file[i + 1]);
    38 		}
    39 	}
    40 
    41 	return NULL;
    42 }
    43 
    44 void UTIL_TrimLeft(char *buffer)
    45 {
    46 	/* Let's think of this as our iterator */
    47 	char *i = buffer;
    48 
    49 	/* Make sure the buffer isn't null */
    50 	if (i && *i)
    51 	{
    52 		/* Add up number of whitespace characters */
    53 		while(isspace((unsigned char) *i))
    54 		{
    55 			i++;
    56 		}
    57 
    58 		/* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
    59 		if (i != buffer)
    60 		{
    61 			memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
    62 		}
    63 	}
    64 }
    65 
    66 void UTIL_TrimRight(char *buffer)
    67 {
    68 	/* Make sure buffer isn't null */
    69 	if (buffer)
    70 	{
    71 		size_t len = strlen(buffer);
    72 
    73 		/* Loop through buffer backwards while replacing whitespace chars with null chars */
    74 		for (size_t i = len - 1; i < len; i--)
    75 		{
    76 			if (isspace((unsigned char) buffer[i]))
    77 			{
    78 				buffer[i] = '\0';
    79 			} else {
    80 				break;
    81 			}
    82 		}
    83 	}
    84 }
    85 
    86 bool UTIL_PathCmp(const char *path1, const char *path2)
    87 {
    88 	size_t pos1 = 0, pos2 = 0;
    89 
    90 	while (true)
    91 	{
    92 		if (path1[pos1] == '\0' || path2[pos2] == '\0')
    93 		{
    94 			return (path1[pos1] == path2[pos2]);
    95 		}
    96 
    97 		if (path1[pos1] == PATH_SEP_CHAR)
    98 		{
    99 			if (path2[pos2] != PATH_SEP_CHAR)
   100 			{
   101 				return false;
   102 			}
   103 
   104 			/* Look for extra path chars */
   105 			while (path1[++pos1])
   106 			{
   107 				if (path1[pos1] != PATH_SEP_CHAR)
   108 				{
   109 					break;
   110 				}
   111 			}
   112 			while (path2[++pos2])
   113 			{
   114 				if (path2[pos2] != PATH_SEP_CHAR)
   115 				{
   116 					break;
   117 				}
   118 			}
   119 			continue;
   120 		}
   121 
   122 		/* If we're at a different non-alphanumeric, the next character MUST match */
   123 		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
   124 		{
   125 			return false;
   126 		}
   127 
   128 	#ifdef WIN32
   129 		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   130 	#else
   131 		if (path1[pos1] != path2[pos2])
   132 	#endif
   133 		{
   134 			return false;
   135 		}
   136 
   137 		pos1++;
   138 		pos2++;
   139 	}
   140 }
   141 
   142 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   143 {
   144 	va_list ap;
   145 
   146 	va_start(ap, fmt);
   147 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   148 	va_end(ap);
   149 
   150 	if (len >= maxlength)
   151 	{
   152 		len = maxlength - 1;
   153 		buffer[len] = '\0';
   154 	}
   155 
   156 	return len;
   157 }
   158 
   159 inline bool pathchar_isalpha(char a)
   160 {
   161 	return (((a & 1<<7) == 0) && isalpha(a));
   162 }
   163 
   164 inline bool pathchar_sep(char a)
   165 {
   166 #if defined WIN32
   167 	return (a == '/' || a == '\\');
   168 #elif defined __linux__
   169 	return (a == '/');
   170 #endif
   171 }
   172 
   173 inline bool pathstr_isabsolute(const char *str)
   174 {
   175 #if defined WIN32
   176 	return (pathchar_isalpha(str[0]) 
   177 		&& str[1] == ':' 
   178 		&& pathchar_sep(str[2]));
   179 #elif defined __linux__
   180 	return (str[0] == '/');
   181 #endif
   182 }
   183 
   184 inline bool pathchar_cmp(char a, char b)
   185 {
   186 #if defined WIN32
   187 	if (pathchar_isalpha(a) && pathchar_isalpha(b))
   188 	{
   189 		return (tolower(a) == tolower(b));
   190 	}
   191 	/* Either path separator is acceptable */
   192 	if (pathchar_sep(a))
   193 	{
   194 		return pathchar_sep(b);
   195 	}
   196 #endif
   197 	return (a == b);
   198 }
   199 
   200 /**
   201  * @brief Forms a relative path given two absolute paths.
   202  *
   203  * @param buffer		Buffer to store relative path in.
   204  * @param maxlength		Maximum length of the output buffer.
   205  * @param relTo			Destination folder to use as a working directory.
   206  *						Final folder name should not be pathchar-terminated.
   207  * @param relFrom		Source file or folder to use as a target.
   208  * @return				True on success, false on failure.
   209  */
   210 bool UTIL_Relatize(char buffer[],
   211 				   size_t maxlength,
   212 				   const char *relTo,
   213 				   const char *relFrom)
   214 {
   215 	/* We don't allow relative paths in here, force
   216 	 * the user to resolve these himself!
   217 	 */
   218 	if (!pathstr_isabsolute(relTo)
   219 		|| !pathstr_isabsolute(relFrom))
   220 	{
   221 		return false;
   222 	}
   223 
   224 #if defined WIN32
   225 	/* Relative paths across drives are not possible */
   226 	if (!pathchar_cmp(relTo[0], relFrom[0]))
   227 	{
   228 		return false;
   229 	}
   230 	/* Get rid of the drive and semicolon part */
   231 	relTo = &relTo[2];
   232 	relFrom = &relFrom[2];
   233 #endif
   234 
   235 	/* Eliminate the common root between the paths */
   236 	const char *rootTo = NULL;
   237 	const char *rootFrom = NULL;
   238 	while (*relTo != '\0' && *relFrom != '\0')
   239 	{
   240 		/* If we get to a new path sequence, start over */
   241 		if (pathchar_sep(*relTo)
   242 			&& pathchar_sep(*relFrom))
   243 		{
   244 			rootTo = relTo;
   245 			rootFrom = relFrom;
   246 		/* If the paths don't compare, stop looking for a common root */
   247 		} else if (!pathchar_cmp(*relTo, *relFrom)) {
   248 			break;
   249 		}
   250 		relTo++;
   251 		relFrom++;
   252 	}
   253 
   254 	/* NULLs shouldn't happen! */
   255 	if (rootTo == NULL
   256 		|| rootFrom == NULL)
   257 	{
   258 		return false;
   259 	}
   260 
   261 	size_t numLevels = 0;
   262 
   263 	/* The root case is special! 
   264 	 * Don't count anything from it.
   265 	 */
   266 	if (*(rootTo + 1) != '\0')
   267 	{
   268 		/* Search for how many levels we need to go up.
   269 	 	 * Since the root pointer points to a '/', we increment
   270 		 * the initial pointer by one.
   271 		 */
   272 		while (*rootTo != '\0')
   273 		{
   274 			if (pathchar_sep(*rootTo))
   275 			{
   276 				/* Check for an improper trailing slash,
   277 				 * just to be nice even though the user 
   278 				 * should NOT have done this!
   279 				 */
   280 				if (*(rootTo + 1) == '\0')
   281 				{
   282 					break;
   283 				}
   284 				numLevels++;
   285 			}
   286 			rootTo++;
   287 		}
   288 	}
   289 
   290 	/* Now build the new relative path. */
   291 	size_t len, total = 0;
   292 	while (numLevels--)
   293 	{
   294 		len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
   295 		if (len >= maxlength - total)
   296 		{
   297 			/* Not enough space in the buffer */
   298 			return false;
   299 		}
   300 		total += len;
   301 	}
   302 
   303 	/* Add the absolute path. */
   304 	len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
   305 	if (len >= maxlength - total)
   306 	{
   307 		return false;
   308 	}
   309 
   310 	return true;
   311 }
   312 
   313 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
   314 {
   315 	size_t len = vsnprintf(buffer, maxlength, fmt, params);
   316 
   317 	if (len >= maxlength)
   318 	{
   319 		len = maxlength - 1;
   320 		buffer[len] = '\0';
   321 	}
   322 
   323 	return len;
   324 }
   325 
   326 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len)
   327 {
   328 	unsigned char *addr1 = (unsigned char *) addr;
   329 	unsigned char *addr2 = (unsigned char *) sig;
   330 
   331 	for (size_t i = 0; i < len; i++)
   332 	{
   333 		if (addr2[i] == '*')
   334 			continue;
   335 		if (addr1[i] != addr2[i])
   336 			return false;
   337 	}
   338 
   339 	return true;
   340 }