Skip to content

Commit 081d6c5

Browse files
committed
refactor: optimize SimpleIni creation
1 parent 978fa0a commit 081d6c5

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/legacy/utils/IniHelper.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
#include <filesystem>
77

88
std::unique_ptr<SimpleIni> SimpleIni::create(const std::string& path, const std::string& defContent) {
9-
if (!std::filesystem::exists(ll::string_utils::str2wstr(path))) {
10-
// 创建新的
11-
std::filesystem::create_directories(
12-
std::filesystem::path(ll::string_utils::str2wstr(path)).remove_filename().u8string()
13-
);
14-
9+
namespace fs = std::filesystem;
10+
auto fpath = fs::path(ll::string_utils::str2wstr(path));
11+
if (!fpath.empty() && !fs::exists(fpath)) { // Create new file
12+
if (fpath.has_root_directory() && fpath.has_parent_path()) {
13+
fs::create_directories(fpath.parent_path());
14+
}
1515
std::ofstream iniFile(path);
1616
if (iniFile.is_open() && defContent != "") iniFile << defContent;
1717
iniFile.close();
1818
}
1919

20-
// 已存在
20+
// Exist
2121
auto root = std::make_unique<SimpleIni>();
2222
root->SetUnicode(true);
2323
auto res = root->LoadFile(path.c_str());

0 commit comments

Comments
 (0)