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