Bumped all versions.
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2008 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
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 bool UTIL_PathCmp(const char *path1, const char *path2)
87 size_t pos1 = 0, pos2 = 0;
91 if (path1[pos1] == '\0' || path2[pos2] == '\0')
93 return (path1[pos1] == path2[pos2]);
96 if (path1[pos1] == PATH_SEP_CHAR)
98 if (path2[pos2] != PATH_SEP_CHAR)
103 /* Look for extra path chars */
104 while (path1[++pos1])
106 if (path1[pos1] != PATH_SEP_CHAR)
111 while (path2[++pos2])
113 if (path2[pos2] != PATH_SEP_CHAR)
121 /* If we're at a different non-alphanumeric, the next character MUST match */
122 if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
128 if (toupper(path1[pos1]) != toupper(path2[pos2]))
130 if (path1[pos1] != path2[pos2])
141 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
146 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
149 if (len >= maxlength)
158 inline bool pathchar_isalpha(char a)
160 return (((a & 1<<7) == 0) && isalpha(a));
163 inline bool pathchar_sep(char a)
166 return (a == '/' || a == '\\');
167 #elif defined __linux__
172 inline bool pathstr_isabsolute(const char *str)
175 return (pathchar_isalpha(str[0])
177 && pathchar_sep(str[2]));
178 #elif defined __linux__
179 return (str[0] == '/');
183 inline bool pathchar_cmp(char a, char b)
186 if (pathchar_isalpha(a) && pathchar_isalpha(b))
188 return (tolower(a) == tolower(b));
190 /* Either path separator is acceptable */
193 return pathchar_sep(b);
200 * @brief Forms a relative path given two absolute paths.
202 * @param buffer Buffer to store relative path in.
203 * @param maxlength Maximum length of the output buffer.
204 * @param relTo Destination folder to use as a working directory.
205 * Final folder name should not be pathchar-terminated.
206 * @param relFrom Source file or folder to use as a target.
207 * @return True on success, false on failure.
209 bool UTIL_Relatize(char buffer[],
214 /* We don't allow relative paths in here, force
215 * the user to resolve these himself!
217 if (!pathstr_isabsolute(relTo)
218 || !pathstr_isabsolute(relFrom))
224 /* Relative paths across drives are not possible */
225 if (!pathchar_cmp(relTo[0], relFrom[0]))
229 /* Get rid of the drive and semicolon part */
231 relFrom = &relFrom[2];
234 /* Eliminate the common root between the paths */
235 const char *rootTo = NULL;
236 const char *rootFrom = NULL;
237 while (*relTo != '\0' && *relFrom != '\0')
239 /* If we get to a new path sequence, start over */
240 if (pathchar_sep(*relTo)
241 && pathchar_sep(*relFrom))
245 /* If the paths don't compare, stop looking for a common root */
246 } else if (!pathchar_cmp(*relTo, *relFrom)) {
253 /* NULLs shouldn't happen! */
260 size_t numLevels = 0;
262 /* The root case is special!
263 * Don't count anything from it.
265 if (*(rootTo + 1) != '\0')
267 /* Search for how many levels we need to go up.
268 * Since the root pointer points to a '/', we increment
269 * the initial pointer by one.
271 while (*rootTo != '\0')
273 if (pathchar_sep(*rootTo))
275 /* Check for an improper trailing slash,
276 * just to be nice even though the user
277 * should NOT have done this!
279 if (*(rootTo + 1) == '\0')
289 /* Now build the new relative path. */
290 size_t len, total = 0;
293 len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
294 if (len >= maxlength - total)
296 /* Not enough space in the buffer */
302 /* Add the absolute path. */
303 len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
304 if (len >= maxlength - total)
312 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
314 size_t len = vsnprintf(buffer, maxlength, fmt, params);
316 if (len >= maxlength)
325 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len)
327 unsigned char *addr1 = (unsigned char *) addr;
328 unsigned char *addr2 = (unsigned char *) sig;
330 for (size_t i = 0; i < len; i++)
334 if (addr1[i] != addr2[i])