]> git.cworth.org Git - apitrace/blobdiff - os_win32.cpp
Merge branch 'master' into compression
[apitrace] / os_win32.cpp
index 2e28ec8695967b15e9f636f0dfc0eaaef8c7b0f5..4bfdf5e00d96373c1a7e003f42c2dfd521ef935c 100644 (file)
@@ -24,7 +24,9 @@
  **************************************************************************/
 
 #include <windows.h>
+#include <signal.h>
 #include <string.h>
+#include <stdio.h>
 
 #include "os.hpp"
 
@@ -37,7 +39,7 @@ namespace OS {
  */
 static CRITICAL_SECTION
 CriticalSection = {
-    (_CRITICAL_SECTION_DEBUG *)-1, -1, 0, 0, 0, 0
+    (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
 };
 
 
@@ -69,7 +71,7 @@ GetProcessName(char *str, size_t size)
 
     lpProcessExt = strrchr(lpProcessName, '.');
     if (lpProcessExt) {
-       *lpProcessExt = '\0';
+        *lpProcessExt = '\0';
     }
 
     strncpy(str, lpProcessName, size);
@@ -77,5 +79,106 @@ GetProcessName(char *str, size_t size)
     return true;
 }
 
+bool
+GetCurrentDir(char *str, size_t size)
+{
+    DWORD ret;
+    ret = GetCurrentDirectoryA(size, str);
+    str[size - 1] = 0;
+    return ret == 0 ? false : true;
+}
+
+void
+DebugMessage(const char *format, ...)
+{
+    char buf[4096];
+
+    va_list ap;
+    va_start(ap, format);
+    fflush(stdout);
+    vsnprintf(buf, sizeof buf, format, ap);
+    va_end(ap);
+
+    OutputDebugStringA(buf);
+
+    /*
+     * Also write the message to stderr, when a debugger is not present (to
+     * avoid duplicate messages in command line debuggers).
+     */
+#if _WIN32_WINNT > 0x0400
+    if (!IsDebuggerPresent()) {
+        fflush(stdout);
+        fputs(buf, stderr);
+        fflush(stderr);
+    }
+#endif
+}
+
+long long GetTime(void)
+{
+    static LARGE_INTEGER frequency;
+    LARGE_INTEGER counter;
+    if (!frequency.QuadPart)
+        QueryPerformanceFrequency(&frequency);
+    QueryPerformanceCounter(&counter);
+    return counter.QuadPart*1000000LL/frequency.QuadPart;
+}
+
+void
+Abort(void)
+{
+#ifndef NDEBUG
+    DebugBreak();
+#else
+    ExitProcess(0);
+#endif
+}
+
+
+struct Interrupts
+{
+    Interrupts()
+        : set(false),
+          prevfilter(NULL),
+          handler(NULL)
+    {}
+
+    bool set;
+    LPTOP_LEVEL_EXCEPTION_FILTER prevFilter;
+
+    void (*handler)(int);
+};
+static Interrupts interrupts;
+
+LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo)
+{
+    if (interrupts.handler) {
+        int exceptionCode = 0;
+        if (exceptionInfo) {
+            exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode;
+        }
+
+        interrupts.handler(exceptionCode);
+    }
+
+    if (interrupts.prevFilter) {
+        return interrupts.prevFilter(exceptionInfo);
+    } else {
+        return EXCEPTION_CONTINUE_SEARCH;
+    }
+}
+
+void
+CatchInterrupts(void (*func)(int))
+{
+    interrupts.handler = func;
+
+    if (!interrupts.set) {
+        interrupts.prevFilter =
+            SetUnhandledExceptionFilter(InterruptHandler);
+        interrupts.set = true;
+    }
+}
+
 
 } /* namespace OS */