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