COIN-OR::LEMON - Graph Library

Ticket #215: ab2f0a7add1e_tj.patch

File ab2f0a7add1e_tj.patch, 7.6 KB (added by Tapolcai János, 16 years ago)
  • lemon/bits/windows.cc

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