Skip to content

Commit 636dcbb

Browse files
HansBugmikucionisaau
authored andcommitted
dev(hansbug): slimer the code with simply _WIN32 macro
1 parent 716f06a commit 636dcbb

File tree

1 file changed

+3
-62
lines changed

1 file changed

+3
-62
lines changed

src/base/platform.c

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <sys/types.h>
2828
#endif
2929

30-
#ifdef __MINGW32__
30+
#ifdef _WIN32
3131
#include <windows.h>
3232
/* strerror is inconsistent with errno.h numbers on MinGW32 (only?) */
3333
const char* oserror(int error_code)
@@ -38,9 +38,6 @@ const char* oserror(int error_code)
3838
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (LPTSTR)&lpMsgBuf, 0, NULL)) {
3939
return "(error message not available)";
4040
} else {
41-
// hansbug: i didnt change this code to remove \r\n for safety
42-
// but i suggest to optimize this code, the current code can only remove \n
43-
// which will leave a \r on windows
4441
char* eol = lpMsgBuf; // remove end of lines from M$ idiocracies:
4542
while (NULL != (eol = strchr(eol, '\n'))) {
4643
if (eol[1] == 0)
@@ -52,56 +49,14 @@ const char* oserror(int error_code)
5249
}
5350
}
5451

55-
#elif _MSC_VER
56-
57-
#include <windows.h>
58-
#include <strsafe.h>
59-
60-
const char* oserror(int error_code)
61-
{
62-
DWORD dwError = (DWORD)error_code;
63-
LPSTR lpMsgBuf = NULL;
64-
65-
if (!FormatMessageA(
66-
FORMAT_MESSAGE_ALLOCATE_BUFFER |
67-
FORMAT_MESSAGE_FROM_SYSTEM |
68-
FORMAT_MESSAGE_IGNORE_INSERTS,
69-
NULL,
70-
dwError,
71-
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
72-
(LPSTR)&lpMsgBuf,
73-
0,
74-
NULL)) {
75-
return "(error message not available)";
76-
} else {
77-
size_t len = strlen(lpMsgBuf);
78-
79-
while ((len > 0) && ((lpMsgBuf[len - 1] == '\n') || (lpMsgBuf[len - 1] == '\r'))) {
80-
lpMsgBuf[len - 1] = '\0';
81-
len -= 1;
82-
}
83-
int i = 0;
84-
for (; i < len; i++) {
85-
if ((lpMsgBuf[i] == '\n') || (lpMsgBuf[i] == '\r')) {
86-
lpMsgBuf[i] = '.';
87-
}
88-
}
89-
90-
static char errorMsg[256];
91-
StringCchCopyA(errorMsg, sizeof(errorMsg), lpMsgBuf);
92-
LocalFree(lpMsgBuf);
93-
return errorMsg;
94-
}
95-
}
96-
9752
#else /* UNIX */
9853

9954
#include <string.h>
10055
const char* oserror(int error_code) { return strerror(error_code); }
10156

10257
#endif
10358

104-
#if defined(__MINGW32__) || defined(_MSC_VER)
59+
#ifdef _WIN32
10560

10661
void base_getMemInfo(meminfo_t* info)
10762
{
@@ -234,7 +189,7 @@ void base_getMemInfo(meminfo_t* info)
234189

235190
#endif
236191

237-
#if defined(__MINGW32__) || defined(_MSC_VER)
192+
#ifdef _WIN32
238193

239194
#include <psapi.h>
240195
#include <stdint.h>
@@ -252,25 +207,11 @@ void base_getProcInfo(procinfo_t* info)
252207
FILETIME ftExitTime;
253208
FILETIME ftKernelTime;
254209
FILETIME ftUserTime;
255-
#ifdef _MSC_VER
256-
PROCESS_MEMORY_COUNTERS pmc = {0};
257-
#else
258210
PROCESS_MEMORY_COUNTERS pmc;
259-
#endif
260211
pmc.cb = sizeof(pmc);
261212

262213
GetSystemTimeAsFileTime(&now);
263-
264-
#ifdef _MSC_VER
265-
// we have to check the return value of GetProcessMemoryInfo on MSC environment
266-
if (!GetProcessMemoryInfo(hThisProcess, &pmc, sizeof(pmc))) {
267-
memset(info, 0, sizeof(procinfo_t));
268-
return;
269-
}
270-
#else
271214
GetProcessMemoryInfo(hThisProcess, &pmc, sizeof(pmc));
272-
#endif
273-
274215
if (GetProcessTimes(hThisProcess, &ftCreationTime, &ftExitTime, &ftKernelTime, &ftUserTime)) {
275216
li.LowPart = ftUserTime.dwLowDateTime;
276217
li.HighPart = ftUserTime.dwHighDateTime;

0 commit comments

Comments
 (0)