**************************************************************************/
#include <windows.h>
+
#include <assert.h>
-#include <signal.h>
#include <string.h>
#include <stdio.h>
#include "os.hpp"
+#include "os_path.hpp"
-namespace OS {
+namespace os {
/*
* Trick from http://locklessinc.com/articles/pthreads_on_windows/
*/
static CRITICAL_SECTION
-CriticalSection = {
+criticalSection = {
(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
};
void
-AcquireMutex(void)
+acquireMutex(void)
{
- EnterCriticalSection(&CriticalSection);
+ EnterCriticalSection(&criticalSection);
}
void
-ReleaseMutex(void)
+releaseMutex(void)
{
- LeaveCriticalSection(&CriticalSection);
+ LeaveCriticalSection(&criticalSection);
}
-bool
-GetProcessName(char *str, size_t size)
+Path
+getProcessName(void)
{
- char szProcessPath[PATH_MAX];
- char *lpProcessName;
- char *lpProcessExt;
-
- GetModuleFileNameA(NULL, szProcessPath, sizeof(szProcessPath)/sizeof(szProcessPath[0]));
+ Path path;
+ size_t size = MAX_PATH;
+ char *buf = path.buf(size);
- lpProcessName = strrchr(szProcessPath, '\\');
- lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
+ DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
+ (void)nWritten;
- lpProcessExt = strrchr(lpProcessName, '.');
- if (lpProcessExt) {
- *lpProcessExt = '\0';
- }
+ path.truncate();
- strncpy(str, lpProcessName, size);
-
- return true;
+ return path;
}
-bool
-GetCurrentDir(char *str, size_t size)
+Path
+getCurrentDir(void)
{
- DWORD ret;
- ret = GetCurrentDirectoryA(size, str);
- str[size - 1] = 0;
- return ret == 0 ? false : true;
+ Path path;
+ size_t size = MAX_PATH;
+ char *buf = path.buf(size);
+
+ DWORD ret = GetCurrentDirectoryA(size, buf);
+ (void)ret;
+
+ buf[size - 1] = 0;
+ path.truncate();
+
+ return path;
}
void
-DebugMessage(const char *format, ...)
+log(const char *format, ...)
{
char buf[4096];
#endif
}
-long long GetTime(void)
+long long
+getTime(void)
{
static LARGE_INTEGER frequency;
LARGE_INTEGER counter;
}
void
-Abort(void)
+abort(void)
{
#ifndef NDEBUG
DebugBreak();
static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
static void (*gCallback)(void) = NULL;
-static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
+static LONG WINAPI
+unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
if (gCallback) {
gCallback();
}
void
-SetExceptionCallback(void (*callback)(void))
+setExceptionCallback(void (*callback)(void))
{
assert(!gCallback);
gCallback = callback;
assert(!prevExceptionFilter);
- prevExceptionFilter = SetUnhandledExceptionFilter(UnhandledExceptionFilter);
+
+ /*
+ * TODO: Unfortunately it seems that the CRT will reset the exception
+ * handler in certain circumnstances. See
+ * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
+ */
+ prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
}
}
void
-ResetExceptionCallback(void)
+resetExceptionCallback(void)
{
gCallback = NULL;
}
-} /* namespace OS */
+} /* namespace os */