Trigger build for hl2sdk-l4d changes.
3 * ======================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
39 mm_GetPlatformError(char *buffer, size_t maxlength)
41 DWORD dw = GetLastError();
43 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
46 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
55 mm_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
57 size_t len = vsnprintf(buffer, maxlength, fmt, params);
69 mm_Format(char *buffer, size_t maxlength, const char *fmt, ...)
75 len = mm_FormatArgs(buffer, maxlength, fmt, ap);
82 mm_PathFormat(char *buffer, size_t maxlen, const char *fmt, ...)
88 len = mm_FormatArgs(buffer, maxlen, fmt, ap);
91 for (size_t i = 0; i < len; i++)
93 if (buffer[i] == ALT_SEP_CHAR)
94 buffer[i] = PATH_SEP_CHAR;
101 mm_TrimLeft(char *buffer)
103 /* Let's think of this as our iterator */
106 /* Make sure the buffer isn't null */
109 /* Add up number of whitespace characters */
110 while(isspace((unsigned char) *i))
113 /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
115 memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
120 mm_TrimRight(char *buffer)
122 /* Make sure buffer isn't null */
125 size_t len = strlen(buffer);
127 /* Loop through buffer backwards while replacing whitespace chars with null chars */
128 for (size_t i = len - 1; i < len; i--)
130 if (isspace((unsigned char) buffer[i]))
138 /* :TODO: this should skip string literals */
140 mm_TrimComments(char *buffer)
143 size_t len = strlen(buffer);
146 for (int i = len - 1; i >= 0; i--)
148 if (buffer[i] == '/')
150 if (++num_sc >= 2 && i==0)
165 /* size_t won't go below 0, manually break out */
174 mm_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
177 size_t len = strlen(str);
179 for (start = 0; start < len; start++)
181 if (!isspace(str[start]))
186 for (end = start; end < len; end++)
188 if (isspace(str[end]))
193 for (i = start; i < end; i++, c++)
201 for (start = end; start < len; start++)
203 if (!isspace(str[start]))
207 for (c = 0; start < len; start++, c++)
211 buf2[c] = str[start];
217 mm_PathCmp(const char *path1, const char *path2)
219 size_t pos1 = 0, pos2 = 0;
223 if (path1[pos1] == '\0' || path2[pos2] == '\0')
224 return (path1[pos1] == path2[pos2]);
226 if (path1[pos1] == PATH_SEP_CHAR)
228 if (path2[pos2] != PATH_SEP_CHAR)
231 /* Look for extra path chars */
232 while (path1[++pos1])
234 if (path1[pos1] != PATH_SEP_CHAR)
237 while (path2[++pos2])
239 if (path2[pos2] != PATH_SEP_CHAR)
245 /* If we're at a different non-alphanumeric, the next character MUST match */
246 if ((((unsigned)path1[pos1] & 0x80) && path1[pos1] != path2[pos2])
248 (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
255 if (toupper(path1[pos1]) != toupper(path2[pos2]))
257 if (path1[pos1] != path2[pos2])
269 mm_ResolvePath(const char *path, char *buffer, size_t maxlength)
272 return _fullpath(buffer, path, maxlength) != NULL;
273 #elif defined __linux__ || defined __APPLE__
274 assert(maxlength >= PATH_MAX);
275 return realpath(path, buffer) != NULL;
280 mm_LoadLibrary(const char *path, char *buffer, size_t maxlength)
285 lib = (void*)LoadLibrary(path);
289 mm_GetPlatformError(buffer, maxlength);
292 #elif defined __linux__ || defined __APPLE__
293 lib = dlopen(path, RTLD_NOW);
297 mm_Format(buffer, maxlength, "%s", dlerror());
306 mm_GetLibAddress(void *lib, const char *name)
309 return GetProcAddress((HMODULE)lib, name);
310 #elif defined __linux__ || defined __APPLE__
311 return dlsym(lib, name);
316 mm_UnloadLibrary(void *lib)
319 FreeLibrary((HMODULE)lib);
320 #elif defined __linux__ || defined __APPLE__
326 mm_GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
329 MEMORY_BASIC_INFORMATION mem;
330 if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
332 if (mem.AllocationBase == NULL)
334 HMODULE dll = (HMODULE)mem.AllocationBase;
335 GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
336 #elif defined __linux__ || defined __APPLE__
338 if (!dladdr(pAddr, &info))
340 if (!info.dli_fbase || !info.dli_fname)
342 const char *dllpath = info.dli_fname;
343 snprintf(buffer, maxlength, "%s", dllpath);