Repository navigation
Expand file tree
/
Copy pathreadconfig.h
More file actions
99 lines (83 loc) · 3.13 KB
/
Copy pathreadconfig.h
File metadata and controls
99 lines (83 loc) · 3.13 KB
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
/*
實現讀取文件與自動填充配置至結構體功能(類動態變量實現)
*/
/*-------------------------------------------------------
Copyright (C) 2026.7.25 by Bill_LTL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
---------------------------------------------------------*/
void read_config(char *first, const char *time);/*讀取配置文件函數
first用於確認是否進行過讀取操作,如已讀取過,則不打印初始化信息*/
void auto_fill(const char *time, char *key, char *val);//自動填充配置結構體函數
/*-------- 配置結構體 ----------*/
struct Config {
char api[128];
//char system[64];
char cmd_get6[128];
char zoneID[128];
char recordID[128];
//char DNS[128];
char domain[128];
char waittime[8];
char ttl[8];
char proxy[8];
char ping6[128];
} cfg;
//-----------配置變量偏移量表------------
struct {
char *name;
size_t offset;
} map[] = {
{"api", offsetof(struct Config, api)},
//{"system", offsetof(struct Config, system)},
{"cmd_get6", offsetof(struct Config, cmd_get6)},
{"zoneID", offsetof(struct Config, zoneID)},
{"recordID", offsetof(struct Config, recordID)},
//{"DNS", offsetof(struct Config, DNS)},
{"domain", offsetof(struct Config, domain)},
{"waittime", offsetof(struct Config, waittime)},
{"ttl", offsetof(struct Config, ttl)},
{"proxy", offsetof(struct Config, proxy)},
{"ping6", offsetof(struct Config, ping6)},
};
//-------------------------------------
void read_config(char *first, const char *time) {
//讀取配置文件
char key[64];
char value[64];
char line[256];
FILE *fp = fopen(".config", "r");
if (fp == NULL) {
printf("%s [錯誤] 無法開啟 .config 文件!\n", time);
return;
}
while (fgets(line, sizeof(line), fp)) {
if (sscanf(line, " %[^ \t=] %*[ \t=] %[^\r\n]", key, value) == 2) {
if(*first==0){
printf("%s 已解析: key=[%s], value=[%s]\n", time, key, value);
}
auto_fill(time, key, value);
}
}
fclose(fp);
*first = 1;
}
//實現填充功能,將key對應到結構體成員並賦值
//匹配key到變量,賦值value,,,,獲取IPv6命令(無則默認)
void auto_fill(const char *time, char *key, char *val) {
for (int i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
if (strcmp(key, map[i].name) == 0) {
char *target_address = (char *)&cfg + map[i].offset;
strcpy(target_address, val);
}
}
return;
}