lemon/bits/windows.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 20 Feb 2009 18:43:34 +0100
changeset 516 dab9e610e37d
child 483 3f0ddf255524
permissions -rw-r--r--
Fixes in adaptors.h to compile on AIX
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
alpar@482
    31
#include <windows.h>
alpar@482
    32
#else
alpar@482
    33
#include <unistd.h>
alpar@482
    34
#include <ctime>
alpar@482
    35
#include <sys/times.h>
alpar@482
    36
#include <sys/time.h>
alpar@482
    37
#endif
alpar@482
    38
alpar@482
    39
#include <cmath>
alpar@482
    40
#include <sstream>
alpar@482
    41
alpar@482
    42
namespace lemon {
alpar@482
    43
  namespace bits {
alpar@482
    44
    void getWinProcTimes(double &rtime,
alpar@482
    45
                         double &utime, double &stime,
alpar@482
    46
                         double &cutime, double &cstime)
alpar@482
    47
    {
alpar@482
    48
#ifdef WIN32
alpar@482
    49
      static const double ch = 4294967296.0e-7;
alpar@482
    50
      static const double cl = 1.0e-7;
alpar@482
    51
alpar@482
    52
      FILETIME system;
alpar@482
    53
      GetSystemTimeAsFileTime(&system);
alpar@482
    54
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
alpar@482
    55
alpar@482
    56
      FILETIME create, exit, kernel, user;
alpar@482
    57
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
alpar@482
    58
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
alpar@482
    59
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
alpar@482
    60
        cutime = 0;
alpar@482
    61
        cstime = 0;
alpar@482
    62
      } else {
alpar@482
    63
        rtime = 0;
alpar@482
    64
        utime = 0;
alpar@482
    65
        stime = 0;
alpar@482
    66
        cutime = 0;
alpar@482
    67
        cstime = 0;
alpar@482
    68
      }
alpar@482
    69
#else
alpar@482
    70
      timeval tv;
alpar@482
    71
      gettimeofday(&tv, 0);
alpar@482
    72
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@482
    73
alpar@482
    74
      tms ts;
alpar@482
    75
      double tck=sysconf(_SC_CLK_TCK);
alpar@482
    76
      times(&ts);
alpar@482
    77
      utime=ts.tms_utime/tck;
alpar@482
    78
      stime=ts.tms_stime/tck;
alpar@482
    79
      cutime=ts.tms_cutime/tck;
alpar@482
    80
      cstime=ts.tms_cstime/tck;
alpar@482
    81
#endif
alpar@482
    82
    }
alpar@482
    83
alpar@482
    84
    std::string getWinFormattedDate()
alpar@482
    85
    {
alpar@482
    86
      std::ostringstream os;
alpar@482
    87
#ifdef WIN32
alpar@482
    88
      SYSTEMTIME time;
alpar@482
    89
      GetSystemTime(&time);
alpar@482
    90
#if defined(_MSC_VER) && (_MSC_VER < 1500)
alpar@482
    91
      LPWSTR buf1, buf2, buf3;
alpar@482
    92
      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@482
    93
                        L"ddd MMM dd", buf1, 11) &&
alpar@482
    94
          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@482
    95
                        L"HH':'mm':'ss", buf2, 9) &&
alpar@482
    96
          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@482
    97
                        L"yyyy", buf3, 5)) {
alpar@482
    98
        os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@482
    99
      }
alpar@482
   100
#else
alpar@482
   101
      char buf1[11], buf2[9], buf3[5];
alpar@482
   102
      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@482
   103
                        "ddd MMM dd", buf1, 11) &&
alpar@482
   104
          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@482
   105
                        "HH':'mm':'ss", buf2, 9) &&
alpar@482
   106
          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@482
   107
                        "yyyy", buf3, 5)) {
alpar@482
   108
        os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@482
   109
      }
alpar@482
   110
#endif
alpar@482
   111
      else os << "unknown";
alpar@482
   112
#else
alpar@482
   113
      timeval tv;
alpar@482
   114
      gettimeofday(&tv, 0);
alpar@482
   115
alpar@482
   116
      char cbuf[26];
alpar@482
   117
      ctime_r(&tv.tv_sec,cbuf);
alpar@482
   118
      os << cbuf;
alpar@482
   119
#endif
alpar@482
   120
      return os.str();
alpar@482
   121
    }
alpar@482
   122
alpar@482
   123
    int getWinRndSeed()
alpar@482
   124
    {
alpar@482
   125
#ifdef WIN32
alpar@482
   126
      FILETIME time;
alpar@482
   127
      GetSystemTimeAsFileTime(&time);
alpar@482
   128
      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
alpar@482
   129
#else
alpar@482
   130
      timeval tv;
alpar@482
   131
      gettimeofday(&tv, 0);
alpar@482
   132
      return getpid() + tv.tv_sec + tv.tv_usec;
alpar@482
   133
#endif
alpar@482
   134
    }
alpar@482
   135
  }
alpar@482
   136
}