forked from buaazp/zimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zconf.c
120 lines (112 loc) · 3.05 KB
/
zconf.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
/*
* zimg - high performance image storage and processing system.
* http://zimg.buaa.us
*
* Copyright (c) 2013, Peter Zhao <zp@buaa.us>.
* All rights reserved.
*
* Use and distribution licensed under the BSD license.
* See the LICENSE file for full text.
*
*/
/**
* @file zconf.c
* @brief Config read functions.
* @author 招牌疯子 zp@buaa.us
* @version 1.0
* @date 2013-07-19
*/
#include <unistd.h>
#include "zconf.h"
#include "zlog.h"
int get_conf_key(char *conf, char *module, char *key, char *value);
int check_conf(char *conf);
int get_conf_key(char *conf, char *module, char *key, char *value)
{
if(check_conf(conf) == -1)
{
return -1;
}
char buf[MAX_LINE];
FILE *fp = fopen(conf, "r");
char *p;
int mflag = 0, kflag = 0;
if(fp == NULL)
{
LOG_PRINT(LOG_ERROR, "Fail to open config file [%s].", conf);
return -1;
}
while(fgets(buf, 100, fp) != NULL)
{
buf[strlen(buf) - 1]='\0';
if(module != NULL)
{
if(strchr(buf, '[') == buf && strstr(buf, module) == buf+1 && strchr(buf, ']') == buf+1+strlen(module))
{
mflag = 1;
while(fgets(buf, 100, fp) != NULL)
{
buf[strlen(buf) - 1]='\0';
if(strchr(buf, '[') == buf)
{
LOG_PRINT(LOG_INFO, "Can't find key [%s] in the module [%s].", key, module);
fclose(fp);
return -1;
}
else if(strstr(buf, key) == buf && strchr(buf, '=') == buf+strlen(key))
{
p = buf + strlen(key) + 1;
while(isspace(p[0]))
{
p++;
}
strcpy(value, p);
fclose(fp);
LOG_PRINT(LOG_INFO, "Key [%s] = %s", key, value);
return 0;
}
}
LOG_PRINT(LOG_INFO, "Key [%s] Not Found.", key);
fclose(fp);
return -1;
}
}
else
{
if(strstr(buf, key) == buf && strchr(buf, '=') == buf+strlen(key))
{
p = buf + strlen(key) + 1;
while(isspace(p[0]))
{
p++;
}
strcpy(value, p);
fclose(fp);
LOG_PRINT(LOG_INFO, "Key [%s] = %s", key, value);
return 0;
}
}
}
if(mflag == 0)
{
LOG_PRINT(LOG_INFO, "Can't find module [%s] in config file.", module);
}
if(kflag == 0)
{
LOG_PRINT(LOG_INFO, "Key [%s] Not Found.", key);
}
fclose(fp);
return -1;
}
int check_conf(char *conf)
{
if(access(conf, F_OK) == 0)
{
return 1;
}
else
{
LOG_PRINT(LOG_ERROR, "%s is not exist.\n", conf);
return -1;
}
}