-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_config.c
172 lines (128 loc) · 2.54 KB
/
read_config.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
/*
配置文件sys_config.cfg:
############# sys config ###########
# Using '=' delimited key and value #
#####################################
server_ip = 192.168.1.121
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define FILE_NAME "sys_config.cfg"
#define FILE_PATH_LEN 32
#define LINE_SIZE 512
#define KV_SIZE 64
char portal_sip[32];
void mytrim(char *str)
{
char *start, *end, c;
int len;
if ((len = strlen(str)) == 0) {
return;
}
for (start = str; (c = *start); start++) {
if (!isspace((int)c)) {
break;
}
}
for (end = str + len; (end > start) && (c = *(end - 1)); end--) {
if (!isspace((int)c)) {
break;
}
}
memmove(str, start, (size_t)(end - start));
str[(size_t)(end - start)] = '\0';
}
void set_key_value(char *key, char *value)
{
if (strcmp(key, "server_ip") == 0) {
memcpy(portal_sip, value, strlen(value));
}
}
static int is_space_line(char *line)
{
return !strlen(line);
}
void parse_line_data(char *line, char *key, char *value)
{
char *index = line;
int i = 0;
int line_len = strlen(line);
if (line_len == 0) {
return;
}
if (line_len > LINE_SIZE) {
line_len = LINE_SIZE;
}
while (*index) {
if (*index == '=') {
index++;
i++;
break;
}
if (i >= KV_SIZE) {
printf("key too long! max size is : %d !\n", KV_SIZE);
return;
}
key[i] = line[i];
i++;
index++;
}
if (i >= line_len) {
return;
}
memcpy(value, index, line_len - i);
mytrim(key);
mytrim(value);
set_key_value(key, value);
}
void init_config_file()
{
char file_path[FILE_PATH_LEN] = {0};
char line[LINE_SIZE] = {0};
char key[KV_SIZE] = {0};
char value[KV_SIZE] = {0};
char *index = NULL;
FILE *fp = NULL;
int line_num = 0;
snprintf(file_path, 32, "./%s", FILE_NAME);
fp = fopen(file_path, "r");
if (fp == NULL) {
return;
}
while (fgets(line, LINE_SIZE, fp)) {
index = line;
if ((strlen(line) + 1) >= LINE_SIZE) {
printf("Line greater than or equal to %u characters which is "
"more than the parser is willing to handle.", LINE_SIZE);
printf("line num is : %d\n", line_num);
}
while (isspace((int)*index)) {
index++;
}
if ((*index == '#') || is_space_line(index)) {
line_num++;
continue;
}
memset(key, 0, KV_SIZE);
memset(value, 0, KV_SIZE);
parse_line_data(index, key, value);
line_num++;
}
fclose(fp);
}
void check_args() {
if (strlen(portal_sip) == 0) {
memcpy(portal_sip, "192.168.1.111", 13);
}
}
void init_sys_config()
{
init_config_file();
check_args();
}
int main(int argc, char **argv)
{
init_sys_config();
return 0;
}