lemon/bits/windows.cc
changeset 492 b9b3473327e3
child 493 3f0ddf255524
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/windows.cc	Mon Feb 16 18:15:52 2009 +0000
     1.3 @@ -0,0 +1,136 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2009
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\file
    1.23 +///\brief Some basic non-inline functions and static global data.
    1.24 +
    1.25 +#include<lemon/bits/windows.h>
    1.26 +
    1.27 +#ifdef WIN32
    1.28 +#ifndef WIN32_LEAN_AND_MEAN
    1.29 +#define WIN32_LEAN_AND_MEAN
    1.30 +#endif
    1.31 +#ifndef NOMINMAX
    1.32 +#define NOMINMAX
    1.33 +#endif
    1.34 +#include <windows.h>
    1.35 +#else
    1.36 +#include <unistd.h>
    1.37 +#include <ctime>
    1.38 +#include <sys/times.h>
    1.39 +#include <sys/time.h>
    1.40 +#endif
    1.41 +
    1.42 +#include <cmath>
    1.43 +#include <sstream>
    1.44 +
    1.45 +namespace lemon {
    1.46 +  namespace bits {
    1.47 +    void getWinProcTimes(double &rtime,
    1.48 +                         double &utime, double &stime,
    1.49 +                         double &cutime, double &cstime)
    1.50 +    {
    1.51 +#ifdef WIN32
    1.52 +      static const double ch = 4294967296.0e-7;
    1.53 +      static const double cl = 1.0e-7;
    1.54 +
    1.55 +      FILETIME system;
    1.56 +      GetSystemTimeAsFileTime(&system);
    1.57 +      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
    1.58 +
    1.59 +      FILETIME create, exit, kernel, user;
    1.60 +      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
    1.61 +        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
    1.62 +        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
    1.63 +        cutime = 0;
    1.64 +        cstime = 0;
    1.65 +      } else {
    1.66 +        rtime = 0;
    1.67 +        utime = 0;
    1.68 +        stime = 0;
    1.69 +        cutime = 0;
    1.70 +        cstime = 0;
    1.71 +      }
    1.72 +#else
    1.73 +      timeval tv;
    1.74 +      gettimeofday(&tv, 0);
    1.75 +      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    1.76 +
    1.77 +      tms ts;
    1.78 +      double tck=sysconf(_SC_CLK_TCK);
    1.79 +      times(&ts);
    1.80 +      utime=ts.tms_utime/tck;
    1.81 +      stime=ts.tms_stime/tck;
    1.82 +      cutime=ts.tms_cutime/tck;
    1.83 +      cstime=ts.tms_cstime/tck;
    1.84 +#endif
    1.85 +    }
    1.86 +
    1.87 +    std::string getWinFormattedDate()
    1.88 +    {
    1.89 +      std::ostringstream os;
    1.90 +#ifdef WIN32
    1.91 +      SYSTEMTIME time;
    1.92 +      GetSystemTime(&time);
    1.93 +#if defined(_MSC_VER) && (_MSC_VER < 1500)
    1.94 +      LPWSTR buf1, buf2, buf3;
    1.95 +      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    1.96 +                        L"ddd MMM dd", buf1, 11) &&
    1.97 +          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
    1.98 +                        L"HH':'mm':'ss", buf2, 9) &&
    1.99 +          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
   1.100 +                        L"yyyy", buf3, 5)) {
   1.101 +        os << buf1 << ' ' << buf2 << ' ' << buf3;
   1.102 +      }
   1.103 +#else
   1.104 +      char buf1[11], buf2[9], buf3[5];
   1.105 +      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
   1.106 +                        "ddd MMM dd", buf1, 11) &&
   1.107 +          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
   1.108 +                        "HH':'mm':'ss", buf2, 9) &&
   1.109 +          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
   1.110 +                        "yyyy", buf3, 5)) {
   1.111 +        os << buf1 << ' ' << buf2 << ' ' << buf3;
   1.112 +      }
   1.113 +#endif
   1.114 +      else os << "unknown";
   1.115 +#else
   1.116 +      timeval tv;
   1.117 +      gettimeofday(&tv, 0);
   1.118 +
   1.119 +      char cbuf[26];
   1.120 +      ctime_r(&tv.tv_sec,cbuf);
   1.121 +      os << cbuf;
   1.122 +#endif
   1.123 +      return os.str();
   1.124 +    }
   1.125 +
   1.126 +    int getWinRndSeed()
   1.127 +    {
   1.128 +#ifdef WIN32
   1.129 +      FILETIME time;
   1.130 +      GetSystemTimeAsFileTime(&time);
   1.131 +      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
   1.132 +#else
   1.133 +      timeval tv;
   1.134 +      gettimeofday(&tv, 0);
   1.135 +      return getpid() + tv.tv_sec + tv.tv_usec;
   1.136 +#endif
   1.137 +    }
   1.138 +  }
   1.139 +}