-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathINIValidator.cpp
108 lines (97 loc) · 3.09 KB
/
INIValidator.cpp
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
#include "Checker.h"
#include "Helper.h"
#include "IniFile.h"
#include "Log.h"
#include "Settings.h"
#include <filesystem>
#include <iostream>
#include <regex>
#include <string>
#include <windows.h>
static void loadFromInput(IniFile& targetIni) {
std::string targetFilePath;
std::cout << "请输入要检查的INI文件路径: ";
std::getline(std::cin, targetFilePath);
targetFilePath = std::regex_replace(targetFilePath, std::regex("^\"|\"$"), "");
if (!targetFilePath.empty()) {
std::filesystem::path targetPath(targetFilePath);
if (std::filesystem::is_regular_file(targetPath))
targetIni.load(targetFilePath);
else if (std::filesystem::is_directory(targetPath)) {
for (const auto& filePath : targetFilePath)
targetIni.load(std::to_string(filePath));
}
else {
std::cerr << "输入路径无效,无法加载文件: " << targetFilePath << std::endl;
loadFromInput(targetIni);
}
}
else if (!Settings::Instance->folderPath.empty()) {
// 用户直接按回车,使用默认目录
std::string defaultDir = Settings::Instance->folderPath;
for (const auto& entry : std::filesystem::directory_iterator(defaultDir)) {
if (entry.is_regular_file() && entry.path().extension() == ".ini")
targetIni.load(entry.path().string());
}
}
else {
std::cerr << "默认路径为空,无法加载文件。" << std::endl;
loadFromInput(targetIni);
}
}
static void loadFromArg(int argc, char* argv[], IniFile& targetIni) {
for (int i = 1; i < argc; ++i) {
std::filesystem::path path(argv[i]);
if (std::filesystem::is_regular_file(path))
targetIni.load(path.string());
else if (std::filesystem::is_directory(path)) {
std::vector<std::string> filePaths;
for (const auto& entry : std::filesystem::recursive_directory_iterator(path.string()))
if (entry.is_regular_file() && entry.path().extension() == ".ini")
targetIni.load(entry.path().string());
}
else {
std::cerr << "错误路径: " << path.string();
loadFromInput(targetIni);
}
}
}
static void loadAgain(IniFile& targetIni) {
std::cout << "按任意键继续检测..." << std::endl;
std::cin.ignore((std::numeric_limits<std::streamsize>::max)(), '\n');
std::cout << "\033[2J\033[H"; // 清空控制台
targetIni = IniFile();
loadFromInput(targetIni);
}
int main(int argc, char* argv[]) {
try {
SetConsoleOutputCP(CP_UTF8);
system("title INI Validator");
#ifndef _DEBUG
std::filesystem::path exe_path = std::filesystem::path(argv[0]).parent_path();
std::filesystem::current_path(exe_path);
#endif // !_DEBUG
auto log = Log();
Settings setting(IniFile("Settings.ini", true));
IniFile configIni("INICodingCheck.ini", true);
IniFile targetIni;
if (argc >= 2)
loadFromArg(argc, argv, targetIni);
else
loadFromInput(targetIni);
SetConsoleCP(CP_UTF8);
while (true) {
Checker checker(configIni, targetIni);
checker.checkFile();
log.output();
std::cout << "\n检查完毕" << std::endl;
loadAgain(targetIni);
}
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
system("pause>nul");
return 0;
}