loader/utility.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 23 22:10:16 2008 -0600 (2008-11-23)
changeset 586 ed3320c98dd7
parent 579 be04fd67923b
child 608 30b546cc4e03
permissions -rw-r--r--
Attempt at fixing windows build errors.
     1 #include <stdio.h>
     2 #include <assert.h>
     3 #include <string.h>
     4 #include <ctype.h>
     5 #include <stdarg.h>
     6 #include <stdlib.h>
     7 #include "loader.h"
     8 #include "utility.h"
     9 
    10 #if defined _WIN32
    11 static void
    12 mm_GetPlatformError(char *buffer, size_t maxlength)
    13 {
    14 	DWORD dw = GetLastError();
    15 	FormatMessageA(
    16 		FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
    17 		NULL,
    18 		dw,
    19 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    20 		(LPSTR)buffer,
    21 		maxlength,
    22 		NULL);
    23 }
    24 #endif
    25 
    26 
    27 size_t
    28 mm_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
    29 {
    30 	size_t len = vsnprintf(buffer, maxlength, fmt, params);
    31 
    32 	if (len >= maxlength)
    33 	{
    34 		len = maxlength - 1;
    35 		buffer[len] = '\0';
    36 	}
    37 
    38 	return len;
    39 }
    40 
    41 size_t
    42 mm_Format(char *buffer, size_t maxlength, const char *fmt, ...)
    43 {
    44 	size_t len;
    45 	va_list ap;
    46 
    47 	va_start(ap, fmt);
    48 	len = mm_FormatArgs(buffer, maxlength, fmt, ap);
    49 	va_end(ap);
    50 
    51 	return len;
    52 }
    53 
    54 size_t
    55 mm_PathFormat(char *buffer, size_t maxlen, const char *fmt, ...)
    56 {
    57 	size_t len;
    58 	va_list ap;
    59 
    60 	va_start(ap, fmt);
    61 	len = mm_FormatArgs(buffer, maxlen, fmt, ap);
    62 	va_end(ap);
    63 
    64 	for (size_t i = 0; i < len; i++)
    65 	{
    66 		if (buffer[i] == ALT_SEP_CHAR)
    67 			buffer[i] = PATH_SEP_CHAR;
    68 	}
    69 
    70 	return len;
    71 }
    72 
    73 void
    74 mm_TrimLeft(char *buffer)
    75 {
    76 	/* Let's think of this as our iterator */
    77 	char *i = buffer;
    78 
    79 	/* Make sure the buffer isn't null */
    80 	if (i && *i)
    81 	{
    82 		/* Add up number of whitespace characters */
    83 		while(isspace((unsigned char) *i))
    84 			i++;
    85 
    86 		/* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
    87 		if (i != buffer)
    88 			memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
    89 	}
    90 }
    91 
    92 void
    93 mm_TrimRight(char *buffer)
    94 {
    95 	/* Make sure buffer isn't null */
    96 	if (buffer)
    97 	{
    98 		size_t len = strlen(buffer);
    99 
   100 		/* Loop through buffer backwards while replacing whitespace chars with null chars */
   101 		for (size_t i = len - 1; i >= 0; i--)
   102 		{
   103 			if (isspace((unsigned char) buffer[i]))
   104 				buffer[i] = '\0';
   105 			else
   106 				break;
   107 		}
   108 	}
   109 }
   110 
   111 /* :TODO: this should skip string literals */
   112 void
   113 mm_TrimComments(char *buffer)
   114 {
   115 	int num_sc = 0;
   116 	size_t len = strlen(buffer);
   117 	if (buffer)
   118 	{
   119 		for (int i = len - 1; i >= 0; i--)
   120 		{
   121 			if (buffer[i] == '/')
   122 			{
   123 				if (++num_sc >= 2 && i==0)
   124 				{
   125 					buffer[i] = '\0';
   126 					return;
   127 				}
   128 			}
   129 			else
   130 			{
   131 				if (num_sc >= 2)
   132 				{
   133 					buffer[i] = '\0';
   134 					return;
   135 				}
   136 				num_sc = 0;
   137 			}
   138 			/* size_t won't go below 0, manually break out */
   139 			if (i == 0)
   140 				break;
   141 			
   142 		}
   143 	}
   144 }
   145 
   146 void
   147 mm_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   148 {
   149 	size_t start;
   150 	size_t len = strlen(str);
   151 
   152 	for (start = 0; start < len; start++)
   153 	{
   154 		if (!isspace(str[start]))
   155 			break;
   156 	}
   157 
   158 	size_t end;
   159 	for (end = start; end < len; end++)
   160 	{
   161 		if (isspace(str[end]))
   162 			break;
   163 	}
   164 	
   165 	size_t i, c = 0;
   166 	for (i = start; i < end; i++, c++)
   167 	{
   168 		if (c >= len1)
   169 			break;
   170 		buf1[c] = str[i];
   171 	}
   172 	buf1[c] = '\0';
   173 
   174 	for (start = end; start < len; start++)
   175 	{
   176 		if (!isspace(str[start]))
   177 			break;
   178 	}
   179 
   180 	for (c = 0; start < len; start++, c++)
   181 	{
   182 		if (c >= len2)
   183 			break;
   184 		buf2[c] = str[start];
   185 	}
   186 	buf2[c] = '\0';
   187 }
   188 
   189 bool
   190 mm_PathCmp(const char *path1, const char *path2)
   191 {
   192 	size_t pos1 = 0, pos2 = 0;
   193 
   194 	while (true)
   195 	{
   196 		if (path1[pos1] == '\0' || path2[pos2] == '\0')
   197 			return (path1[pos1] == path2[pos2]);
   198 
   199 		if (path1[pos1] == PATH_SEP_CHAR)
   200 		{
   201 			if (path2[pos2] != PATH_SEP_CHAR)
   202 				return false;
   203 
   204 			/* Look for extra path chars */
   205 			while (path1[++pos1])
   206 			{
   207 				if (path1[pos1] != PATH_SEP_CHAR)
   208 					break;
   209 			}
   210 			while (path2[++pos2])
   211 			{
   212 				if (path2[pos2] != PATH_SEP_CHAR)
   213 					break;
   214 			}
   215 			continue;
   216 		}
   217 
   218 		/* If we're at a different non-alphanumeric, the next character MUST match */
   219 		if ((((unsigned)path1[pos1] & 0x80) && path1[pos1] != path2[pos2])
   220 			||
   221 			!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2])
   222 			)
   223 		{
   224 			return false;
   225 		}
   226 
   227 	#ifdef WIN32
   228 		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   229 	#else
   230 		if (path1[pos1] != path2[pos2])
   231 	#endif
   232 		{
   233 			return false;
   234 		}
   235 
   236 		pos1++;
   237 		pos2++;
   238 	}
   239 }
   240 
   241 bool
   242 mm_ResolvePath(const char *path, char *buffer, size_t maxlength)
   243 {
   244 #if defined _WIN32
   245 	return _fullpath(buffer, path, maxlength) != NULL;
   246 #elif defined __linux__
   247 	assert(maxlength >= PATH_MAX);
   248 	return realpath(path, buffer) != NULL;
   249 #endif
   250 }
   251 
   252 void *
   253 mm_LoadLibrary(const char *path, char *buffer, size_t maxlength)
   254 {
   255 	void *lib;
   256 
   257 #if defined _WIN32
   258 	lib = (void*)LoadLibrary(path);
   259 
   260 	if (lib == NULL)
   261 	{
   262 		mm_GetPlatformError(buffer, maxlength);
   263 		return NULL;
   264 	}
   265 #elif defined __linux__
   266 	lib = dlopen(path, RTLD_NOW);
   267 
   268 	if (lib == NULL)
   269 	{
   270 		mm_Format(buffer, maxlength, "%s", dlerror());
   271 		return NULL;
   272 	}
   273 #endif
   274 
   275 	return lib;
   276 }
   277 
   278 void *
   279 mm_GetLibAddress(void *lib, const char *name)
   280 {
   281 #if defined _WIN32
   282 	return GetProcAddress((HMODULE)lib, name);
   283 #elif defined __linux__
   284 	return dlsym(lib, name);
   285 #endif
   286 }
   287 
   288 void
   289 mm_UnloadLibrary(void *lib)
   290 {
   291 #if defined _WIN32
   292 	FreeLibrary((HMODULE)lib);
   293 #else
   294 	dlclose(lib);
   295 #endif
   296 }
   297 
   298 bool
   299 mm_GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
   300 {
   301 #if defined _WIN32
   302 	MEMORY_BASIC_INFORMATION mem;
   303 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
   304 		return false;
   305 	if (mem.AllocationBase == NULL)
   306 		return false;
   307 	HMODULE dll = (HMODULE)mem.AllocationBase;
   308 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
   309 #elif defined __linux__
   310 	Dl_info info;
   311 	if (!dladdr(pAddr, &info))
   312 		return false;
   313 	if (!info.dli_fbase || !info.dli_fname)
   314 		return false;
   315 	const char *dllpath = info.dli_fname;
   316 	snprintf(buffer, maxlength, "%s", dllpath);
   317 #endif
   318 	return true;
   319 }
   320