forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibconfig.c
135 lines (121 loc) · 2.87 KB
/
libconfig.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
/*****************************************************************************
* Copyright (C) 2014-2015
* file: libconfig.c
* author: gozfree <[email protected]>
* created: 2015-09-29 23:51
* updated: 2015-09-29 23:51
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <libmacro.h>
#include <liblog.h>
#include "libconfig.h"
extern struct config_ops ini_ops;
extern struct config_ops json_ops;
extern struct config_ops lua_ops;
struct config *g_config = NULL;
struct config_ops_list {
char suffix[32];
struct config_ops *ops;
};
static struct config_ops_list conf_ops_list[] = {
{"lua", &lua_ops},
{"json", &json_ops},
{"ini", &ini_ops}
};
static char *get_file_suffix(const char *name)
{
int point = '.';
char *tmp = strrchr((char *)name, point);
if (tmp) {
return tmp+1;
}
return NULL;
}
static struct config_ops *find_backend(const char *name)
{
if (!name) {
loge("config name can not be NULL\n");
return NULL;
}
int i = 0;
int max_list = (sizeof(conf_ops_list)/sizeof(config_ops_list));
char *suffix = get_file_suffix(name);
if (!suffix) {
loge("there is no suffix in config name\n");
return NULL;
}
for (i = 0; i < max_list; i++) {
if (!strcasecmp(conf_ops_list[i].suffix, suffix)) {
break;
}
}
if (i == max_list) {
loge("the %s file is not supported\n", suffix);
return NULL;
}
return conf_ops_list[i].ops;
}
struct config *conf_load(const char *name)
{
struct config_ops *ops = find_backend(name);
if (!ops) {
loge("can not find valid config backend\n");
return NULL;
}
struct config *c = CALLOC(1, struct config);
if (!c) {
loge("malloc failed!\n");
return NULL;
}
c->ops = ops;
if (c->ops->load) {
if (-1 == c->ops->load(c, name)) {
free(c);
return NULL;
}
}
g_config = c;
return c;
}
int conf_set(struct config *c, const char *key, const char *val)
{
if (!c || !c->ops->set_string)
return -1;
return c->ops->set_string(c, key, val, NULL);
}
void conf_del(struct config *c, const char *key)
{
if (!c || !c->ops->del)
return;
return c->ops->del(c, key);
}
void conf_dump(struct config *c)
{
if (!c || !c->ops->dump)
return;
return c->ops->dump(c, stderr);
}
int conf_save(struct config *c)
{
if (!c || !c->ops->save)
return -1;
return c->ops->save(c);
}
void conf_dump_to_file(FILE *f, struct config *c)
{
if (!c || !c->ops->dump)
return;
return c->ops->dump(c, f);
}
void conf_unload(struct config *c)
{
if (c && c->ops->unload) {
c->ops->unload(c);
}
free(c);
}