]> granicus.if.org Git - transmission/commitdiff
(trunk, libT) improved/new MSVC portability wrappers dirname, basename, gettimeofday...
authorJordan Lee <jordan@transmissionbt.com>
Sun, 8 Sep 2013 18:27:27 +0000 (18:27 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sun, 8 Sep 2013 18:27:27 +0000 (18:27 +0000)
libtransmission/log.c
libtransmission/session.c
libtransmission/utils.c
libtransmission/utils.h

index 4e803ec9077998194516a4724d1072f708ae8cf0..83eee1097c57205a2570ee26da7d4b133a11e89b 100644 (file)
@@ -158,7 +158,7 @@ tr_logGetTimeStr (char * buf, int buflen)
   time_t seconds;
   int milliseconds;
 
-  gettimeofday (&tv, NULL);
+  tr_gettimeofday (&tv);
 
   seconds = tv.tv_sec;
   tr_localtime_r (&seconds, &now_tm);
index d079d12cdeb96ce237dfe9f904130670e547b37a..041447064295294c79f593255970e129c93b3c2e 100644 (file)
@@ -669,7 +669,7 @@ onNowTimer (evutil_socket_t foo UNUSED, short bar UNUSED, void * vsession)
   **/
 
   /* schedule the next timer for right after the next second begins */
-  gettimeofday (&tv, NULL);
+  tr_gettimeofday (&tv);
   usec = 1000000 - tv.tv_usec;
   if (usec > max)
     usec = max;
index 80ca6813625ad369e94bda8d55bab4087a5cacfb..e05f917586d5a14d9183a0f7981c255ad90e6ee1 100644 (file)
@@ -49,7 +49,7 @@
  #include <w32api.h>
  #define WINVER WindowsXP /* freeaddrinfo (), getaddrinfo (), getnameinfo () */
  #include <direct.h> /* _getcwd () */
- #include <windows.h> /* Sleep () */
+ #include <windows.h> /* Sleep (), GetSystemTimeAsFileTime () */
 #endif
 
 #include "transmission.h"
@@ -83,6 +83,41 @@ tr_localtime_r (const time_t *_clock, struct tm *_result)
 #endif
 }
 
+int
+tr_gettimeofday (struct timeval * tv)
+{
+#ifdef _MSC_VER
+#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
+
+  FILETIME ft;
+  uint64_t tmp = 0;
+
+  if (tv == NULL)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+
+  GetSystemTimeAsFileTime(&ft);
+  tmp |= ft.dwHighDateTime;
+  tmp <<= 32;
+  tmp |= ft.dwLowDateTime;
+  tmp /= 10; /* to microseconds */
+  tmp -= DELTA_EPOCH_IN_MICROSECS;
+
+  tv->tv_sec = tmp / 1000000UL;
+  tv->tv_usec = tmp % 1000000UL;
+
+  return 0;
+
+#undef DELTA_EPOCH_IN_MICROSECS
+#else
+
+  return gettimeofday (tv, NULL);
+
+#endif
+}
+
 /***
 ****
 ***/
@@ -248,19 +283,69 @@ tr_loadFile (const char * path,
 char*
 tr_basename (const char * path)
 {
+#ifdef _MSC_VER
+
+  char fname[_MAX_FNAME], ext[_MAX_EXT];
+  if (_splitpath_s (path, NULL, 0, NULL, 0, fname, sizeof (fname), ext, sizeof (ext)) == 0)
+    {
+      const size_t tmpLen = strlen(fname) + strlen(ext) + 2;
+      char * const tmp = tr_malloc (tmpLen);
+      if (tmp != NULL)
+        {
+          if (_makepath_s (tmp, tmpLen, NULL, NULL, fname, ext) == 0)
+            return tmp;
+
+          tr_free (tmp);
+        }
+    }
+
+  return tr_strdup (".");
+
+#else
+
   char * tmp = tr_strdup (path);
   char * ret = tr_strdup (basename (tmp));
   tr_free (tmp);
   return ret;
+
+#endif
 }
 
 char*
 tr_dirname (const char * path)
 {
+#ifdef _MSC_VER
+
+  char drive[_MAX_DRIVE], dir[_MAX_DIR];
+  if (_splitpath_s (path, drive, sizeof (drive), dir, sizeof (dir), NULL, 0, NULL, 0) == 0)
+    {
+      const size_t tmpLen = strlen(drive) + strlen(dir) + 2;
+      char * const tmp = tr_malloc (tmpLen);
+      if (tmp != NULL)
+        {
+          if (_makepath_s (tmp, tmpLen, drive, dir, NULL, NULL) == 0)
+            {
+              size_t len = strlen(tmp);
+              while (len > 0 && (tmp[len - 1] == '/' || tmp[len - 1] == '\\'))
+                tmp[--len] = '\0';
+
+              return tmp;
+            }
+
+          tr_free (tmp);
+        }
+    }
+
+  return tr_strdup (".");
+
+#else
+
   char * tmp = tr_strdup (path);
   char * ret = tr_strdup (dirname (tmp));
   tr_free (tmp);
   return ret;
+
+#endif
 }
 
 char*
@@ -647,7 +732,7 @@ tr_time_msec (void)
 {
   struct timeval tv;
 
-  gettimeofday (&tv, NULL);
+  tr_gettimeofday (&tv);
   return (uint64_t) tv.tv_sec * 1000 + (tv.tv_usec / 1000);
 }
 
index 28ad6ba6c3f90f79cd02dd985a64b3f25c3947a7..520915104fbab3264a473cb75a3c68337350e357 100644 (file)
@@ -17,6 +17,7 @@
 #include <stddef.h> /* size_t */
 #include <time.h> /* time_t */
 
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -388,6 +389,11 @@ char* tr_strratio (char * buf, size_t buflen, double ratio, const char * infinit
 /** @brief Portability wrapper for localtime_r () that uses the system implementation if available */
 struct tm * tr_localtime_r (const time_t *_clock, struct tm *_result);
 
+struct timeval;
+
+/** @brief Portability wrapper for gettimeofday (), with tz argument dropped */
+int tr_gettimeofday (struct timeval * tv);
+
 
 /**
  * @brief move a file