]> git.cworth.org Git - apitrace/blobdiff - common/os_win32.cpp
Move os::Path to a separate header.
[apitrace] / common / os_win32.cpp
index 587503c41553fa7cd013cf5ad28862fa34f938a7..f33e175aac4916cc4baa296c219ab2b622c55a0a 100644 (file)
  **************************************************************************/
 
 #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];
 
@@ -115,7 +116,8 @@ DebugMessage(const char *format, ...)
 #endif
 }
 
-long long GetTime(void)
+long long
+getTime(void)
 {
     static LARGE_INTEGER frequency;
     LARGE_INTEGER counter;
@@ -126,7 +128,7 @@ long long GetTime(void)
 }
 
 void
-Abort(void)
+abort(void)
 {
 #ifndef NDEBUG
     DebugBreak();
@@ -139,7 +141,8 @@ Abort(void)
 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();
@@ -153,7 +156,7 @@ static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
 }
 
 void
-SetExceptionCallback(void (*callback)(void))
+setExceptionCallback(void (*callback)(void))
 {
     assert(!gCallback);
 
@@ -161,15 +164,21 @@ SetExceptionCallback(void (*callback)(void))
         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 */