-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.c
115 lines (96 loc) · 2.57 KB
/
env.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
typedef enum {
ENV_FALSE = 0,
ENV_TRUE = 1
} env_bool_t;
#ifdef _WIN32
#define strcasecmp(s1, s2) stricmp(s1, s2)
#endif
static env_bool_t zstr(const char *s)
{
return (s == NULL || s[0] == '\0') ? ENV_TRUE : ENV_FALSE;
}
static const char* env_get_string_imp(const char* key, const char* default_value, env_bool_t must_get)
{
char* val = getenv(key);
if (!zstr(val)) {
return val;
}
if (must_get) {
printf("env key \"%s\" does not find!\n", key);
assert(NULL);
} else {
return default_value;
}
}
static const int env_get_int_imp(const char* key, const int default_value, env_bool_t must_get)
{
const char* val = getenv(key);
if (!zstr(val)) {
return atoi(val);
}
if (must_get) {
printf("env key \"%s\" does not find!\n", key);
assert(NULL);
} else {
return default_value;
}
}
static env_bool_t env_true(const char *s)
{
if (zstr(s)) {
return ENV_FALSE;
}
if (!strcasecmp(s, "yes") ||
!strcasecmp(s, "true") ||
!strcasecmp(s, "y") ||
!strcasecmp(s, "t") ||
!strcasecmp(s, "1") ||
!strcasecmp(s, "enabled") ||
!strcasecmp(s, "active") ||
!strcasecmp(s, "allow") ||
!strcasecmp(s, "on")) {
return ENV_TRUE;
}
return ENV_FALSE;
}
static const env_bool_t env_get_bool_imp(const char* key, const env_bool_t default_value, env_bool_t must_get)
{
const char* val = getenv(key);
if (!zstr(val)) {
return env_true(val);
}
if (must_get) {
printf("env key \"%s\" does not find!\n", key);
assert(NULL);
} else {
return default_value;
}
}
const char* env_get_string(const char* key, const char* default_value)
{
return env_get_string_imp(key, default_value, ENV_FALSE);
}
const int env_get_int(const char* key, const int default_value)
{
return env_get_int_imp(key, default_value, ENV_FALSE);
}
const env_bool_t env_get_bool(const char* key, const env_bool_t default_value)
{
return env_get_bool_imp(key, default_value, ENV_FALSE);
}
const char* env_mustget_string(const char* key)
{
return env_get_string_imp(key, NULL, ENV_TRUE);
}
const int env_mustget_int(const char* key)
{
return env_get_int_imp(key, 0, ENV_TRUE);
}
const env_bool_t env_mustget_bool(const char* key)
{
return env_get_bool_imp(key, ENV_FALSE, ENV_TRUE);
}