1+ #ifndef CONFIGFILEPROCESS_H
2+ #define CONFIGFILEPROCESS_H
3+
4+ #include < regex>
5+ #include < iostream>
6+ #include < fstream>
7+ #include " ../MyHelper/myhelper.h"
8+
9+ using namespace std ;
10+ extern string configFilePath;
11+
12+ map<string, string> readConfigFile ();
13+ void writeConfigFile (const map<string, string>& configMap);
14+
15+ // 读取配置文件并存储到一个键值对映射中
16+ map<string, string> readConfigFile () {
17+ string filename = " " ;
18+ if (configFilePath != " " ) {
19+ filename = configFilePath;
20+ }
21+ else {
22+ filename = " config.txt" ;
23+ }
24+ map<string, string> configMap;
25+ ifstream configFile (filename);
26+
27+ if (configFile.is_open ()) {
28+ string line;
29+ while (getline (configFile, line)) {
30+ // 解析每一行的配置项和值
31+ size_t delimiterPos = line.find (' =' );
32+ if (delimiterPos != string::npos) {
33+ string key = trim (line.substr (0 , delimiterPos));
34+ string value = trim (line.substr (delimiterPos + 1 ));
35+ configMap[key] = value;
36+ }
37+ }
38+ configFile.close ();
39+ }
40+ return configMap;
41+ }
42+ // 将配置项写入配置文件
43+ void writeConfigFile (const map<string, string>& configMap) {
44+ string filename = " " ;
45+ if (configFilePath != " " ) {
46+ filename = configFilePath;
47+ }
48+ else {
49+ filename = " config.txt" ;
50+ }
51+ ofstream configFileClear (filename, ofstream::trunc);
52+ configFileClear.close ();
53+ ofstream configFile (filename);
54+ if (configFile.is_open ())
55+ {
56+ for (const auto & pair : configMap) {
57+ configFile << pair.first << " = " << pair.second << endl;
58+ }
59+ configFile.close ();
60+ }
61+ else {
62+ printf (" 无法打开配置文件。\n " );
63+ }
64+ }
65+
66+ // 检查配置文件是否存在并初始化
67+ void checkConfig (string configFilePath) {
68+ // string fileName = "config.txt";
69+ // ifstream file(configFilePath);
70+ if (configFilePath == " " )
71+ {
72+ string fileName = " config.txt" ;
73+ ifstream file (fileName);
74+ if (!file) {
75+ // 配置文件不存在,创建它
76+ ofstream configFile (" config.txt" ); // 创建配置文件
77+ if (configFile.is_open ()) {
78+ // 写入配置项
79+ configFile << " smtpHost = " << endl;
80+ configFile << " smtpPort = " << endl;
81+ configFile << " emailKey = " << endl;
82+ configFile << " sendEmial = " << endl;
83+ configFile << " reciveEmial = " << endl;
84+ configFile << " pushPlusToken = " << endl;
85+ configFile << " WeChatQYID = " << endl;
86+ configFile << " WeChatQYApplicationSecret = " << endl;
87+ configFile << " WeChatQYApplicationID = " << endl;
88+ configFile << " TGBotToken = " << endl;
89+ configFile << " TGBotChatID = " << endl;
90+ configFile << " IsEnableCustomTGBotApi = " << endl;
91+ configFile << " CustomTGBotApi = " << endl;
92+ configFile << " DingTalkAccessToken = " << endl;
93+ configFile << " DingTalkSecret = " << endl;
94+ configFile << " BarkUrl = " << endl;
95+ configFile << " BrakKey = " << endl;
96+ configFile << " ShellPath = " << endl;
97+ configFile << " apiPort = " << endl;
98+ configFile << " ForwardDeviceName = " << endl;
99+ configFile << " smsCodeKey = 验证码±verification±code±인증±代码±随机码" << endl;
100+ configFile << " forwardIgnoreStorageType = sm" << endl;
101+ configFile.close ();
102+ }
103+ else {
104+ cout << " 无法打开配置文件。" << endl;
105+ }
106+ }
107+ }
108+ }
109+ #endif // CONFIGFILEPROCESS_H
0 commit comments