-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir_item.cpp
61 lines (56 loc) · 1.72 KB
/
dir_item.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
//
// Created by xhl on 2023/6/28.
//
#include "dir_item.hpp"
dir_item::dir_item(const utils::fs::path &_full_path) {
this->full_path = _full_path;
this->short_name = _full_path.filename();
this->has_asm = false;
this->prt_cleared = false;
}
void dir_item::dump_to_file() {
if (this->children.empty()) return;
auto stem{full_path.filename().wstring() + L".mnu"};
auto filename{full_path / utils::fs::path(stem)};
std::ofstream mnu_file(filename, std::ios::out | std::ios::binary);
// for the title
std::wstring mnu_content{this->short_name + L"\n#\n#\n"};
// for the items
for (const auto &child: children) {
std::wstring comment{child};
if (child.find_first_of('/') not_eq std::wstring::npos) {
comment = child.substr(3);
}
mnu_content.append(child).append(L"\n备注-").append(comment).append(L"\n#\n");
}
mnu_file << utils::ToGBK(mnu_content);
mnu_file.flush();
mnu_file.close();
}
void dir_item::add_child(const utils::fs::path &child) {
if (utils::fs::is_directory(child)) {
this->children.insert(L"/" + child.filename().wstring());
return;
}
auto filename{utils::TrimNumSuffix(child.filename().wstring())};
bool is_asm{utils::EndsWith(filename, L".asm")};
bool is_prt{utils::EndsWith(filename, L".prt")};
if (is_asm) {
this->has_asm = true;
if (prt_cleared) {
goto emplace;
}
for (auto it{children.begin()}; it not_eq children.end();) {
if (utils::EndsWith(*it, L".prt")) {
it = children.erase(it);
} else {
++it;
}
}
this->prt_cleared = true;
}
emplace:
if (not has_asm and is_prt or has_asm and is_asm) {
this->children.insert(filename);
}
}