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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "options.h"
struct sect sect_hd = {""};
static int to_integer(const char *str, int n)
{
int i = *str=='-';
if (str[i]>'0' && str[i]<='9') {
i++;
for (; i<n; i++)
if (!isdigit(str[i]))
return 0;
return atoi(str);
}
return 0;
}
int strtoval(char *str, union val *val)
{
int n;
while (isspace(*str))
str++;
n = strlen(str);
while (n && isspace(str[n-1]))
n--;
val->integ = 0;
if (n==1 && *str=='0')
return 0;
if (n >= 4) {
str[n] = '\0';
val->p = str;
return 2;
}
if ((val->integ = to_integer(str, n)) != 0) {
return 0;
}
strncpy(val->str, str, 4);
if (n && n<4)
val->str[n] = '\0';
return 1;
}
static struct sect *getsect(const char *name, struct sect **prev)
{
struct sect *s = §_hd;
if (!name[0])
return s;
while (s->next) {
if (!strcmp(name, s->next->name)) {
if (prev)
*prev = s;
return s->next;
}
s = s->next;
}
return NULL;
}
struct sect *addsect(const char *name)
{
struct sect *s = getsect(name, NULL);
if (!s) {
s = malloc(sizeof(struct sect));
strcpy(s->name, name);
s->opts = NULL;
s->next = sect_hd.next;
sect_hd.next = s;
}
return s;
}
static struct option *newopt(const char *key, union val val, int tp)
{
struct option *o;
int n = sizeof(struct option) + strlen(key) - 2;
int m = 0;
char *p;
if (tp == 1 && val.str[3])
m = 5;
if (tp == 2)
m = strlen(val.p)+1;
o = malloc(n+m);
o->val = val;
if (m) {
p = (char *) o;
p += n;
if (tp == 2) {
strncpy(o->val.str, val.p, 4);
strcpy(p, val.p);
} else {
memcpy(p, val.str, 4);
p[4] = '\0';
}
tp = 1;
}
o->tp_key[0] = tp;
strcpy(opt_key(o), key);
return o;
}
void addopt(const char *key, union val val, int tp, struct sect *sect)
{
struct option *o = newopt(key, val, tp);
if (!sect)
sect = §_hd;
o->next = sect->opts;
sect->opts = o;
}
struct option *getoptions(const char *sect_name)
{
struct sect *s = getsect(sect_name, NULL);
if (s)
return s->opts;
return NULL;
}
/* getopt might be declared in stdio.h */
static struct option *get_opt(const char *sect_name, const char *key)
{
struct option *o = getoptions(sect_name);
while (o) {
if (!strcmp(key, opt_key(o)))
return o;
o = o->next;
}
return NULL;
}
int getopt_int(const char *sect_name, const char *key)
{
struct option *o = get_opt(sect_name, key);
if (o) {
const char *s = opt_longstr(o);
if (s) {
int d = to_integer(s, strlen(s));
if (d != 0)
return d;
}
return o->val.integ;
}
return 0;
}
char *getopt_str(const char *sect_name, const char *key)
{
struct option *o = get_opt(sect_name, key);
if (o)
return opt_longstr(o);
return NULL;
}
char *opt_longstr(struct option *o)
{
if (opt_isint(o))
return NULL;
if (!o->val.str[3])
return o->val.str;
return (char *) o + (sizeof(struct option) + strlen(opt_key(o)) - 2);
}
static void rmopt(const char *key, struct option *o)
{
struct option *p;
while (o->next) {
if (!strcmp(key, opt_key(o->next))) {
p = o->next;
o->next = p->next;
free(p);
} else
o = o->next;
}
}
void setoption(const char *sect_name, const char *key, union val val, int tp)
{
struct sect *s = addsect(sect_name);
struct option *o;
if (tp==0 || tp==1 && !val.str[3]) {
o = s->opts;
while (o) {
if (!strcmp(key, opt_key(o))) {
o->val = val;
o->tp_key[0] = tp;
rmopt(key, o);
return;
}
o = o->next;
}
}
addopt(key, val, tp, s);
rmopt(key, s->opts);
}
void unsetoption(const char *sect_name, const char *key)
{
struct sect *s = getsect(sect_name, NULL);
struct option *o;
if (!s || !s->opts)
return;
o = s->opts;
if (!strcmp(key, opt_key(o))) {
s->opts = o->next;
free(o);
} else
rmopt(key, o);
}
void freeoptions(const char *sect_name)
{
struct sect *s, *p;
struct option *o;
if (sect_name[0]) {
s = getsect(sect_name, &p);
if (!s)
return;
p->next = s->next;
} else {
while (sect_hd.next)
freeoptions(sect_hd.next->name);
s = §_hd;
}
while (o = s->opts) {
s->opts = o->next;
free(o);
}
if (sect_name[0])
free(s);
else
s->opts = NULL;
}
|