From 456189fd16169c5824467c963b15b323f259b159 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 15 Apr 2018 12:31:41 +0200 Subject: [PATCH] Refactor porting --- Minetestmapper/CMakeLists.txt | 2 ++ Minetestmapper/porting.cpp | 50 ++++++++++++++++++++++++++++++++++ Minetestmapper/porting.h | 33 ++++++++++++++++------ Minetestmapper/porting_posix.h | 30 -------------------- Minetestmapper/porting_win32.h | 10 ------- 5 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 Minetestmapper/porting.cpp delete mode 100644 Minetestmapper/porting_posix.h delete mode 100644 Minetestmapper/porting_win32.h diff --git a/Minetestmapper/CMakeLists.txt b/Minetestmapper/CMakeLists.txt index 98df531..2244f08 100644 --- a/Minetestmapper/CMakeLists.txt +++ b/Minetestmapper/CMakeLists.txt @@ -29,6 +29,8 @@ set(sources CharEncodingConverter.h PaintEngine_libgd.cpp PaintEngine_libgd.h + porting.cpp + porting.h db.h db-leveldb.cpp db-leveldb.h diff --git a/Minetestmapper/porting.cpp b/Minetestmapper/porting.cpp new file mode 100644 index 0000000..5563c69 --- /dev/null +++ b/Minetestmapper/porting.cpp @@ -0,0 +1,50 @@ +#include "porting.h" + +#include // fopen +#include // getenv + + + +inline void sleepMs(int time) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(time)); +} + +FILE *porting::fopen(const char *filename, const char *mode) +{ + FILE *file = nullptr; +#ifdef _WIN32 + fopen_s(&file, filename, mode); +#else + file = fopen(minetestConf, mode) +#endif // _WIN32 + + return file; +} + +std::string porting::getenv(const char *name) +{ + std::string env; + char *buf = nullptr; + +#ifdef _WIN32 + std::size_t len; + _dupenv_s(&buf, &len, name); +#else + env = getenv(name); +#endif // _WIN32 + return buf == nullptr ? std::string() : std::string(buf); +} + +std::string porting::strerror(int errnum) +{ +#ifdef _WIN32 + const std::size_t len = 100; + char errmsg[len]; + strerror_s(errmsg, len, errnum); +#else + char *errmsg = strerror(errnum); +#endif // _WIN32 + + return std::string(errmsg); +} diff --git a/Minetestmapper/porting.h b/Minetestmapper/porting.h index a468def..e346b18 100644 --- a/Minetestmapper/porting.h +++ b/Minetestmapper/porting.h @@ -1,11 +1,9 @@ -#ifndef _PORTING_H -#define _PORTING_H +#pragma once -#ifdef _WIN32 -#include "porting_win32.h" -#else -#include "porting_posix.h" -#endif +#include +#include +#include +#include #ifdef _MSC_VER #ifndef strcasecmp @@ -13,5 +11,24 @@ #endif #endif -#endif // _PORTING_H +inline void sleepMs(int time); + +namespace porting { + + /* + Wrapper for fopen_s on Windows. This is not only to keep the deprecation notice quit, + it is also much faster the the deprecated fopen (~50 ms!) + On other systems it does still use the original fopen + */ + FILE* fopen(const char *filename, const char *mode); + + /* + Wrapper for getenv_s on Windows. + Uses getenv on other OS. + */ + std::string getenv(const char *name); + + std::string strerror(int errnum); + +} // namespace porting diff --git a/Minetestmapper/porting_posix.h b/Minetestmapper/porting_posix.h deleted file mode 100644 index 6365526..0000000 --- a/Minetestmapper/porting_posix.h +++ /dev/null @@ -1,30 +0,0 @@ - -#include -#include -#include -#include - -#define sleepMs(x) usleep((x)*1000) - -inline uint64_t getRelativeTimeStampMs() -{ - int rv = -1; - struct timespec ts; - #ifdef CLOCK_MONOTONIC_RAW - if (rv == -1) - rv = clock_gettime(CLOCK_MONOTONIC_RAW, &ts); - #endif - #ifdef _POSIX_MONOTONIC_CLOCK - if (rv == -1) - rv = clock_gettime(CLOCK_MONOTONIC, &ts); - #endif - if (rv == -1) { - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000 + tv.tv_usec / 1000; - } - else { - return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; - } -} - diff --git a/Minetestmapper/porting_win32.h b/Minetestmapper/porting_win32.h deleted file mode 100644 index 7a3f8ab..0000000 --- a/Minetestmapper/porting_win32.h +++ /dev/null @@ -1,10 +0,0 @@ - -#include - -#define sleepMs(x) Sleep(x) - -inline uint64_t getRelativeTimeStampMs() -{ - return GetTickCount64(); -} -