1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2006 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
19 * @brief Utility functons
24 * Returns a pointer to the extension in a file name
26 const char *UTIL_GetExtension(const char *file)
28 int len = strlen(file);
31 for (i=len-1; i>=0; i--)
33 if (file[i] == '/' || file[i] == '\\')
36 if ((file[i] == '.') && (i != len-1))
38 return (const char *)&(file[i+1]);
46 * Removes whitespace characters from left side of string
48 void UTIL_TrimLeft(char *buffer)
50 // Let's think of this as our iterator
53 // Make sure the buffer isn't null
56 // Add up number of whitespace characters
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()!
63 memcpy(buffer, i, (strlen(i) + 1) * sizeof(char));
67 //:TODO: this should skip string literals
68 void UTIL_TrimComments(char *buffer)
71 size_t len = strlen(buffer);
74 for (int i=(int)len-1; i>=0; i--)
78 if (++num_sc >= 2 && i==0)
91 //size_t won't go below 0, manually break out
99 * Removes whitespace characters from right side of string
101 void UTIL_TrimRight(char *buffer)
103 // Make sure buffer isn't null
106 // Loop through buffer backwards while replacing whitespace chars with null chars
107 for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
109 if (isspace(buffer[i]))
118 * Breaks a string at the first space until it reaches a nonspace
120 void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
123 size_t len = strlen(str);
125 for (start=0; start<len; start++)
127 if (!isspace(str[start]))
132 for (end=start; end<len; end++)
134 if (isspace(str[end]))
139 for (i=start; i<end; i++,c++)
147 for (start=end; start<len; start++)
149 if (!isspace(str[start]))
153 for (c=0; start<len; start++,c++)
157 buf2[c] = str[start];
162 bool UTIL_PathCmp(const char *path1, const char *path2)
164 size_t len1=strlen(path1);
165 size_t len2=strlen(path2);
166 size_t pos1=0,pos2=0;
170 if (path1[pos1] == '\0' || path2[pos2] == '\0')
172 return (path1[pos1] == path2[pos2]);
175 if (path1[pos1] == PATH_SEP_CHAR)
177 if (path2[pos2] != PATH_SEP_CHAR)
179 //look for extra path chars
180 while (path1[++pos1])
182 if (path1[pos1] != PATH_SEP_CHAR)
185 while (path2[++pos2])
187 if (path2[pos2] != PATH_SEP_CHAR)
193 //if we're at a different non-alphanumeric, the next character MUST Match
194 if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
198 if (toupper(path1[pos1]) != toupper(path2[pos2]))
200 if (path1[pos1] != path2[pos2])