nearly complete rewrite of Metamod:Source, API is the same but internally all engine/game functionality is abstracted. i also renamed files to make everything look a bit more conformant
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
14 #include "metamod_util.h"
18 * @brief Utility functions
22 const char *UTIL_GetExtension(const char *file)
24 int len = strlen(file);
27 for (i = len - 1; i >= 0; i--)
29 if (file[i] == '/' || file[i] == '\\')
34 if ((file[i] == '.') && (i != len - 1))
36 return (const char *)&(file[i + 1]);
43 void UTIL_TrimLeft(char *buffer)
45 /* Let's think of this as our iterator */
48 /* Make sure the buffer isn't null */
51 /* Add up number of whitespace characters */
52 while(isspace((unsigned char) *i))
57 /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
60 memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
65 void UTIL_TrimRight(char *buffer)
67 /* Make sure buffer isn't null */
70 size_t len = strlen(buffer);
72 /* Loop through buffer backwards while replacing whitespace chars with null chars */
73 for (size_t i = len - 1; i >= 0; i--)
75 if (isspace((unsigned char) buffer[i]))
85 /* :TODO: this should skip string literals */
86 void UTIL_TrimComments(char *buffer)
89 size_t len = strlen(buffer);
92 for (int i = len - 1; i >= 0; i--)
96 if (++num_sc >= 2 && i==0)
109 /* size_t won't go below 0, manually break out */
118 void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
121 size_t len = strlen(str);
123 for (start = 0; start < len; start++)
125 if (!isspace(str[start]))
132 for (end = start; end < len; end++)
134 if (isspace(str[end]))
141 for (i = start; i < end; i++, c++)
151 for (start = end; start < len; start++)
153 if (!isspace(str[start]))
159 for (c = 0; start < len; start++, c++)
165 buf2[c] = str[start];
170 bool UTIL_PathCmp(const char *path1, const char *path2)
172 size_t pos1 = 0, pos2 = 0;
176 if (path1[pos1] == '\0' || path2[pos2] == '\0')
178 return (path1[pos1] == path2[pos2]);
181 if (path1[pos1] == PATH_SEP_CHAR)
183 if (path2[pos2] != PATH_SEP_CHAR)
188 /* Look for extra path chars */
189 while (path1[++pos1])
191 if (path1[pos1] != PATH_SEP_CHAR)
196 while (path2[++pos2])
198 if (path2[pos2] != PATH_SEP_CHAR)
206 /* If we're at a different non-alphanumeric, the next character MUST match */
207 if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
213 if (toupper(path1[pos1]) != toupper(path2[pos2]))
215 if (path1[pos1] != path2[pos2])
226 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
231 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
234 if (len >= maxlength)
243 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
245 size_t len = vsnprintf(buffer, maxlength, fmt, params);
247 if (len >= maxlength)
256 inline bool pathchar_isalpha(char a)
258 return (((a & 1<<7) == 0) && isalpha(a));
261 inline bool pathchar_sep(char a)
264 return (a == '/' || a == '\\');
265 #elif defined __linux__
270 inline bool pathstr_isabsolute(const char *str)
273 return (pathchar_isalpha(str[0])
275 && pathchar_sep(str[2]));
276 #elif defined __linux__
277 return (str[0] == '/');
281 inline bool pathchar_cmp(char a, char b)
284 if (pathchar_isalpha(a) && pathchar_isalpha(b))
286 return (tolower(a) == tolower(b));
288 /* Either path separator is acceptable */
291 return pathchar_sep(b);
298 * @brief Forms a relative path given two absolute paths.
300 * @param buffer Buffer to store relative path in.
301 * @param maxlength Maximum length of the output buffer.
302 * @param relTo Destination folder to use as a working directory.
303 * Final folder name should not be pathchar-terminated.
304 * @param relFrom Source file or folder to use as a target.
305 * @return True on success, false on failure.
307 bool UTIL_Relatize(char buffer[],
312 /* We don't allow relative paths in here, force
313 * the user to resolve these himself!
315 if (!pathstr_isabsolute(relTo)
316 || !pathstr_isabsolute(relFrom))
322 /* Relative paths across drives are not possible */
323 if (!pathchar_cmp(relTo[0], relFrom[0]))
327 /* Get rid of the drive and semicolon part */
329 relFrom = &relFrom[2];
332 /* Eliminate the common root between the paths */
333 const char *rootTo = NULL;
334 const char *rootFrom = NULL;
335 while (*relTo != '\0' && *relFrom != '\0')
337 /* If we get to a new path sequence, start over */
338 if (pathchar_sep(*relTo)
339 && pathchar_sep(*relFrom))
343 /* If the paths don't compare, stop looking for a common root */
344 } else if (!pathchar_cmp(*relTo, *relFrom)) {
351 /* NULLs shouldn't happen! */
358 size_t numLevels = 0;
360 /* The root case is special!
361 * Don't count anything from it.
363 if (*(rootTo + 1) != '\0')
365 /* Search for how many levels we need to go up.
366 * Since the root pointer points to a '/', we increment
367 * the initial pointer by one.
369 while (*rootTo != '\0')
371 if (pathchar_sep(*rootTo))
373 /* Check for an improper trailing slash,
374 * just to be nice even though the user
375 * should NOT have done this!
377 if (*(rootTo + 1) == '\0')
387 /* Now build the new relative path. */
388 size_t len, total = 0;
391 len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
392 if (len >= maxlength - total)
394 /* Not enough space in the buffer */
400 /* Add the absolute path. */
401 len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
402 if (len >= maxlength - total)