- Debian写入前检查是否存在处于激活状态的简体中文locale,如果存在则跳过写入

- 如果文件打开失败,抛出异常
This commit is contained in:
TheWhiteDog9487
2026-05-25 15:08:58 +08:00
parent f4678ef4a6
commit a7ef4ee63a
+16 -5
View File
@@ -4,6 +4,7 @@
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
#include <ciso646> #include <ciso646>
#include <vector>
#ifdef __linux__ #ifdef __linux__
#include <unistd.h> #include <unistd.h>
@@ -50,10 +51,8 @@ auto GetCurrentSystemType(){
#ifdef __linux__ #ifdef __linux__
return System::Linux; return System::Linux;
#else
return System::Unsupported;
#endif #endif
} return System::Unsupported; }
auto GetCurrentWslDistro(){ auto GetCurrentWslDistro(){
#pragma warning(suppress : 4996) #pragma warning(suppress : 4996)
@@ -128,9 +127,21 @@ auto Debian(){
cout << "即将开始更新语言配置,请输入y或者yes继续,或者按Ctrl+C终止本程序"; cout << "即将开始更新语言配置,请输入y或者yes继续,或者按Ctrl+C终止本程序";
check(); check();
cout << "开始更新语言配置" << endl; cout << "开始更新语言配置" << endl;
fstream locale_gen("/etc/locale.gen", ios::app); fstream locale_gen("/etc/locale.gen");
if (locale_gen.is_open() == false){
throw system_error(errno, system_category(), "未能正确打开/etc/locale.gen"); }
vector<string> FileContent;
string Buffer = "";
while (getline(locale_gen, Buffer)){
FileContent.push_back(Buffer); }
for (auto& Line : FileContent){
if (Line == "zh_CN.UTF-8 UTF-8"){
goto skip_write; } }
locale_gen.clear();
locale_gen.seekp(0, ios::end);
locale_gen << '\n' << locale_gen << '\n' <<
"zh_CN.UTF-8 UTF-8" << endl; "zh_CN.UTF-8 UTF-8" << endl;
skip_write:
locale_gen.close(); locale_gen.close();
system("locale-gen"); system("locale-gen");
system("update-locale LANG=zh_CN.UTF-8"); system("update-locale LANG=zh_CN.UTF-8");
@@ -138,7 +149,7 @@ auto Debian(){
cout << "输入y或者yes重启或者按Ctrl+C终止本程序" << endl; cout << "输入y或者yes重启或者按Ctrl+C终止本程序" << endl;
check(); check();
RebootIfIsWSL(); RebootIfIsWSL();
system("reboot");} system("reboot"); }
auto OsType = GetCurrentSystemType(); auto OsType = GetCurrentSystemType();