Changed license in sourcehook_hookmangen_x86.h (agreed to by BAILOPAN and faluco)
Updated MSVC7 proj file
3 * ======================================================
5 * Copyright (C) 2004-2007 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 /* :TODO: this should skip string literals */
103 void UTIL_TrimComments(char *buffer)
106 size_t len = strlen(buffer);
109 for (int i = len - 1; i >= 0; i--)
111 if (buffer[i] == '/')
113 if (++num_sc >= 2 && i==0)
126 /* size_t won't go below 0, manually break out */
135 void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
138 size_t len = strlen(str);
140 for (start = 0; start < len; start++)
142 if (!isspace(str[start]))
149 for (end = start; end < len; end++)
151 if (isspace(str[end]))
158 for (i = start; i < end; i++, c++)
168 for (start = end; start < len; start++)
170 if (!isspace(str[start]))
176 for (c = 0; start < len; start++, c++)
182 buf2[c] = str[start];
187 bool UTIL_PathCmp(const char *path1, const char *path2)
189 size_t pos1 = 0, pos2 = 0;
193 if (path1[pos1] == '\0' || path2[pos2] == '\0')
195 return (path1[pos1] == path2[pos2]);
198 if (path1[pos1] == PATH_SEP_CHAR)
200 if (path2[pos2] != PATH_SEP_CHAR)
205 /* Look for extra path chars */
206 while (path1[++pos1])
208 if (path1[pos1] != PATH_SEP_CHAR)
213 while (path2[++pos2])
215 if (path2[pos2] != PATH_SEP_CHAR)
223 /* If we're at a different non-alphanumeric, the next character MUST match */
224 if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
230 if (toupper(path1[pos1]) != toupper(path2[pos2]))
232 if (path1[pos1] != path2[pos2])
243 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
248 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
251 if (len >= maxlength)
260 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
262 size_t len = vsnprintf(buffer, maxlength, fmt, params);
264 if (len >= maxlength)
273 inline bool pathchar_isalpha(char a)
275 return (((a & 1<<7) == 0) && isalpha(a));
278 inline bool pathchar_sep(char a)
281 return (a == '/' || a == '\\');
282 #elif defined __linux__
287 inline bool pathstr_isabsolute(const char *str)
290 return (pathchar_isalpha(str[0])
292 && pathchar_sep(str[2]));
293 #elif defined __linux__
294 return (str[0] == '/');
298 inline bool pathchar_cmp(char a, char b)
301 if (pathchar_isalpha(a) && pathchar_isalpha(b))
303 return (tolower(a) == tolower(b));
305 /* Either path separator is acceptable */
308 return pathchar_sep(b);
315 * @brief Forms a relative path given two absolute paths.
317 * @param buffer Buffer to store relative path in.
318 * @param maxlength Maximum length of the output buffer.
319 * @param relTo Destination folder to use as a working directory.
320 * Final folder name should not be pathchar-terminated.
321 * @param relFrom Source file or folder to use as a target.
322 * @return True on success, false on failure.
324 bool UTIL_Relatize(char buffer[],
329 /* We don't allow relative paths in here, force
330 * the user to resolve these himself!
332 if (!pathstr_isabsolute(relTo)
333 || !pathstr_isabsolute(relFrom))
339 /* Relative paths across drives are not possible */
340 if (!pathchar_cmp(relTo[0], relFrom[0]))
344 /* Get rid of the drive and semicolon part */
346 relFrom = &relFrom[2];
349 /* Eliminate the common root between the paths */
350 const char *rootTo = NULL;
351 const char *rootFrom = NULL;
352 while (*relTo != '\0' && *relFrom != '\0')
354 /* If we get to a new path sequence, start over */
355 if (pathchar_sep(*relTo)
356 && pathchar_sep(*relFrom))
360 /* If the paths don't compare, stop looking for a common root */
361 } else if (!pathchar_cmp(*relTo, *relFrom)) {
368 /* NULLs shouldn't happen! */
375 size_t numLevels = 0;
377 /* The root case is special!
378 * Don't count anything from it.
380 if (*(rootTo + 1) != '\0')
382 /* Search for how many levels we need to go up.
383 * Since the root pointer points to a '/', we increment
384 * the initial pointer by one.
386 while (*rootTo != '\0')
388 if (pathchar_sep(*rootTo))
390 /* Check for an improper trailing slash,
391 * just to be nice even though the user
392 * should NOT have done this!
394 if (*(rootTo + 1) == '\0')
404 /* Now build the new relative path. */
405 size_t len, total = 0;
408 len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
409 if (len >= maxlength - total)
411 /* Not enough space in the buffer */
417 /* Add the absolute path. */
418 len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
419 if (len >= maxlength - total)