Changeset 14193


Ignore:
Timestamp:
Sep 8, 2013, 6:27:27 PM (8 years ago)
Author:
jordan
Message:

(trunk, libT) improved/new MSVC portability wrappers dirname, basename, gettimeofday(). patch by mikedld, breakage by charles

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/log.c

    r13966 r14193  
    159159  int milliseconds;
    160160
    161   gettimeofday (&tv, NULL);
     161  tr_gettimeofday (&tv);
    162162
    163163  seconds = tv.tv_sec;
  • trunk/libtransmission/session.c

    r14190 r14193  
    670670
    671671  /* schedule the next timer for right after the next second begins */
    672   gettimeofday (&tv, NULL);
     672  tr_gettimeofday (&tv);
    673673  usec = 1000000 - tv.tv_usec;
    674674  if (usec > max)
  • trunk/libtransmission/utils.c

    r14116 r14193  
    5050 #define WINVER WindowsXP /* freeaddrinfo (), getaddrinfo (), getnameinfo () */
    5151 #include <direct.h> /* _getcwd () */
    52  #include <windows.h> /* Sleep () */
     52 #include <windows.h> /* Sleep (), GetSystemTimeAsFileTime () */
    5353#endif
    5454
     
    8484}
    8585
     86int
     87tr_gettimeofday (struct timeval * tv)
     88{
     89#ifdef _MSC_VER
     90#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
     91
     92  FILETIME ft;
     93  uint64_t tmp = 0;
     94
     95  if (tv == NULL)
     96    {
     97      errno = EINVAL;
     98      return -1;
     99    }
     100
     101  GetSystemTimeAsFileTime(&ft);
     102  tmp |= ft.dwHighDateTime;
     103  tmp <<= 32;
     104  tmp |= ft.dwLowDateTime;
     105  tmp /= 10; /* to microseconds */
     106  tmp -= DELTA_EPOCH_IN_MICROSECS;
     107
     108  tv->tv_sec = tmp / 1000000UL;
     109  tv->tv_usec = tmp % 1000000UL;
     110
     111  return 0;
     112
     113#undef DELTA_EPOCH_IN_MICROSECS
     114#else
     115
     116  return gettimeofday (tv, NULL);
     117
     118#endif
     119}
     120
    86121/***
    87122****
     
    249284tr_basename (const char * path)
    250285{
     286#ifdef _MSC_VER
     287
     288  char fname[_MAX_FNAME], ext[_MAX_EXT];
     289  if (_splitpath_s (path, NULL, 0, NULL, 0, fname, sizeof (fname), ext, sizeof (ext)) == 0)
     290    {
     291      const size_t tmpLen = strlen(fname) + strlen(ext) + 2;
     292      char * const tmp = tr_malloc (tmpLen);
     293      if (tmp != NULL)
     294        {
     295          if (_makepath_s (tmp, tmpLen, NULL, NULL, fname, ext) == 0)
     296            return tmp;
     297
     298          tr_free (tmp);
     299        }
     300    }
     301
     302  return tr_strdup (".");
     303
     304#else
     305
    251306  char * tmp = tr_strdup (path);
    252307  char * ret = tr_strdup (basename (tmp));
    253308  tr_free (tmp);
    254309  return ret;
     310
     311#endif
    255312}
    256313
     
    258315tr_dirname (const char * path)
    259316{
     317#ifdef _MSC_VER
     318
     319  char drive[_MAX_DRIVE], dir[_MAX_DIR];
     320  if (_splitpath_s (path, drive, sizeof (drive), dir, sizeof (dir), NULL, 0, NULL, 0) == 0)
     321    {
     322      const size_t tmpLen = strlen(drive) + strlen(dir) + 2;
     323      char * const tmp = tr_malloc (tmpLen);
     324      if (tmp != NULL)
     325        {
     326          if (_makepath_s (tmp, tmpLen, drive, dir, NULL, NULL) == 0)
     327            {
     328              size_t len = strlen(tmp);
     329              while (len > 0 && (tmp[len - 1] == '/' || tmp[len - 1] == '\\'))
     330                tmp[--len] = '\0';
     331
     332              return tmp;
     333            }
     334
     335          tr_free (tmp);
     336        }
     337    }
     338
     339  return tr_strdup (".");
     340
     341#else
     342
    260343  char * tmp = tr_strdup (path);
    261344  char * ret = tr_strdup (dirname (tmp));
    262345  tr_free (tmp);
    263346  return ret;
     347
     348#endif
    264349}
    265350
     
    648733  struct timeval tv;
    649734
    650   gettimeofday (&tv, NULL);
     735  tr_gettimeofday (&tv);
    651736  return (uint64_t) tv.tv_sec * 1000 + (tv.tv_usec / 1000);
    652737}
  • trunk/libtransmission/utils.h

    r13991 r14193  
    1717#include <stddef.h> /* size_t */
    1818#include <time.h> /* time_t */
     19
    1920
    2021#ifdef __cplusplus
     
    388389/** @brief Portability wrapper for localtime_r () that uses the system implementation if available */
    389390struct tm * tr_localtime_r (const time_t *_clock, struct tm *_result);
     391
     392struct timeval;
     393
     394/** @brief Portability wrapper for gettimeofday (), with tz argument dropped */
     395int tr_gettimeofday (struct timeval * tv);
    390396
    391397
Note: See TracChangeset for help on using the changeset viewer.