1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2008 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
19 * @brief Utility functions
23 const char *UTIL_GetExtension(const char *file)
25 int len = strlen(file);
28 for (i = len - 1; i >= 0; i--)
30 if (file[i] == '/' || file[i] == '\\')
35 if ((file[i] == '.') && (i != len - 1))
37 return (const char *)&(file[i + 1]);
44 void UTIL_TrimLeft(char *buffer)
46 /* Let's think of this as our iterator */
49 /* Make sure the buffer isn't null */
52 /* Add up number of whitespace characters */
53 while(isspace((unsigned char) *i))
58 /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
61 memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
66 void UTIL_TrimRight(char *buffer)
68 /* Make sure buffer isn't null */
71 size_t len = strlen(buffer);
73 /* Loop through buffer backwards while replacing whitespace chars with null chars */
74 for (size_t i = len - 1; i < len; i--)
76 if (isspace((unsigned char) buffer[i]))
86 bool UTIL_PathCmp(const char *path1, const char *path2)
88 size_t pos1 = 0, pos2 = 0;
92 if (path1[pos1] == '\0' || path2[pos2] == '\0')
94 return (path1[pos1] == path2[pos2]);
97 if (path1[pos1] == PATH_SEP_CHAR)
99 if (path2[pos2] != PATH_SEP_CHAR)
104 /* Look for extra path chars */
105 while (path1[++pos1])
107 if (path1[pos1] != PATH_SEP_CHAR)
112 while (path2[++pos2])
114 if (path2[pos2] != PATH_SEP_CHAR)
122 /* If we're at a different non-alphanumeric, the next character MUST match */
123 if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
129 if (toupper(path1[pos1]) != toupper(path2[pos2]))
131 if (path1[pos1] != path2[pos2])
142 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
147 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
150 if (len >= maxlength)
159 inline bool pathchar_isalpha(char a)
161 return (((a & 1<<7) == 0) && isalpha(a));
164 inline bool pathchar_sep(char a)
167 return (a == '/' || a == '\\');
168 #elif defined __linux__
173 inline bool pathstr_isabsolute(const char *str)
176 return (pathchar_isalpha(str[0])
178 && pathchar_sep(str[2]));
179 #elif defined __linux__
180 return (str[0] == '/');
184 inline bool pathchar_cmp(char a, char b)
187 if (pathchar_isalpha(a) && pathchar_isalpha(b))
189 return (tolower(a) == tolower(b));
191 /* Either path separator is acceptable */
194 return pathchar_sep(b);
201 * @brief Forms a relative path given two absolute paths.
203 * @param buffer Buffer to store relative path in.
204 * @param maxlength Maximum length of the output buffer.
205 * @param relTo Destination folder to use as a working directory.
206 * Final folder name should not be pathchar-terminated.
207 * @param relFrom Source file or folder to use as a target.
208 * @return True on success, false on failure.
210 bool UTIL_Relatize(char buffer[],
215 /* We don't allow relative paths in here, force
216 * the user to resolve these himself!
218 if (!pathstr_isabsolute(relTo)
219 || !pathstr_isabsolute(relFrom))
225 /* Relative paths across drives are not possible */
226 if (!pathchar_cmp(relTo[0], relFrom[0]))
230 /* Get rid of the drive and semicolon part */
232 relFrom = &relFrom[2];
235 /* Eliminate the common root between the paths */
236 const char *rootTo = NULL;
237 const char *rootFrom = NULL;
238 while (*relTo != '\0' && *relFrom != '\0')
240 /* If we get to a new path sequence, start over */
241 if (pathchar_sep(*relTo)
242 && pathchar_sep(*relFrom))
246 /* If the paths don't compare, stop looking for a common root */
247 } else if (!pathchar_cmp(*relTo, *relFrom)) {
254 /* NULLs shouldn't happen! */
261 size_t numLevels = 0;
263 /* The root case is special!
264 * Don't count anything from it.
266 if (*(rootTo + 1) != '\0')
268 /* Search for how many levels we need to go up.
269 * Since the root pointer points to a '/', we increment
270 * the initial pointer by one.
272 while (*rootTo != '\0')
274 if (pathchar_sep(*rootTo))
276 /* Check for an improper trailing slash,
277 * just to be nice even though the user
278 * should NOT have done this!
280 if (*(rootTo + 1) == '\0')
290 /* Now build the new relative path. */
291 size_t len, total = 0;
294 len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
295 if (len >= maxlength - total)
297 /* Not enough space in the buffer */
303 /* Add the absolute path. */
304 len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
305 if (len >= maxlength - total)
313 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
315 size_t len = vsnprintf(buffer, maxlength, fmt, params);
317 if (len >= maxlength)
326 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len)
328 unsigned char *addr1 = (unsigned char *) addr;
329 unsigned char *addr2 = (unsigned char *) sig;
331 for (size_t i = 0; i < len; i++)
335 if (addr1[i] != addr2[i])