-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtilities.h
61 lines (47 loc) · 1.2 KB
/
Utilities.h
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
53
54
55
56
57
58
59
60
61
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
char * itoa_(int i) {
static char buffer[21] = { 0 };
char* c = buffer + 19; // buffer[20] must be \0
int x = abs(i);
do {
*--c = 48 + x % 10;
} while (x && (x /= 10));
if (i < 0) *--c = 45;
return c;
}
long getenvnum(const char* name) {
static char buffer[32] = { 0 };
return
GetEnvironmentVariable(name, buffer, sizeof(buffer))
? atol(buffer)
: 0;
}
long getenvnum_ex(const char* name, int default_val) {
static char buffer[32] = { 0 };
return
GetEnvironmentVariable(name, buffer, sizeof(buffer))
? atol(buffer)
: default_val;
}
char* readenv(const char* name) {
static TCHAR buffer[127];
GetEnvironmentVariable(name, buffer, sizeof buffer);
return _strdup(buffer); // memory leaks go brr
}
#ifndef WIN2K_BUILD
void usleep(__int64 usec) {
static HANDLE timer = NULL;
static LARGE_INTEGER ft;
ft.QuadPart = -(10 * usec); // Convert to 100 nanosecond interval, negative value indicates relative time
if (timer == NULL) [[unlikely]] { timer = CreateWaitableTimer(NULL, TRUE, NULL); }
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
}
#endif
#ifdef __cplusplus
}
#endif