-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.cpp
More file actions
52 lines (42 loc) · 966 Bytes
/
system.cpp
File metadata and controls
52 lines (42 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include"system.h"
#ifdef _WIN32
#include<windows.h>
#else
#include<cerrno>
#include<cstring>
#endif
namespace godefv{namespace os{
error_t::error_t(int errcode):code(errcode){}
#ifdef _WIN32
std::string error_t::string() const{
if(!code) return std::string{"(no error)"};
LPVOID lpMsgBuf;
DWORD bufLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
if(!bufLen) return std::string{};
LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
std::string result(lpMsgStr, lpMsgStr+bufLen);
LocalFree(lpMsgBuf);
return result;
}
#else
std::string error_t::string() const{
char * e = strerror(code);
return e ? e : "";
}
#endif
error_t last_error() noexcept{
#ifdef _WIN32
return error_t{static_cast<int>(GetLastError())};
#else
return error_t{errno};
#endif
}
}} /* namespace godefv::os */