lemon/bits/windows.cc
changeset 783 ef88c0a30f85
parent 491 879c55700cd4
child 877 141f9c0db4a3
child 940 64260c0f58eb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/windows.cc	Thu Nov 05 15:48:01 2009 +0100
     1.3 @@ -0,0 +1,132 @@
     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 +#ifdef UNICODE
    1.35 +#undef UNICODE
    1.36 +#endif
    1.37 +#include <windows.h>
    1.38 +#ifdef LOCALE_INVARIANT
    1.39 +#define MY_LOCALE LOCALE_INVARIANT
    1.40 +#else
    1.41 +#define MY_LOCALE LOCALE_NEUTRAL
    1.42 +#endif
    1.43 +#else
    1.44 +#include <unistd.h>
    1.45 +#include <ctime>
    1.46 +#include <sys/times.h>
    1.47 +#include <sys/time.h>
    1.48 +#endif
    1.49 +
    1.50 +#include <cmath>
    1.51 +#include <sstream>
    1.52 +
    1.53 +namespace lemon {
    1.54 +  namespace bits {
    1.55 +    void getWinProcTimes(double &rtime,
    1.56 +                         double &utime, double &stime,
    1.57 +                         double &cutime, double &cstime)
    1.58 +    {
    1.59 +#ifdef WIN32
    1.60 +      static const double ch = 4294967296.0e-7;
    1.61 +      static const double cl = 1.0e-7;
    1.62 +
    1.63 +      FILETIME system;
    1.64 +      GetSystemTimeAsFileTime(&system);
    1.65 +      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
    1.66 +
    1.67 +      FILETIME create, exit, kernel, user;
    1.68 +      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
    1.69 +        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
    1.70 +        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
    1.71 +        cutime = 0;
    1.72 +        cstime = 0;
    1.73 +      } else {
    1.74 +        rtime = 0;
    1.75 +        utime = 0;
    1.76 +        stime = 0;
    1.77 +        cutime = 0;
    1.78 +        cstime = 0;
    1.79 +      }
    1.80 +#else
    1.81 +      timeval tv;
    1.82 +      gettimeofday(&tv, 0);
    1.83 +      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    1.84 +
    1.85 +      tms ts;
    1.86 +      double tck=sysconf(_SC_CLK_TCK);
    1.87 +      times(&ts);
    1.88 +      utime=ts.tms_utime/tck;
    1.89 +      stime=ts.tms_stime/tck;
    1.90 +      cutime=ts.tms_cutime/tck;
    1.91 +      cstime=ts.tms_cstime/tck;
    1.92 +#endif
    1.93 +    }
    1.94 +
    1.95 +    std::string getWinFormattedDate()
    1.96 +    {
    1.97 +      std::ostringstream os;
    1.98 +#ifdef WIN32
    1.99 +      SYSTEMTIME time;
   1.100 +      GetSystemTime(&time);
   1.101 +      char buf1[11], buf2[9], buf3[5];
   1.102 +	  if (GetDateFormat(MY_LOCALE, 0, &time,
   1.103 +                        ("ddd MMM dd"), buf1, 11) &&
   1.104 +          GetTimeFormat(MY_LOCALE, 0, &time,
   1.105 +                        ("HH':'mm':'ss"), buf2, 9) &&
   1.106 +          GetDateFormat(MY_LOCALE, 0, &time,
   1.107 +                        ("yyyy"), buf3, 5)) {
   1.108 +        os << buf1 << ' ' << buf2 << ' ' << buf3;
   1.109 +      }
   1.110 +      else os << "unknown";
   1.111 +#else
   1.112 +      timeval tv;
   1.113 +      gettimeofday(&tv, 0);
   1.114 +
   1.115 +      char cbuf[26];
   1.116 +      ctime_r(&tv.tv_sec,cbuf);
   1.117 +      os << cbuf;
   1.118 +#endif
   1.119 +      return os.str();
   1.120 +    }
   1.121 +
   1.122 +    int getWinRndSeed()
   1.123 +    {
   1.124 +#ifdef WIN32
   1.125 +      FILETIME time;
   1.126 +      GetSystemTimeAsFileTime(&time);
   1.127 +      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
   1.128 +#else
   1.129 +      timeval tv;
   1.130 +      gettimeofday(&tv, 0);
   1.131 +      return getpid() + tv.tv_sec + tv.tv_usec;
   1.132 +#endif
   1.133 +    }
   1.134 +  }
   1.135 +}