sourcemm/util.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 06 00:59:01 2005 +0000 (2005-10-06)
changeset 122 7c36121e2dfe
parent 84 3f77bc9e64d8
child 166 e85ebcf21a7e
permissions -rw-r--r--
Initial import of new GameDLL loading code
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 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 <stdarg.h>
    14 #include <stdio.h>
    15 #include "util.h"
    16 #include "oslink.h"
    17 
    18 /**
    19  * @brief Utility functons
    20  * @file util.cpp
    21  */
    22 
    23 /* UTIL_GetExtension
    24  * Returns a pointer to the extension in a file name
    25  */
    26 const char *UTIL_GetExtension(const char *file)
    27 {
    28 	int len = strlen(file);
    29 	int i = 0;
    30 
    31 	for (i=len-1; i>=0; i--)
    32 	{
    33 		if (file[i] == '/' || file[i] == '\\')
    34 			return NULL;
    35 
    36 		if ((file[i] == '.') && (i != len-1))
    37 		{
    38 			return (const char *)&(file[i+1]);
    39 		}
    40 	}
    41 
    42 	return NULL;
    43 }
    44 
    45 /* UTIL_TrimLeft
    46  * Removes whitespace characters from left side of string
    47  */
    48 void UTIL_TrimLeft(char *buffer)
    49 {
    50 	// Let's think of this as our iterator
    51 	char *i = buffer;
    52 
    53 	// Make sure the buffer isn't null
    54 	if (i && *i)
    55 	{
    56 		// Add up number of whitespace characters
    57 		while(isspace(*i))
    58 			i++;
    59 
    60 		// If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
    61 		// :TODO: change this to not use memcpy()!
    62 		if (i != buffer)
    63 			memcpy(buffer, i, (strlen(i) + 1) * sizeof(char));
    64 	}
    65 }
    66 
    67 //:TODO: this should skip string literals
    68 void UTIL_TrimComments(char *buffer)
    69 {
    70 	int num_sc = 0;
    71 	size_t len = strlen(buffer);
    72 	if (buffer)
    73 	{
    74 		for (int i=(int)len-1; i>=0; i--)
    75 		{
    76 			if (buffer[i] == '/')
    77 			{
    78 				if (++num_sc >= 2 && i==0)
    79 				{
    80 					buffer[i] = '\0';
    81 					return;
    82 				}
    83 			} else {
    84 				if (num_sc >= 2)
    85 				{
    86 					buffer[i] = '\0';
    87 					return;
    88 				}
    89 				num_sc = 0;
    90 			}
    91 			//size_t won't go below 0, manually break out
    92 			if (i == 0)
    93 				break;
    94 		}
    95 	}
    96 }
    97 
    98 /* UTIL_TrimLeft
    99  * Removes whitespace characters from right side of string
   100  */
   101 void UTIL_TrimRight(char *buffer)
   102 {
   103 	// Make sure buffer isn't null
   104 	if (buffer)
   105 	{
   106 		// Loop through buffer backwards while replacing whitespace chars with null chars
   107 		for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
   108 		{
   109 			if (isspace(buffer[i]))
   110 				buffer[i] = '\0';
   111 			else
   112 				break;
   113 		}
   114 	}
   115 }
   116 
   117 /* UTIL_KeySplit
   118  * Breaks a string at the first space until it reaches a nonspace
   119  */
   120 void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   121 {
   122 	size_t start;
   123 	size_t len = strlen(str);
   124 
   125 	for (start=0; start<len; start++)
   126 	{
   127 		if (!isspace(str[start]))
   128 			break;
   129 	}
   130 
   131 	size_t end;
   132 	for (end=start; end<len; end++)
   133 	{
   134 		if (isspace(str[end]))
   135 			break;
   136 	}
   137 	
   138 	size_t i, c=0;
   139 	for (i=start; i<end; i++,c++)
   140 	{
   141 		if (c >= len1)
   142 			break;
   143 		buf1[c] = str[i];
   144 	}
   145 	buf1[c] = '\0';
   146 
   147 	for (start=end; start<len; start++)
   148 	{
   149 		if (!isspace(str[start]))
   150 			break;
   151 	}
   152 
   153 	for (c=0; start<len; start++,c++)
   154 	{
   155 		if (c >= len2)
   156 			break;
   157 		buf2[c] = str[start];
   158 	}
   159 	buf2[c] = '\0';
   160 }
   161 
   162 /** 
   163  * Formats a path name for an OS
   164  */
   165 void UTIL_PathFmt(char *buffer, size_t len, const char *fmt, ...)
   166 {
   167 	va_list ap;
   168 	va_start(ap,fmt);
   169 	size_t mylen = vsnprintf(buffer, len, fmt, ap);
   170 	va_end(ap);
   171 
   172 	for (size_t i=0; i<mylen; i++)
   173 	{
   174 		if (buffer[i] == ALT_SEP_CHAR)
   175 			buffer[i] = PATH_SEP_CHAR;
   176 	}
   177 }
   178 
   179 bool UTIL_PathCmp(const char *path1, const char *path2)
   180 {
   181 	size_t len1=strlen(path1);
   182 	size_t len2=strlen(path2);
   183 	size_t pos1=0,pos2=0;
   184 
   185 	while (true)
   186 	{
   187 		if (path1[pos1] == '\0' || path2[pos2] == '\0')
   188 		{
   189 			return (path1[pos1] == path2[pos2]);
   190 		}
   191 
   192 		if (path1[pos1] == PATH_SEP_CHAR)
   193 		{
   194 			if (path2[pos2] != PATH_SEP_CHAR)
   195 				return false;
   196 			//look for extra path chars
   197 			while (path1[++pos1])
   198 			{
   199 				if (path1[pos1] != PATH_SEP_CHAR)
   200 					break;
   201 			}
   202 			while (path2[++pos2])
   203 			{
   204 				if (path2[pos2] != PATH_SEP_CHAR)
   205 					break;
   206 			}
   207 			continue;
   208 		}
   209 
   210 		//if we're at a different non-alphanumeric, the next character MUST Match
   211 		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
   212 			return false;
   213 
   214 #ifdef WIN32
   215 		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   216 #else
   217 		if (path1[pos1] != path2[pos2])
   218 #endif
   219 			return false;
   220 		pos1++;
   221 		pos2++;
   222 	}
   223 }