1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/loader/utility.cpp Sun Nov 23 21:21:49 2008 -0600
1.3 @@ -0,0 +1,303 @@
1.4 +#include <stdio.h>
1.5 +#include <assert.h>
1.6 +#include <string.h>
1.7 +#include <ctype.h>
1.8 +#include <stdarg.h>
1.9 +#include <stdlib.h>
1.10 +#include "loader.h"
1.11 +#include "utility.h"
1.12 +
1.13 +size_t
1.14 +mm_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
1.15 +{
1.16 + size_t len = vsnprintf(buffer, maxlength, fmt, params);
1.17 +
1.18 + if (len >= maxlength)
1.19 + {
1.20 + len = maxlength - 1;
1.21 + buffer[len] = '\0';
1.22 + }
1.23 +
1.24 + return len;
1.25 +}
1.26 +
1.27 +size_t
1.28 +mm_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.29 +{
1.30 + size_t len;
1.31 + va_list ap;
1.32 +
1.33 + va_start(ap, fmt);
1.34 + len = mm_FormatArgs(buffer, maxlength, fmt, ap);
1.35 + va_end(ap);
1.36 +
1.37 + return len;
1.38 +}
1.39 +
1.40 +size_t
1.41 +mm_PathFormat(char *buffer, size_t maxlen, const char *fmt, ...)
1.42 +{
1.43 + size_t len;
1.44 + va_list ap;
1.45 +
1.46 + va_start(ap, fmt);
1.47 + len = mm_FormatArgs(buffer, maxlen, fmt, ap);
1.48 + va_end(ap);
1.49 +
1.50 + for (size_t i = 0; i < len; i++)
1.51 + {
1.52 + if (buffer[i] == ALT_SEP_CHAR)
1.53 + buffer[i] = PATH_SEP_CHAR;
1.54 + }
1.55 +
1.56 + return len;
1.57 +}
1.58 +
1.59 +void
1.60 +mm_TrimLeft(char *buffer)
1.61 +{
1.62 + /* Let's think of this as our iterator */
1.63 + char *i = buffer;
1.64 +
1.65 + /* Make sure the buffer isn't null */
1.66 + if (i && *i)
1.67 + {
1.68 + /* Add up number of whitespace characters */
1.69 + while(isspace((unsigned char) *i))
1.70 + i++;
1.71 +
1.72 + /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
1.73 + if (i != buffer)
1.74 + memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
1.75 + }
1.76 +}
1.77 +
1.78 +void
1.79 +mm_TrimRight(char *buffer)
1.80 +{
1.81 + /* Make sure buffer isn't null */
1.82 + if (buffer)
1.83 + {
1.84 + size_t len = strlen(buffer);
1.85 +
1.86 + /* Loop through buffer backwards while replacing whitespace chars with null chars */
1.87 + for (size_t i = len - 1; i >= 0; i--)
1.88 + {
1.89 + if (isspace((unsigned char) buffer[i]))
1.90 + buffer[i] = '\0';
1.91 + else
1.92 + break;
1.93 + }
1.94 + }
1.95 +}
1.96 +
1.97 +/* :TODO: this should skip string literals */
1.98 +void
1.99 +mm_TrimComments(char *buffer)
1.100 +{
1.101 + int num_sc = 0;
1.102 + size_t len = strlen(buffer);
1.103 + if (buffer)
1.104 + {
1.105 + for (int i = len - 1; i >= 0; i--)
1.106 + {
1.107 + if (buffer[i] == '/')
1.108 + {
1.109 + if (++num_sc >= 2 && i==0)
1.110 + {
1.111 + buffer[i] = '\0';
1.112 + return;
1.113 + }
1.114 + }
1.115 + else
1.116 + {
1.117 + if (num_sc >= 2)
1.118 + {
1.119 + buffer[i] = '\0';
1.120 + return;
1.121 + }
1.122 + num_sc = 0;
1.123 + }
1.124 + /* size_t won't go below 0, manually break out */
1.125 + if (i == 0)
1.126 + break;
1.127 +
1.128 + }
1.129 + }
1.130 +}
1.131 +
1.132 +void
1.133 +mm_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
1.134 +{
1.135 + size_t start;
1.136 + size_t len = strlen(str);
1.137 +
1.138 + for (start = 0; start < len; start++)
1.139 + {
1.140 + if (!isspace(str[start]))
1.141 + break;
1.142 + }
1.143 +
1.144 + size_t end;
1.145 + for (end = start; end < len; end++)
1.146 + {
1.147 + if (isspace(str[end]))
1.148 + break;
1.149 + }
1.150 +
1.151 + size_t i, c = 0;
1.152 + for (i = start; i < end; i++, c++)
1.153 + {
1.154 + if (c >= len1)
1.155 + break;
1.156 + buf1[c] = str[i];
1.157 + }
1.158 + buf1[c] = '\0';
1.159 +
1.160 + for (start = end; start < len; start++)
1.161 + {
1.162 + if (!isspace(str[start]))
1.163 + break;
1.164 + }
1.165 +
1.166 + for (c = 0; start < len; start++, c++)
1.167 + {
1.168 + if (c >= len2)
1.169 + break;
1.170 + buf2[c] = str[start];
1.171 + }
1.172 + buf2[c] = '\0';
1.173 +}
1.174 +
1.175 +bool
1.176 +mm_PathCmp(const char *path1, const char *path2)
1.177 +{
1.178 + size_t pos1 = 0, pos2 = 0;
1.179 +
1.180 + while (true)
1.181 + {
1.182 + if (path1[pos1] == '\0' || path2[pos2] == '\0')
1.183 + return (path1[pos1] == path2[pos2]);
1.184 +
1.185 + if (path1[pos1] == PATH_SEP_CHAR)
1.186 + {
1.187 + if (path2[pos2] != PATH_SEP_CHAR)
1.188 + return false;
1.189 +
1.190 + /* Look for extra path chars */
1.191 + while (path1[++pos1])
1.192 + {
1.193 + if (path1[pos1] != PATH_SEP_CHAR)
1.194 + break;
1.195 + }
1.196 + while (path2[++pos2])
1.197 + {
1.198 + if (path2[pos2] != PATH_SEP_CHAR)
1.199 + break;
1.200 + }
1.201 + continue;
1.202 + }
1.203 +
1.204 + /* If we're at a different non-alphanumeric, the next character MUST match */
1.205 + if ((((unsigned)path1[pos1] & 0x80) && path1[pos1] != path2[pos2])
1.206 + ||
1.207 + !isalpha(path1[pos1]) && (path1[pos1] != path2[pos2])
1.208 + )
1.209 + {
1.210 + return false;
1.211 + }
1.212 +
1.213 + #ifdef WIN32
1.214 + if (toupper(path1[pos1]) != toupper(path2[pos2]))
1.215 + #else
1.216 + if (path1[pos1] != path2[pos2])
1.217 + #endif
1.218 + {
1.219 + return false;
1.220 + }
1.221 +
1.222 + pos1++;
1.223 + pos2++;
1.224 + }
1.225 +}
1.226 +
1.227 +bool
1.228 +mm_ResolvePath(const char *path, char *buffer, size_t maxlength)
1.229 +{
1.230 +#if defined _WIN32
1.231 + return _fullpath(buffer, path, maxlength) != NULL;
1.232 +#elif defined __linux__
1.233 + assert(maxlength >= PATH_MAX);
1.234 + return realpath(path, buffer) != NULL;
1.235 +#endif
1.236 +}
1.237 +
1.238 +void *
1.239 +mm_LoadLibrary(const char *path, char *buffer, size_t maxlength)
1.240 +{
1.241 + void *lib;
1.242 +
1.243 +#if defined _WIN32
1.244 + lib = LoadLibrary(path);
1.245 +
1.246 + if (lib == NULL)
1.247 + {
1.248 + mm_GetPlatformError(buffer, maxlength);
1.249 + return NULL;
1.250 + }
1.251 +#elif defined __linux__
1.252 + lib = dlopen(path, RTLD_NOW);
1.253 +
1.254 + if (lib == NULL)
1.255 + {
1.256 + mm_Format(buffer, maxlength, "%s", dlerror());
1.257 + return NULL;
1.258 + }
1.259 +#endif
1.260 +
1.261 + return lib;
1.262 +}
1.263 +
1.264 +void *
1.265 +mm_GetLibAddress(void *lib, const char *name)
1.266 +{
1.267 +#if defined _WIN32
1.268 + return GetProcAddress(lib, name);
1.269 +#elif defined __linux__
1.270 + return dlsym(lib, name);
1.271 +#endif
1.272 +}
1.273 +
1.274 +void
1.275 +mm_UnloadLibrary(void *lib)
1.276 +{
1.277 +#if defined _WIN32
1.278 + FreeLibrary(lib);
1.279 +#else
1.280 + dlclose(lib);
1.281 +#endif
1.282 +}
1.283 +
1.284 +bool
1.285 +mm_GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
1.286 +{
1.287 +#if defined _WIN32
1.288 + MEMORY_BASIC_INFORMATION mem;
1.289 + if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
1.290 + return false;
1.291 + if (mem.AllocationBase == NULL)
1.292 + return false;
1.293 + HMODULE dll = (HMODULE)mem.AllocationBase;
1.294 + GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
1.295 +#elif defined __linux__
1.296 + Dl_info info;
1.297 + if (!dladdr(pAddr, &info))
1.298 + return false;
1.299 + if (!info.dli_fbase || !info.dli_fname)
1.300 + return false;
1.301 + const char *dllpath = info.dli_fname;
1.302 + snprintf(buffer, maxlength, "%s", dllpath);
1.303 +#endif
1.304 + return true;
1.305 +}
1.306 +