sourcemm/util.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 15 19:30:22 2006 +0000 (2006-02-15)
changeset 183 df9dd785ad3e
parent 175 b497b7777f6b
child 238 c27105949293
permissions -rw-r--r--
updated changelog
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 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 bool UTIL_PathCmp(const char *path1, const char *path2)
   163 {
   164 	size_t len1=strlen(path1);
   165 	size_t len2=strlen(path2);
   166 	size_t pos1=0,pos2=0;
   167 
   168 	while (true)
   169 	{
   170 		if (path1[pos1] == '\0' || path2[pos2] == '\0')
   171 		{
   172 			return (path1[pos1] == path2[pos2]);
   173 		}
   174 
   175 		if (path1[pos1] == PATH_SEP_CHAR)
   176 		{
   177 			if (path2[pos2] != PATH_SEP_CHAR)
   178 				return false;
   179 			//look for extra path chars
   180 			while (path1[++pos1])
   181 			{
   182 				if (path1[pos1] != PATH_SEP_CHAR)
   183 					break;
   184 			}
   185 			while (path2[++pos2])
   186 			{
   187 				if (path2[pos2] != PATH_SEP_CHAR)
   188 					break;
   189 			}
   190 			continue;
   191 		}
   192 
   193 		//if we're at a different non-alphanumeric, the next character MUST Match
   194 		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
   195 			return false;
   196 
   197 #ifdef WIN32
   198 		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   199 #else
   200 		if (path1[pos1] != path2[pos2])
   201 #endif
   202 			return false;
   203 		pos1++;
   204 		pos2++;
   205 	}
   206 }