-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathminc_config.c
151 lines (127 loc) · 3.09 KB
/
minc_config.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "minc_config.h"
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#endif
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
static const char *_CONFIG_VAR[]=
{
"MINC_FORCE_V2",
"MINC_COMPRESS",
"MINC_CHUNKING",
"MINC_LOGFILE",
"MINC_LOGLEVEL",
"MINC_MAX_FILE_BUFFER_KB",
"MINC_MAX_MEMORY_KB",
"MINC_FILE_CACHE_MB",
"MINC_CHECKSUM",
"MINC_PREFER_V2_API"
};
enum {
_MICFG_MAX_STRING_LENGTH=256
};
/*settings cache*/
static char _CONFIG_VAL[MICFG_COUNT][_MICFG_MAX_STRING_LENGTH];
static int _CONFIG_PRESENT[MICFG_COUNT]={0};
static int _CONFIG_INIT[MICFG_COUNT]={0};
/** Simple function to read a user's .mincrc file, if present.
*/
static int
miread_cfg(const char *name, char *buffer, int maxlen)
{
FILE *fp;
int result = 0;
char *home_ptr = getenv("HOME");
char path[256];
if (home_ptr != NULL) {
strncpy(path, home_ptr, sizeof(path) - 1);
}
else {
path[0] = '\0';
}
strcat(path, "/.mincrc");
if ((fp = fopen(path, "r")) != NULL) {
while (fgets(buffer, maxlen, fp)) {
if (buffer[0] == '#') {
continue;
}
if (!strncasecmp(buffer, name, strlen(name))) {
char *tmp = strchr(buffer, '=');
if (tmp != NULL) {
tmp++;
while (isspace(*tmp)) {
tmp++;
}
strncpy(buffer, tmp, maxlen);
result = 1;
break;
}
}
}
fclose(fp);
}
return (result);
}
const char * miget_cfg_str(int id)
{
if(id<0 || id>=MICFG_COUNT) return "";
if(!_CONFIG_INIT[id])
{
const char *name=_CONFIG_VAR[id];
char buffer[_MICFG_MAX_STRING_LENGTH];
char *var_ptr;
if ((var_ptr = getenv(name)) != NULL) {
strncpy(buffer, var_ptr, _MICFG_MAX_STRING_LENGTH - 1);
_CONFIG_PRESENT[id]=1;
} else {
if (!miread_cfg(name, buffer, _MICFG_MAX_STRING_LENGTH-1)) {
_CONFIG_PRESENT[id]=0;
buffer[0]='\0';
} else {
_CONFIG_PRESENT[id]=1;
}
}
strncpy(_CONFIG_VAL[id],buffer,_MICFG_MAX_STRING_LENGTH-1);
_CONFIG_VAL[id][_MICFG_MAX_STRING_LENGTH-1]='\0';
_CONFIG_INIT[id]=1;
}
return _CONFIG_VAL[id];
}
int miget_cfg_present(int id)
{
if(id <0 || id>=MICFG_COUNT) return 0;
miget_cfg_str(id);
return _CONFIG_PRESENT[id];
}
int miget_cfg_bool(int id)
{
const char *_var=miget_cfg_str(id);
return atoi(_var) != 0;
}
int miget_cfg_int(int id)
{
const char *_var=miget_cfg_str(id);
return atoi(_var);
}
double miget_cfg_double(int id)
{
const char *_var=miget_cfg_str(id);
return atof(_var);
}