add .gitignore
[swift-upb.git] / compat.cpp
index 208828d..7a5a654 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  compat.cpp
- *  p2tp
+ *  swift
  *
  *  Created by Arno Bakker, Victor Grishchenko
  *  Copyright 2009 Delft University of Technology. All rights reserved.
 #include <Tchar.h>
 #include <io.h>
 #include <sys/timeb.h>
-#include "compat/hirestimeofday.h"
+#include <vector>
+#include <stdexcept>
 #else
 #include <unistd.h>
 #include <sys/time.h>
 #endif
 
-namespace p2tp {
+namespace swift {
 
 #ifdef _WIN32
 static HANDLE map_handles[1024];
@@ -37,7 +38,7 @@ int     file_seek (int fd, size_t offset) {
 #ifndef _WIN32
     return lseek(fd,offset,SEEK_SET);
 #else
-       return _lseek(fd,offset,SEEK_SET);
+    return _lseek(fd,offset,SEEK_SET);
 #endif
 }
 
@@ -54,7 +55,7 @@ void print_error(const char* msg) {
 #ifdef _WIN32
     int e = WSAGetLastError();
     if (e)
-        fprintf(stderr,"network error #%i\n",e);
+        fprintf(stderr,"network error #%u\n",e);
 #endif
 }
 
@@ -76,7 +77,7 @@ void*   memory_map (int fd, size_t size) {
                                        0,
                                        0,
                                        NULL    );
-       if (maphandle == NULL)
+    if (maphandle == NULL)
         return NULL;
     map_handles[fd] = maphandle;
 
@@ -95,8 +96,8 @@ void    memory_unmap (int fd, void* mapping, size_t size) {
     munmap(mapping,size);
     close(fd);
 #else
-       UnmapViewOfFile(mapping);
-       CloseHandle(map_handles[fd]);
+    UnmapViewOfFile(mapping);
+    CloseHandle(map_handles[fd]);
 #endif
 }
 
@@ -121,10 +122,28 @@ int inet_aton(const char *cp, struct in_addr *inp)
     return 1;
 }
 
+#endif
+
+#ifdef _WIN32
+
+LARGE_INTEGER get_freq() {
+    LARGE_INTEGER proc_freq;
+    if (!::QueryPerformanceFrequency(&proc_freq))
+       print_error("HiResTimeOfDay: QueryPerformanceFrequency() failed");
+    return proc_freq;
+}
+
 tint usec_time(void)
 {
-       HiResTimeOfDay* tod = HiResTimeOfDay::Instance();
-       return tod->getTimeUSec();
+       static LARGE_INTEGER last_time;
+       LARGE_INTEGER cur_time;
+       QueryPerformanceCounter(&cur_time);
+       if (cur_time.QuadPart<last_time.QuadPart)
+               print_error("QueryPerformanceCounter wrapped"); // does this happen?
+       last_time = cur_time;
+       static float freq = 1000000.0/get_freq().QuadPart;
+       tint usec = cur_time.QuadPart * freq;
+       return usec;
 }
 
 
@@ -133,14 +152,64 @@ tint usec_time(void)
 tint usec_time(void)
 {
     struct timeval t;
-       gettimeofday(&t,NULL);
-       tint ret;
-       ret = t.tv_sec;
-       ret *= 1000000;
-       ret += t.tv_usec;
-       return ret;
+    gettimeofday(&t,NULL);
+    tint ret;
+    ret = t.tv_sec;
+    ret *= 1000000;
+    ret += t.tv_usec;
+    return ret;
 }
 
 #endif
 
+void LibraryInit(void)
+{
+#ifdef _WIN32
+       static WSADATA _WSAData;
+       // win32 requires you to initialize the Winsock DLL with the desired
+       // specification version
+       WORD wVersionRequested;
+    wVersionRequested = MAKEWORD(2, 2);
+       WSAStartup(wVersionRequested, &_WSAData);
+#endif
+}
+
+
+std::string gettmpdir(void)
+{
+#ifdef _WIN32
+  DWORD result = ::GetTempPath(0, _T(""));
+  if (result == 0)
+       throw std::runtime_error("Could not get system temp path");
+
+  std::vector<TCHAR> tempPath(result + 1);
+  result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
+  if((result == 0) || (result >= tempPath.size()))
+       throw std::runtime_error("Could not get system temp path");
+
+  return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
+#else
+         return std::string("/tmp/");
+#endif
+}
+
+bool    make_socket_nonblocking(SOCKET fd) {
+#ifdef _WIN32
+    u_long enable = 1;
+    return 0==ioctlsocket(fd, FIONBIO, &enable);
+#else
+    int enable=1;
+    return 0==fcntl(fd, F_SETFL, O_NONBLOCK);
+#endif
+}
+
+bool    close_socket (SOCKET sock) {
+#ifdef _WIN32
+    return 0==closesocket(sock);
+#else
+    return 0==::close(sock);
+#endif
+}
+
+
 }