Refactor porting
parent
b5b3d29fa3
commit
456189fd16
|
@ -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
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
#include "porting.h"
|
||||
|
||||
#include <cstdio> // fopen
|
||||
#include <cstdlib> // 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);
|
||||
}
|
|
@ -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 <chrono>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#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
|
||||
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
|
||||
#include <ctime>
|
||||
#include <cerrno>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
|
||||
#include <windows.h>
|
||||
|
||||
#define sleepMs(x) Sleep(x)
|
||||
|
||||
inline uint64_t getRelativeTimeStampMs()
|
||||
{
|
||||
return GetTickCount64();
|
||||
}
|
||||
|
Loading…
Reference in New Issue