1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 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];
163 * Formats a path name for an OS
165 void UTIL_PathFmt(char *buffer, size_t len, const char *fmt, ...)
169 size_t mylen = vsnprintf(buffer, len, fmt, ap);
172 for (size_t i=0; i<mylen; i++)
174 if (buffer[i] == ALT_SEP_CHAR)
175 buffer[i] = PATH_SEP_CHAR;
179 bool UTIL_PathCmp(const char *path1, const char *path2)
181 size_t len1=strlen(path1);
182 size_t len2=strlen(path2);
183 size_t pos1=0,pos2=0;
187 if (path1[pos1] == '\0' || path2[pos2] == '\0')
189 return (path1[pos1] == path2[pos2]);
192 if (path1[pos1] == PATH_SEP_CHAR)
194 if (path2[pos2] != PATH_SEP_CHAR)
196 //look for extra path chars
197 while (path1[++pos1])
199 if (path1[pos1] != PATH_SEP_CHAR)
202 while (path2[++pos2])
204 if (path2[pos2] != PATH_SEP_CHAR)
210 //if we're at a different non-alphanumeric, the next character MUST Match
211 if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
215 if (toupper(path1[pos1]) != toupper(path2[pos2]))
217 if (path1[pos1] != path2[pos2])