Fixed problem on OBV where alternately loaded plugins (CreateInterface_MMS) could be incorrectly told they were on the OB engine (
bug 4491, r=dvander).
This only happened when there was more than one plugin that loaded this way. And the first plugin to load had to fail on OBV first so that OB could be tried. It was being permanently set to OB for future plugin loads and this caused problems.
3 * ======================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
31 #include "metamod_util.h"
32 #include "metamod_oslink.h"
35 * @brief Utility functions
39 const char *UTIL_GetExtension(const char *file)
41 int len = strlen(file);
44 for (i = len - 1; i >= 0; i--)
46 if (file[i] == '/' || file[i] == '\\')
51 if ((file[i] == '.') && (i != len - 1))
53 return (const char *)&(file[i + 1]);
60 void UTIL_TrimLeft(char *buffer)
62 /* Let's think of this as our iterator */
65 /* Make sure the buffer isn't null */
68 /* Add up number of whitespace characters */
69 while(isspace((unsigned char) *i))
74 /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
77 memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
82 void UTIL_TrimRight(char *buffer)
84 /* Make sure buffer isn't null */
87 size_t len = strlen(buffer);
89 /* Loop through buffer backwards while replacing whitespace chars with null chars */
90 for (size_t i = len - 1; i >= 0; i--)
92 if (isspace((unsigned char) buffer[i]))
102 bool UTIL_PathCmp(const char *path1, const char *path2)
104 size_t pos1 = 0, pos2 = 0;
108 if (path1[pos1] == '\0' || path2[pos2] == '\0')
110 return (path1[pos1] == path2[pos2]);
113 if (path1[pos1] == PATH_SEP_CHAR)
115 if (path2[pos2] != PATH_SEP_CHAR)
120 /* Look for extra path chars */
121 while (path1[++pos1])
123 if (path1[pos1] != PATH_SEP_CHAR)
128 while (path2[++pos2])
130 if (path2[pos2] != PATH_SEP_CHAR)
138 /* If we're at a different non-alphanumeric, the next character MUST match */
139 if ((((unsigned)path1[pos1] & 0x80) && path1[pos1] != path2[pos2])
141 !isalpha(path1[pos1]) && (path1[pos1] != path2[pos2])
148 if (toupper(path1[pos1]) != toupper(path2[pos2]))
150 if (path1[pos1] != path2[pos2])
161 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
166 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
169 if (len >= maxlength)
178 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
180 size_t len = vsnprintf(buffer, maxlength, fmt, params);
182 if (len >= maxlength)
191 inline bool pathchar_isalpha(char a)
193 return (((a & 1<<7) == 0) && isalpha(a));
196 inline bool pathchar_sep(char a)
199 return (a == '/' || a == '\\');
205 inline bool pathstr_isabsolute(const char *str)
208 return (pathchar_isalpha(str[0])
210 && pathchar_sep(str[2]));
212 return (str[0] == '/');
216 inline bool pathchar_cmp(char a, char b)
219 if (pathchar_isalpha(a) && pathchar_isalpha(b))
221 return (tolower(a) == tolower(b));
223 /* Either path separator is acceptable */
226 return pathchar_sep(b);
233 * @brief Forms a relative path given two absolute paths.
235 * @param buffer Buffer to store relative path in.
236 * @param maxlength Maximum length of the output buffer.
237 * @param relTo Destination folder to use as a working directory.
238 * Final folder name should not be pathchar-terminated.
239 * @param relFrom Source file or folder to use as a target.
240 * @return True on success, false on failure.
242 bool UTIL_Relatize(char buffer[],
247 /* We don't allow relative paths in here, force
248 * the user to resolve these himself!
250 if (!pathstr_isabsolute(relTo)
251 || !pathstr_isabsolute(relFrom))
257 /* Relative paths across drives are not possible */
258 if (!pathchar_cmp(relTo[0], relFrom[0]))
262 /* Get rid of the drive and semicolon part */
264 relFrom = &relFrom[2];
267 /* Eliminate the common root between the paths */
268 const char *rootTo = NULL;
269 const char *rootFrom = NULL;
270 while (*relTo != '\0' && *relFrom != '\0')
272 /* If we get to a new path sequence, start over */
273 if (pathchar_sep(*relTo)
274 && pathchar_sep(*relFrom))
278 /* If the paths don't compare, stop looking for a common root */
279 } else if (!pathchar_cmp(*relTo, *relFrom)) {
286 /* NULLs shouldn't happen! */
293 size_t numLevels = 0;
295 /* The root case is special!
296 * Don't count anything from it.
298 if (*(rootTo + 1) != '\0')
300 /* Search for how many levels we need to go up.
301 * Since the root pointer points to a '/', we increment
302 * the initial pointer by one.
304 while (*rootTo != '\0')
306 if (pathchar_sep(*rootTo))
308 /* Check for an improper trailing slash,
309 * just to be nice even though the user
310 * should NOT have done this!
312 if (*(rootTo + 1) == '\0')
322 /* Now build the new relative path. */
323 size_t len, total = 0;
326 len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
327 if (len >= maxlength - total)
329 /* Not enough space in the buffer */
335 /* Add the absolute path. */
336 len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
337 if (len >= maxlength - total)
345 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len)
347 unsigned char *addr1 = (unsigned char *) addr;
348 unsigned char *addr2 = (unsigned char *) sig;
350 for (size_t i = 0; i < len; i++)
354 if (addr1[i] != addr2[i])