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