1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/util.cpp Tue Sep 18 20:03:47 2007 +0000
1.3 @@ -0,0 +1,408 @@
1.4 +/* ======== SourceMM ========
1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 + * No warranties of any kind
1.7 + *
1.8 + * License: zlib/libpng
1.9 + *
1.10 + * Author(s): David "BAILOPAN" Anderson
1.11 + * ============================
1.12 + */
1.13 +
1.14 +#include <ctype.h>
1.15 +#include <string.h>
1.16 +#include <stdio.h>
1.17 +#include "util.h"
1.18 +#include "oslink.h"
1.19 +
1.20 +/**
1.21 + * @brief Utility functions
1.22 + * @file util.cpp
1.23 + */
1.24 +
1.25 +const char *UTIL_GetExtension(const char *file)
1.26 +{
1.27 + int len = strlen(file);
1.28 + int i = 0;
1.29 +
1.30 + for (i = len - 1; i >= 0; i--)
1.31 + {
1.32 + if (file[i] == '/' || file[i] == '\\')
1.33 + {
1.34 + return NULL;
1.35 + }
1.36 +
1.37 + if ((file[i] == '.') && (i != len - 1))
1.38 + {
1.39 + return (const char *)&(file[i + 1]);
1.40 + }
1.41 + }
1.42 +
1.43 + return NULL;
1.44 +}
1.45 +
1.46 +void UTIL_TrimLeft(char *buffer)
1.47 +{
1.48 + /* Let's think of this as our iterator */
1.49 + char *i = buffer;
1.50 +
1.51 + /* Make sure the buffer isn't null */
1.52 + if (i && *i)
1.53 + {
1.54 + /* Add up number of whitespace characters */
1.55 + while(isspace((unsigned char) *i))
1.56 + {
1.57 + i++;
1.58 + }
1.59 +
1.60 + /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
1.61 + if (i != buffer)
1.62 + {
1.63 + memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
1.64 + }
1.65 + }
1.66 +}
1.67 +
1.68 +void UTIL_TrimRight(char *buffer)
1.69 +{
1.70 + /* Make sure buffer isn't null */
1.71 + if (buffer)
1.72 + {
1.73 + size_t len = strlen(buffer);
1.74 +
1.75 + /* Loop through buffer backwards while replacing whitespace chars with null chars */
1.76 + for (size_t i = len - 1; i >= 0; i--)
1.77 + {
1.78 + if (isspace((unsigned char) buffer[i]))
1.79 + {
1.80 + buffer[i] = '\0';
1.81 + } else {
1.82 + break;
1.83 + }
1.84 + }
1.85 + }
1.86 +}
1.87 +
1.88 +/* :TODO: this should skip string literals */
1.89 +void UTIL_TrimComments(char *buffer)
1.90 +{
1.91 + int num_sc = 0;
1.92 + size_t len = strlen(buffer);
1.93 + if (buffer)
1.94 + {
1.95 + for (int i = len - 1; i >= 0; i--)
1.96 + {
1.97 + if (buffer[i] == '/')
1.98 + {
1.99 + if (++num_sc >= 2 && i==0)
1.100 + {
1.101 + buffer[i] = '\0';
1.102 + return;
1.103 + }
1.104 + } else {
1.105 + if (num_sc >= 2)
1.106 + {
1.107 + buffer[i] = '\0';
1.108 + return;
1.109 + }
1.110 + num_sc = 0;
1.111 + }
1.112 + /* size_t won't go below 0, manually break out */
1.113 + if (i == 0)
1.114 + {
1.115 + break;
1.116 + }
1.117 + }
1.118 + }
1.119 +}
1.120 +
1.121 +void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
1.122 +{
1.123 + size_t start;
1.124 + size_t len = strlen(str);
1.125 +
1.126 + for (start = 0; start < len; start++)
1.127 + {
1.128 + if (!isspace(str[start]))
1.129 + {
1.130 + break;
1.131 + }
1.132 + }
1.133 +
1.134 + size_t end;
1.135 + for (end = start; end < len; end++)
1.136 + {
1.137 + if (isspace(str[end]))
1.138 + {
1.139 + break;
1.140 + }
1.141 + }
1.142 +
1.143 + size_t i, c = 0;
1.144 + for (i = start; i < end; i++, c++)
1.145 + {
1.146 + if (c >= len1)
1.147 + {
1.148 + break;
1.149 + }
1.150 + buf1[c] = str[i];
1.151 + }
1.152 + buf1[c] = '\0';
1.153 +
1.154 + for (start = end; start < len; start++)
1.155 + {
1.156 + if (!isspace(str[start]))
1.157 + {
1.158 + break;
1.159 + }
1.160 + }
1.161 +
1.162 + for (c = 0; start < len; start++, c++)
1.163 + {
1.164 + if (c >= len2)
1.165 + {
1.166 + break;
1.167 + }
1.168 + buf2[c] = str[start];
1.169 + }
1.170 + buf2[c] = '\0';
1.171 +}
1.172 +
1.173 +bool UTIL_PathCmp(const char *path1, const char *path2)
1.174 +{
1.175 + size_t pos1 = 0, pos2 = 0;
1.176 +
1.177 + while (true)
1.178 + {
1.179 + if (path1[pos1] == '\0' || path2[pos2] == '\0')
1.180 + {
1.181 + return (path1[pos1] == path2[pos2]);
1.182 + }
1.183 +
1.184 + if (path1[pos1] == PATH_SEP_CHAR)
1.185 + {
1.186 + if (path2[pos2] != PATH_SEP_CHAR)
1.187 + {
1.188 + return false;
1.189 + }
1.190 +
1.191 + /* Look for extra path chars */
1.192 + while (path1[++pos1])
1.193 + {
1.194 + if (path1[pos1] != PATH_SEP_CHAR)
1.195 + {
1.196 + break;
1.197 + }
1.198 + }
1.199 + while (path2[++pos2])
1.200 + {
1.201 + if (path2[pos2] != PATH_SEP_CHAR)
1.202 + {
1.203 + break;
1.204 + }
1.205 + }
1.206 + continue;
1.207 + }
1.208 +
1.209 + /* If we're at a different non-alphanumeric, the next character MUST match */
1.210 + if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
1.211 + {
1.212 + return false;
1.213 + }
1.214 +
1.215 + #ifdef WIN32
1.216 + if (toupper(path1[pos1]) != toupper(path2[pos2]))
1.217 + #else
1.218 + if (path1[pos1] != path2[pos2])
1.219 + #endif
1.220 + {
1.221 + return false;
1.222 + }
1.223 +
1.224 + pos1++;
1.225 + pos2++;
1.226 + }
1.227 +}
1.228 +
1.229 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.230 +{
1.231 + va_list ap;
1.232 +
1.233 + va_start(ap, fmt);
1.234 + size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.235 + va_end(ap);
1.236 +
1.237 + if (len >= maxlength)
1.238 + {
1.239 + len = maxlength - 1;
1.240 + buffer[len] = '\0';
1.241 + }
1.242 +
1.243 + return len;
1.244 +}
1.245 +
1.246 +inline bool pathchar_isalpha(char a)
1.247 +{
1.248 + return (((a & 1<<7) == 0) && isalpha(a));
1.249 +}
1.250 +
1.251 +inline bool pathchar_sep(char a)
1.252 +{
1.253 +#if defined WIN32
1.254 + return (a == '/' || a == '\\');
1.255 +#elif defined __linux__
1.256 + return (a == '/');
1.257 +#endif
1.258 +}
1.259 +
1.260 +inline bool pathstr_isabsolute(const char *str)
1.261 +{
1.262 +#if defined WIN32
1.263 + return (pathchar_isalpha(str[0])
1.264 + && str[1] == ':'
1.265 + && pathchar_sep(str[2]));
1.266 +#elif defined __linux__
1.267 + return (str[0] == '/');
1.268 +#endif
1.269 +}
1.270 +
1.271 +inline bool pathchar_cmp(char a, char b)
1.272 +{
1.273 +#if defined WIN32
1.274 + if (pathchar_isalpha(a) && pathchar_isalpha(b))
1.275 + {
1.276 + return (tolower(a) == tolower(b));
1.277 + }
1.278 + /* Either path separator is acceptable */
1.279 + if (pathchar_sep(a))
1.280 + {
1.281 + return pathchar_sep(b);
1.282 + }
1.283 +#endif
1.284 + return (a == b);
1.285 +}
1.286 +
1.287 +/**
1.288 + * @brief Forms a relative path given two absolute paths.
1.289 + *
1.290 + * @param buffer Buffer to store relative path in.
1.291 + * @param maxlength Maximum length of the output buffer.
1.292 + * @param relTo Destination folder to use as a working directory.
1.293 + * Final folder name should not be pathchar-terminated.
1.294 + * @param relFrom Source file or folder to use as a target.
1.295 + * @return True on success, false on failure.
1.296 + */
1.297 +bool UTIL_Relatize(char buffer[],
1.298 + size_t maxlength,
1.299 + const char *relTo,
1.300 + const char *relFrom)
1.301 +{
1.302 + /* We don't allow relative paths in here, force
1.303 + * the user to resolve these himself!
1.304 + */
1.305 + if (!pathstr_isabsolute(relTo)
1.306 + || !pathstr_isabsolute(relFrom))
1.307 + {
1.308 + return false;
1.309 + }
1.310 +
1.311 +#if defined WIN32
1.312 + /* Relative paths across drives are not possible */
1.313 + if (!pathchar_cmp(relTo[0], relFrom[0]))
1.314 + {
1.315 + return false;
1.316 + }
1.317 + /* Get rid of the drive and semicolon part */
1.318 + relTo = &relTo[2];
1.319 + relFrom = &relFrom[2];
1.320 +#endif
1.321 +
1.322 + /* Eliminate the common root between the paths */
1.323 + const char *rootTo = NULL;
1.324 + const char *rootFrom = NULL;
1.325 + while (*relTo != '\0' && *relFrom != '\0')
1.326 + {
1.327 + /* If we get to a new path sequence, start over */
1.328 + if (pathchar_sep(*relTo)
1.329 + && pathchar_sep(*relFrom))
1.330 + {
1.331 + rootTo = relTo;
1.332 + rootFrom = relFrom;
1.333 + /* If the paths don't compare, stop looking for a common root */
1.334 + } else if (!pathchar_cmp(*relTo, *relFrom)) {
1.335 + break;
1.336 + }
1.337 + relTo++;
1.338 + relFrom++;
1.339 + }
1.340 +
1.341 + /* NULLs shouldn't happen! */
1.342 + if (rootTo == NULL
1.343 + || rootFrom == NULL)
1.344 + {
1.345 + return false;
1.346 + }
1.347 +
1.348 + size_t numLevels = 0;
1.349 +
1.350 + /* The root case is special!
1.351 + * Don't count anything from it.
1.352 + */
1.353 + if (*(rootTo + 1) != '\0')
1.354 + {
1.355 + /* Search for how many levels we need to go up.
1.356 + * Since the root pointer points to a '/', we increment
1.357 + * the initial pointer by one.
1.358 + */
1.359 + while (*rootTo != '\0')
1.360 + {
1.361 + if (pathchar_sep(*rootTo))
1.362 + {
1.363 + /* Check for an improper trailing slash,
1.364 + * just to be nice even though the user
1.365 + * should NOT have done this!
1.366 + */
1.367 + if (*(rootTo + 1) == '\0')
1.368 + {
1.369 + break;
1.370 + }
1.371 + numLevels++;
1.372 + }
1.373 + rootTo++;
1.374 + }
1.375 + }
1.376 +
1.377 + /* Now build the new relative path. */
1.378 + size_t len, total = 0;
1.379 + while (numLevels--)
1.380 + {
1.381 + len = _snprintf(&buffer[total], maxlength - total, ".." PATH_SEP_STR);
1.382 + if (len >= maxlength - total)
1.383 + {
1.384 + /* Not enough space in the buffer */
1.385 + return false;
1.386 + }
1.387 + total += len;
1.388 + }
1.389 +
1.390 + /* Add the absolute path. */
1.391 + len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
1.392 + if (len >= maxlength - total)
1.393 + {
1.394 + return false;
1.395 + }
1.396 +
1.397 + return true;
1.398 +}
1.399 +
1.400 +size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
1.401 +{
1.402 + size_t len = vsnprintf(buffer, maxlength, fmt, params);
1.403 +
1.404 + if (len >= maxlength)
1.405 + {
1.406 + len = maxlength - 1;
1.407 + buffer[len] = '\0';
1.408 + }
1.409 +
1.410 + return len;
1.411 +}