/
keyvalue.c
197 lines (156 loc) · 3.79 KB
/
keyvalue.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* cassbeam - a Cassegrain antenna simulator
Copyright (C) August 18, 2003 Walter Brisken
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see
<http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "keyvalue.h"
struct KeyValue *newKeyValue()
{
struct KeyValue *p;
p = g_new(struct KeyValue, 1);
p->nparms = 0;
return p;
}
void deleteKeyValue(struct KeyValue *p)
{
int i;
if(!p) return;
for(i = 0; i < p->nparms; i++)
{
g_free(p->key[i]);
g_free(p->value[i]);
}
g_free(p);
}
void KeyValueaddparm(struct KeyValue *p, const char *key, const char *value)
{
p->key[p->nparms] = g_strdup(key);
p->value[p->nparms] = g_strdup(value);
p->nparms++;
}
void KeyValueupdateparm(struct KeyValue *p, const char *key, const char *value)
{
int i;
i = KeyValuekeyindex(p, key);
if(i >= 0)
{
g_free(p->value[i]);
p->value[i] = g_strdup(value);
}
else
{
p->key[p->nparms] = g_strdup(key);
p->value[p->nparms] = g_strdup(value);
p->nparms++;
}
}
void KeyValueupdateparmdouble(struct KeyValue *p, const char *key, double value)
{
int i;
char v[100];
sprintf(v, "%f", value);
i = KeyValuekeyindex(p, key);
if(i >= 0)
{
g_free(p->value[i]);
p->value[i] = g_strdup(v);
}
else
{
p->key[p->nparms] = g_strdup(key);
p->value[p->nparms] = g_strdup(v);
p->nparms++;
}
}
struct KeyValue *loadKeyValue(const char *filename)
{
struct KeyValue *p;
FILE *in;
int i;
char str[1000], K[200], V[200];
if(strcmp(filename, "-") == 0) in = stdin;
else in = fopen(filename, "r");
if(!in)
{
fprintf(stderr, "loadKeyValue: no file: %s\n", filename);
return 0;
}
p = newKeyValue();
while(1)
{
fgets(str, 999, in);
if(feof(in)) break;
for(i = 0; str[i] != 0; i++)
{
if(str[i] == '#') str[i] = 0;
else if(str[i] == '=') str[i] = ' ';
}
if(str[0] == 0) continue;
if(sscanf(str, "%s %s\n", K, V) != 2) continue;
KeyValueaddparm(p, K, V);
}
if(strcmp(filename, "-") != 0) fclose(in);
return p;
}
int saveKeyValue(const struct KeyValue *kv, const char *filename)
{
int i;
FILE *out;
out = fopen(filename, "w");
if(!out) return 0;
for(i = 0; i < kv->nparms; i++)
fprintf(out, "%s = %s\n", kv->key[i], kv->value[i]);
fclose(out);
return 1;
}
/* return -1 if not in the list of keys */
int KeyValuekeyindex(const struct KeyValue *p, const char *key)
{
int i;
for(i = 0; i < p->nparms; i++)
if(strcmp(p->key[i], key) == 0) return i;
return -1;
}
int getKeyValueint(const struct KeyValue *p, const char *key)
{
int i = KeyValuekeyindex(p, key);
if(i < 0) return KV_INTERR;
return atoi(p->value[i]);
}
double getKeyValuedouble(const struct KeyValue *p, const char *key)
{
int i = KeyValuekeyindex(p, key);
if(i < 0) return KV_FLOATERR;
return atof(p->value[i]);
}
Vector getKeyValueVector(const struct KeyValue *p, const char *key)
{
int i = KeyValuekeyindex(p, key);
if(i < 0) return 0;
return newVectorfromstring(p->value[i]);
}
const char *getKeyValuestring(const struct KeyValue *p, const char *key)
{
int i = KeyValuekeyindex(p, key);
if(i < 0) return 0;
return p->value[i];
}
void printKeyValue(const struct KeyValue *p)
{
int i;
for(i = 0; i < p->nparms; i++)
printf("KV: %s = %s\n", p->key[i], p->value[i]);
}