lemon/bits/windows.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 09 Feb 2010 23:29:51 +0100
changeset 823 a7e93de12cbd
parent 482 879c55700cd4
child 877 141f9c0db4a3
child 913 64260c0f58eb
permissions -rw-r--r--
Add a warning about huge capacities in Preflow (#319)
alpar@482
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@482
     2
 *
alpar@482
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@482
     4
 *
alpar@482
     5
 * Copyright (C) 2003-2009
alpar@482
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@482
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@482
     8
 *
alpar@482
     9
 * Permission to use, modify and distribute this software is granted
alpar@482
    10
 * provided that this copyright notice appears in all copies. For
alpar@482
    11
 * precise terms see the accompanying LICENSE file.
alpar@482
    12
 *
alpar@482
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@482
    14
 * express or implied, and with no claim as to its suitability for any
alpar@482
    15
 * purpose.
alpar@482
    16
 *
alpar@482
    17
 */
alpar@482
    18
alpar@482
    19
///\file
alpar@482
    20
///\brief Some basic non-inline functions and static global data.
alpar@482
    21
alpar@482
    22
#include<lemon/bits/windows.h>
alpar@482
    23
alpar@482
    24
#ifdef WIN32
alpar@482
    25
#ifndef WIN32_LEAN_AND_MEAN
alpar@482
    26
#define WIN32_LEAN_AND_MEAN
alpar@482
    27
#endif
alpar@482
    28
#ifndef NOMINMAX
alpar@482
    29
#define NOMINMAX
alpar@482
    30
#endif
tapolcai@483
    31
#ifdef UNICODE
tapolcai@483
    32
#undef UNICODE
tapolcai@483
    33
#endif
alpar@482
    34
#include <windows.h>
tapolcai@483
    35
#ifdef LOCALE_INVARIANT
tapolcai@483
    36
#define MY_LOCALE LOCALE_INVARIANT
tapolcai@483
    37
#else
tapolcai@483
    38
#define MY_LOCALE LOCALE_NEUTRAL
tapolcai@483
    39
#endif
alpar@482
    40
#else
alpar@482
    41
#include <unistd.h>
alpar@482
    42
#include <ctime>
alpar@482
    43
#include <sys/times.h>
alpar@482
    44
#include <sys/time.h>
alpar@482
    45
#endif
alpar@482
    46
alpar@482
    47
#include <cmath>
alpar@482
    48
#include <sstream>
alpar@482
    49
alpar@482
    50
namespace lemon {
alpar@482
    51
  namespace bits {
alpar@482
    52
    void getWinProcTimes(double &rtime,
alpar@482
    53
                         double &utime, double &stime,
alpar@482
    54
                         double &cutime, double &cstime)
alpar@482
    55
    {
alpar@482
    56
#ifdef WIN32
alpar@482
    57
      static const double ch = 4294967296.0e-7;
alpar@482
    58
      static const double cl = 1.0e-7;
alpar@482
    59
alpar@482
    60
      FILETIME system;
alpar@482
    61
      GetSystemTimeAsFileTime(&system);
alpar@482
    62
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
alpar@482
    63
alpar@482
    64
      FILETIME create, exit, kernel, user;
alpar@482
    65
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
alpar@482
    66
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
alpar@482
    67
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
alpar@482
    68
        cutime = 0;
alpar@482
    69
        cstime = 0;
alpar@482
    70
      } else {
alpar@482
    71
        rtime = 0;
alpar@482
    72
        utime = 0;
alpar@482
    73
        stime = 0;
alpar@482
    74
        cutime = 0;
alpar@482
    75
        cstime = 0;
alpar@482
    76
      }
alpar@482
    77
#else
alpar@482
    78
      timeval tv;
alpar@482
    79
      gettimeofday(&tv, 0);
alpar@482
    80
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@482
    81
alpar@482
    82
      tms ts;
alpar@482
    83
      double tck=sysconf(_SC_CLK_TCK);
alpar@482
    84
      times(&ts);
alpar@482
    85
      utime=ts.tms_utime/tck;
alpar@482
    86
      stime=ts.tms_stime/tck;
alpar@482
    87
      cutime=ts.tms_cutime/tck;
alpar@482
    88
      cstime=ts.tms_cstime/tck;
alpar@482
    89
#endif
alpar@482
    90
    }
alpar@482
    91
alpar@482
    92
    std::string getWinFormattedDate()
alpar@482
    93
    {
alpar@482
    94
      std::ostringstream os;
alpar@482
    95
#ifdef WIN32
alpar@482
    96
      SYSTEMTIME time;
alpar@482
    97
      GetSystemTime(&time);
tapolcai@483
    98
      char buf1[11], buf2[9], buf3[5];
tapolcai@483
    99
	  if (GetDateFormat(MY_LOCALE, 0, &time,
tapolcai@483
   100
                        ("ddd MMM dd"), buf1, 11) &&
tapolcai@483
   101
          GetTimeFormat(MY_LOCALE, 0, &time,
tapolcai@483
   102
                        ("HH':'mm':'ss"), buf2, 9) &&
tapolcai@483
   103
          GetDateFormat(MY_LOCALE, 0, &time,
tapolcai@483
   104
                        ("yyyy"), buf3, 5)) {
alpar@482
   105
        os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@482
   106
      }
alpar@482
   107
      else os << "unknown";
alpar@482
   108
#else
alpar@482
   109
      timeval tv;
alpar@482
   110
      gettimeofday(&tv, 0);
alpar@482
   111
alpar@482
   112
      char cbuf[26];
alpar@482
   113
      ctime_r(&tv.tv_sec,cbuf);
alpar@482
   114
      os << cbuf;
alpar@482
   115
#endif
alpar@482
   116
      return os.str();
alpar@482
   117
    }
alpar@482
   118
alpar@482
   119
    int getWinRndSeed()
alpar@482
   120
    {
alpar@482
   121
#ifdef WIN32
alpar@482
   122
      FILETIME time;
alpar@482
   123
      GetSystemTimeAsFileTime(&time);
alpar@482
   124
      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
alpar@482
   125
#else
alpar@482
   126
      timeval tv;
alpar@482
   127
      gettimeofday(&tv, 0);
alpar@482
   128
      return getpid() + tv.tv_sec + tv.tv_usec;
alpar@482
   129
#endif
alpar@482
   130
    }
alpar@482
   131
  }
alpar@482
   132
}