Wrap around the usages of windows.h
authorAlpar Juttner <alpar@cs.elte.hu>
Mon, 16 Feb 2009 15:03:32 +0000
changeset 482879c55700cd4
parent 481 daddd623ac9a
child 483 3f0ddf255524
child 487 afd134142111
child 515 b9b3473327e3
Wrap around the usages of windows.h
lemon/CMakeLists.txt
lemon/Makefile.am
lemon/bits/windows.cc
lemon/bits/windows.h
lemon/graph_to_eps.h
lemon/random.h
lemon/time_measure.h
     1.1 --- a/lemon/CMakeLists.txt	Tue Feb 10 17:21:26 2009 +0000
     1.2 +++ b/lemon/CMakeLists.txt	Mon Feb 16 15:03:32 2009 +0000
     1.3 @@ -4,7 +4,9 @@
     1.4    arg_parser.cc
     1.5    base.cc
     1.6    color.cc
     1.7 -  random.cc)
     1.8 +  random.cc
     1.9 +  bits/windows.cc
    1.10 +)
    1.11  
    1.12  INSTALL(
    1.13    TARGETS lemon
     2.1 --- a/lemon/Makefile.am	Tue Feb 10 17:21:26 2009 +0000
     2.2 +++ b/lemon/Makefile.am	Mon Feb 16 15:03:32 2009 +0000
     2.3 @@ -10,7 +10,8 @@
     2.4          lemon/arg_parser.cc \
     2.5          lemon/base.cc \
     2.6          lemon/color.cc \
     2.7 -        lemon/random.cc
     2.8 +        lemon/random.cc \
     2.9 +	lemon/bits/windows.cc
    2.10  
    2.11  #lemon_libemon_la_CXXFLAGS = $(GLPK_CFLAGS) $(CPLEX_CFLAGS) $(SOPLEX_CXXFLAGS)
    2.12  #lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS)
    2.13 @@ -40,7 +41,8 @@
    2.14  	lemon/smart_graph.h \
    2.15          lemon/time_measure.h \
    2.16          lemon/tolerance.h \
    2.17 -	lemon/unionfind.h
    2.18 +	lemon/unionfind.h \
    2.19 +	lemon/bits/windows.h
    2.20  
    2.21  bits_HEADERS += \
    2.22  	lemon/bits/alteration_notifier.h \
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/lemon/bits/windows.cc	Mon Feb 16 15:03:32 2009 +0000
     3.3 @@ -0,0 +1,136 @@
     3.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     3.5 + *
     3.6 + * This file is a part of LEMON, a generic C++ optimization library.
     3.7 + *
     3.8 + * Copyright (C) 2003-2009
     3.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    3.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    3.11 + *
    3.12 + * Permission to use, modify and distribute this software is granted
    3.13 + * provided that this copyright notice appears in all copies. For
    3.14 + * precise terms see the accompanying LICENSE file.
    3.15 + *
    3.16 + * This software is provided "AS IS" with no warranty of any kind,
    3.17 + * express or implied, and with no claim as to its suitability for any
    3.18 + * purpose.
    3.19 + *
    3.20 + */
    3.21 +
    3.22 +///\file
    3.23 +///\brief Some basic non-inline functions and static global data.
    3.24 +
    3.25 +#include<lemon/bits/windows.h>
    3.26 +
    3.27 +#ifdef WIN32
    3.28 +#ifndef WIN32_LEAN_AND_MEAN
    3.29 +#define WIN32_LEAN_AND_MEAN
    3.30 +#endif
    3.31 +#ifndef NOMINMAX
    3.32 +#define NOMINMAX
    3.33 +#endif
    3.34 +#include <windows.h>
    3.35 +#else
    3.36 +#include <unistd.h>
    3.37 +#include <ctime>
    3.38 +#include <sys/times.h>
    3.39 +#include <sys/time.h>
    3.40 +#endif
    3.41 +
    3.42 +#include <cmath>
    3.43 +#include <sstream>
    3.44 +
    3.45 +namespace lemon {
    3.46 +  namespace bits {
    3.47 +    void getWinProcTimes(double &rtime,
    3.48 +                         double &utime, double &stime,
    3.49 +                         double &cutime, double &cstime)
    3.50 +    {
    3.51 +#ifdef WIN32
    3.52 +      static const double ch = 4294967296.0e-7;
    3.53 +      static const double cl = 1.0e-7;
    3.54 +
    3.55 +      FILETIME system;
    3.56 +      GetSystemTimeAsFileTime(&system);
    3.57 +      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
    3.58 +
    3.59 +      FILETIME create, exit, kernel, user;
    3.60 +      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
    3.61 +        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
    3.62 +        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
    3.63 +        cutime = 0;
    3.64 +        cstime = 0;
    3.65 +      } else {
    3.66 +        rtime = 0;
    3.67 +        utime = 0;
    3.68 +        stime = 0;
    3.69 +        cutime = 0;
    3.70 +        cstime = 0;
    3.71 +      }
    3.72 +#else
    3.73 +      timeval tv;
    3.74 +      gettimeofday(&tv, 0);
    3.75 +      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    3.76 +
    3.77 +      tms ts;
    3.78 +      double tck=sysconf(_SC_CLK_TCK);
    3.79 +      times(&ts);
    3.80 +      utime=ts.tms_utime/tck;
    3.81 +      stime=ts.tms_stime/tck;
    3.82 +      cutime=ts.tms_cutime/tck;
    3.83 +      cstime=ts.tms_cstime/tck;
    3.84 +#endif
    3.85 +    }
    3.86 +
    3.87 +    std::string getWinFormattedDate()
    3.88 +    {
    3.89 +      std::ostringstream os;
    3.90 +#ifdef WIN32
    3.91 +      SYSTEMTIME time;
    3.92 +      GetSystemTime(&time);
    3.93 +#if defined(_MSC_VER) && (_MSC_VER < 1500)
    3.94 +      LPWSTR buf1, buf2, buf3;
    3.95 +      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    3.96 +                        L"ddd MMM dd", buf1, 11) &&
    3.97 +          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
    3.98 +                        L"HH':'mm':'ss", buf2, 9) &&
    3.99 +          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
   3.100 +                        L"yyyy", buf3, 5)) {
   3.101 +        os << buf1 << ' ' << buf2 << ' ' << buf3;
   3.102 +      }
   3.103 +#else
   3.104 +      char buf1[11], buf2[9], buf3[5];
   3.105 +      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
   3.106 +                        "ddd MMM dd", buf1, 11) &&
   3.107 +          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
   3.108 +                        "HH':'mm':'ss", buf2, 9) &&
   3.109 +          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
   3.110 +                        "yyyy", buf3, 5)) {
   3.111 +        os << buf1 << ' ' << buf2 << ' ' << buf3;
   3.112 +      }
   3.113 +#endif
   3.114 +      else os << "unknown";
   3.115 +#else
   3.116 +      timeval tv;
   3.117 +      gettimeofday(&tv, 0);
   3.118 +
   3.119 +      char cbuf[26];
   3.120 +      ctime_r(&tv.tv_sec,cbuf);
   3.121 +      os << cbuf;
   3.122 +#endif
   3.123 +      return os.str();
   3.124 +    }
   3.125 +
   3.126 +    int getWinRndSeed()
   3.127 +    {
   3.128 +#ifdef WIN32
   3.129 +      FILETIME time;
   3.130 +      GetSystemTimeAsFileTime(&time);
   3.131 +      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
   3.132 +#else
   3.133 +      timeval tv;
   3.134 +      gettimeofday(&tv, 0);
   3.135 +      return getpid() + tv.tv_sec + tv.tv_usec;
   3.136 +#endif
   3.137 +    }
   3.138 +  }
   3.139 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/lemon/bits/windows.h	Mon Feb 16 15:03:32 2009 +0000
     4.3 @@ -0,0 +1,34 @@
     4.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     4.5 + *
     4.6 + * This file is a part of LEMON, a generic C++ optimization library.
     4.7 + *
     4.8 + * Copyright (C) 2003-2009
     4.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    4.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    4.11 + *
    4.12 + * Permission to use, modify and distribute this software is granted
    4.13 + * provided that this copyright notice appears in all copies. For
    4.14 + * precise terms see the accompanying LICENSE file.
    4.15 + *
    4.16 + * This software is provided "AS IS" with no warranty of any kind,
    4.17 + * express or implied, and with no claim as to its suitability for any
    4.18 + * purpose.
    4.19 + *
    4.20 + */
    4.21 +
    4.22 +#ifndef LEMON_WINDOWS_H
    4.23 +#define LEMON_WINDOWS_H
    4.24 +
    4.25 +#include <string>
    4.26 +
    4.27 +namespace lemon {
    4.28 +  namespace bits {
    4.29 +    void getWinProcTimes(double &rtime,
    4.30 +                         double &utime, double &stime,
    4.31 +                         double &cutime, double &cstime);
    4.32 +    std::string getWinFormattedDate();
    4.33 +    int getWinRndSeed();
    4.34 +  }
    4.35 +}
    4.36 +
    4.37 +#endif
     5.1 --- a/lemon/graph_to_eps.h	Tue Feb 10 17:21:26 2009 +0000
     5.2 +++ b/lemon/graph_to_eps.h	Mon Feb 16 15:03:32 2009 +0000
     5.3 @@ -29,13 +29,7 @@
     5.4  #include<sys/time.h>
     5.5  #include<ctime>
     5.6  #else
     5.7 -#ifndef WIN32_LEAN_AND_MEAN
     5.8 -#define WIN32_LEAN_AND_MEAN
     5.9 -#endif
    5.10 -#ifndef NOMINMAX
    5.11 -#define NOMINMAX
    5.12 -#endif
    5.13 -#include<windows.h>
    5.14 +#include<lemon/bits/windows.h>
    5.15  #endif
    5.16  
    5.17  #include<lemon/math.h>
    5.18 @@ -683,41 +677,19 @@
    5.19      os << "%%Creator: LEMON, graphToEps()\n";
    5.20  
    5.21      {
    5.22 +      os << "%%CreationDate: ";
    5.23  #ifndef WIN32
    5.24        timeval tv;
    5.25        gettimeofday(&tv, 0);
    5.26  
    5.27        char cbuf[26];
    5.28        ctime_r(&tv.tv_sec,cbuf);
    5.29 -      os << "%%CreationDate: " << cbuf;
    5.30 +      os << cbuf;
    5.31  #else
    5.32 -      SYSTEMTIME time;
    5.33 -      GetSystemTime(&time);
    5.34 -#if defined(_MSC_VER) && (_MSC_VER < 1500)
    5.35 -      LPWSTR buf1, buf2, buf3;
    5.36 -      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    5.37 -                        L"ddd MMM dd", buf1, 11) &&
    5.38 -          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
    5.39 -                        L"HH':'mm':'ss", buf2, 9) &&
    5.40 -          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    5.41 -                        L"yyyy", buf3, 5)) {
    5.42 -        os << "%%CreationDate: " << buf1 << ' '
    5.43 -           << buf2 << ' ' << buf3 << std::endl;
    5.44 -      }
    5.45 -#else
    5.46 -        char buf1[11], buf2[9], buf3[5];
    5.47 -        if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    5.48 -                          "ddd MMM dd", buf1, 11) &&
    5.49 -            GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
    5.50 -                          "HH':'mm':'ss", buf2, 9) &&
    5.51 -            GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    5.52 -                          "yyyy", buf3, 5)) {
    5.53 -          os << "%%CreationDate: " << buf1 << ' '
    5.54 -             << buf2 << ' ' << buf3 << std::endl;
    5.55 -        }
    5.56 -#endif
    5.57 +      os << bits::getWinFormattedDate();
    5.58  #endif
    5.59      }
    5.60 +    os << std::endl;
    5.61  
    5.62      if (_autoArcWidthScale) {
    5.63        double max_w=0;
     6.1 --- a/lemon/random.h	Tue Feb 10 17:21:26 2009 +0000
     6.2 +++ b/lemon/random.h	Mon Feb 16 15:03:32 2009 +0000
     6.3 @@ -77,7 +77,7 @@
     6.4  #include <sys/types.h>
     6.5  #include <unistd.h>
     6.6  #else
     6.7 -#include <windows.h>
     6.8 +#include <lemon/bits/windows.h>
     6.9  #endif
    6.10  
    6.11  ///\ingroup misc
    6.12 @@ -666,9 +666,7 @@
    6.13        gettimeofday(&tv, 0);
    6.14        seed(getpid() + tv.tv_sec + tv.tv_usec);
    6.15  #else
    6.16 -      FILETIME time;
    6.17 -      GetSystemTimeAsFileTime(&time);
    6.18 -      seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
    6.19 +      seed(bits::getWinRndSeed());
    6.20  #endif
    6.21        return true;
    6.22      }
     7.1 --- a/lemon/time_measure.h	Tue Feb 10 17:21:26 2009 +0000
     7.2 +++ b/lemon/time_measure.h	Mon Feb 16 15:03:32 2009 +0000
     7.3 @@ -24,14 +24,7 @@
     7.4  ///\brief Tools for measuring cpu usage
     7.5  
     7.6  #ifdef WIN32
     7.7 -#ifndef WIN32_LEAN_AND_MEAN
     7.8 -#define WIN32_LEAN_AND_MEAN
     7.9 -#endif
    7.10 -#ifndef NOMINMAX
    7.11 -#define NOMINMAX
    7.12 -#endif
    7.13 -#include <windows.h>
    7.14 -#include <cmath>
    7.15 +#include <lemon/bits/windows.h>
    7.16  #else
    7.17  #include <unistd.h>
    7.18  #include <sys/times.h>
    7.19 @@ -92,26 +85,7 @@
    7.20        cutime=ts.tms_cutime/tck;
    7.21        cstime=ts.tms_cstime/tck;
    7.22  #else
    7.23 -      static const double ch = 4294967296.0e-7;
    7.24 -      static const double cl = 1.0e-7;
    7.25 -
    7.26 -      FILETIME system;
    7.27 -      GetSystemTimeAsFileTime(&system);
    7.28 -      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
    7.29 -
    7.30 -      FILETIME create, exit, kernel, user;
    7.31 -      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
    7.32 -        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
    7.33 -        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
    7.34 -        cutime = 0;
    7.35 -        cstime = 0;
    7.36 -      } else {
    7.37 -        rtime = 0;
    7.38 -        utime = 0;
    7.39 -        stime = 0;
    7.40 -        cutime = 0;
    7.41 -        cstime = 0;
    7.42 -      }
    7.43 +      bits::getWinProcTimes(rtime, utime, stime, cutime, cstime);
    7.44  #endif
    7.45      }
    7.46