-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_config.cpp
169 lines (141 loc) · 2.86 KB
/
a_config.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/****************************************************************
xerxes engine
a_common.cpp
****************************************************************/
#include "xerxes.h"
/***************************** data *****************************/
#define KEYSIZE 20
#define VALUESIZE 80
char cfgfn[255];
struct config_key
{
char keyname[KEYSIZE];
char value[VALUESIZE];
config_key *next;
};
config_key *cf_top;
/***************************** code *****************************/
void cfg_Init(char *fn)
{
char temp[80];
config_key *cf;
GetCurrentDirectory(255, cfgfn);
strcat(cfgfn, va("\\%s",fn));
cf_top = 0;
FILE *f = fopen(fn, "r");
if (!f) return;
cf = 0;
while (!feof(f))
{
fscanf(f,"%s%*c", temp);
if (strlen(temp)>1 && isletter(temp[0]) && !feof(f))
{
if (strlen(temp)>=KEYSIZE)
err("cfg_Init(), key too big");
if (!cf)
cf = cf_top = (config_key *) malloc(sizeof config_key);
else
{
cf->next = (config_key *) malloc(sizeof config_key);
cf = cf->next;
}
strcpy(cf->keyname, temp);
fgets(cf->value, VALUESIZE-1, f);
strclean(cf->value);
cf->next = 0;
}
}
fclose(f);
}
bool cfg_KeyPresent(char *key)
{
_ASSERTE(strlen(key)<KEYSIZE);
config_key *cf = cf_top;
while (cf)
{
if (!strcasecmp(key, cf->keyname))
return true;
cf = cf->next;
}
return false;
}
char *cfg_GetKeyValue(char *key)
{
_ASSERTE(strlen(key)<KEYSIZE);
config_key *cf = cf_top;
while (cf)
{
if (!strcasecmp(key, cf->keyname))
return cf->value;
cf = cf->next;
}
return NULL;
}
void cfg_SetKeyValue(char *key, char *value)
{
_ASSERTE(strlen(key)<KEYSIZE);
_ASSERTE(strlen(value)<VALUESIZE);
if (!cf_top)
{
cf_top = (config_key *) malloc(sizeof config_key);
strcpy(cf_top->keyname, key);
strcpy(cf_top->value, value);
cf_top->next = 0;
return;
}
config_key *cf = cf_top, *last = cf_top;
while (cf)
{
if (!strcasecmp(key, cf->keyname))
{
strcpy(cf->value, value);
return;
}
last = cf;
cf = cf->next;
}
last->next = (config_key *) malloc(sizeof config_key);
cf = last->next;
strcpy(cf->keyname, key);
strcpy(cf->value, value);
cf->next = 0;
}
void cfg_SetDefaultKeyValue(char *key, char *value)
{
if (cfg_KeyPresent(key)) return;
cfg_SetKeyValue(key, value);
}
void cfg_DeleteKey(char *key)
{
_ASSERTE(strlen(key)<KEYSIZE);
config_key *store, *last = 0, *cf = cf_top;
while (cf)
{
if (!strcasecmp(key, cf->keyname))
{
store = cf->next;
free(cf);
if (last) last->next = store;
else cf_top = store;
return;
}
last = cf;
cf = cf->next;
}
return;
}
void cfg_WriteConfig()
{
bool first = true;
config_key *cf = cf_top;
FILE *f = fopen(cfgfn, "w");
if (!f) err("cfg_WriteConfig(), could not open config file!");
while (cf)
{
if (!first) fprintf(f,"\n");
fprintf(f, "%s %s", cf->keyname, cf->value);
cf = cf->next;
first = false;
}
fclose(f);
}