This repository was archived by the owner on Nov 17, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbookmark_store.cpp
More file actions
182 lines (129 loc) · 4.96 KB
/
Copy pathbookmark_store.cpp
File metadata and controls
182 lines (129 loc) · 4.96 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdafx.h"
#include <map>
#include "utils.h"
#include "bookmark_store.h"
bookmark_store::bookmark_store() {
//..
}
bookmark_store::~bookmark_store()
{
//..
}
void bookmark_store::Write(bool thread_pool) {
if (!m_is_dirty) {
FB2K_console_print_v("Saving... nothing to do.");
return;
}
setlocale(LC_ALL, ".UTF8");
if (thread_pool) {
if (!is_cfg_Instant_Write()) {
FB2K_console_print_v("Saving later.");
return;
}
_write();
}
else {
auto work = [this] {
try {
this->m_persist.writeDataFileJSON(m_masterList);
this->m_is_dirty = false;
FB2K_console_print_v("Saved.");
}
catch (std::exception const&) {
//..
}
};
fb2k::splitTask(work);
return;
}
}
using bm_rev_it_type = std::vector<bookmark_t>::reverse_iterator;
using keep_map_rev_it_type = std::map <std::string, bm_rev_it_type>::reverse_iterator;
std::vector<bookmark_t> bookmark_store::Discard_Bookmarks(std::vector<bookmark_t> master_list) {
size_t keep_n_latest = is_cfg_cu_keep_tail() ? atoi(cfg_cu_keep_tail_count.get_value()) : 0;
pfc::string8 keep_comment_prefix= cfg_cu_keep_com_prefix;
bool keep_non_radio_seek = is_cfg_cu_keep_seek();
bool keep_backup = is_cfg_cu_keep_backup();
if (keep_backup) {
pfc::string8 n8_path = core_api::pathInProfile("configuration");
extract_native_path(n8_path, n8_path);
std::filesystem::path os_dat = std::filesystem::u8path(n8_path.c_str());
std::filesystem::path os_bak = std::filesystem::u8path(n8_path.c_str());
pfc::string8 file_date;
currentFmtDate(file_date, "_%y-%m-%d_%Hh_%Mm_%Ss");
pfc::string8 filename = core_api::get_my_file_name();
pfc::string8 filename_bak = core_api::get_my_file_name();
filename << ".dll.dat";
filename_bak << file_date << ".dll.dat";
os_dat.append(filename.c_str());
os_bak.append(filename_bak.c_str());
std::error_code ec;
if (std::filesystem::exists(os_dat)) {
std::filesystem::copy_file(os_dat, os_bak, std::filesystem::copy_options::overwrite_existing, ec);
}
}
size_t keep_n_latest_fixed = (master_list.size() >= keep_n_latest ? keep_n_latest : master_list.size());
std::map<std::string, bm_rev_it_type> keep_bms;
bm_rev_it_type it = master_list.rbegin() + keep_n_latest_fixed;
while (it != master_list.rend()) {
it = std::find_if(it, master_list.rend(), [](const auto bmit) { return (bool)bmit.guid_playlist.Data1; });
if (it == master_list.rend()) {
//
continue;
//
}
keep_map_rev_it_type keep_rev_it = std::find_if(keep_bms.rbegin(), keep_bms.rend(),
[it](const std::pair<std::string, bm_rev_it_type>& kbm) {
bool bres = (bool)it->guid_playlist.Data1;
return bres && pfc::guid_equal(pfc::GUID_from_text(kbm.first.c_str()), it->guid_playlist);
});
if (keep_rev_it == keep_bms.rend()) {
keep_bms.emplace(std::pair{ pfc::print_guid(it->guid_playlist), it });
};
it = std::find_if(it, master_list.rend(), [it](bookmark_t const bm) { return !pfc::guid_equal(bm.guid_playlist, it->guid_playlist); });
}
bm_rev_it_type& it_delete = master_list.rbegin() + keep_n_latest_fixed;
std::vector<std::string> v_pl_done;
while (it_delete != master_list.rend()) {
size_t curr_ndx = master_list.size() - std::distance(it_delete, master_list.rend());
bool pre_cond = (keep_comment_prefix.get_length() && it_delete->get_comment().startsWith(keep_comment_prefix)) ||
(keep_non_radio_seek && !it_delete->isRadio() && it_delete->get_time() != 0.0);
if ((bool)it_delete->guid_playlist.Data1 && !pre_cond) {
bool bwasdone = std::find(v_pl_done.begin(), v_pl_done.end(),
pfc::print_guid(it_delete->guid_playlist).c_str()) != v_pl_done.end();
if (bwasdone) {
it_delete->subsong = 9999;
it_delete++;
continue;
}
}
keep_map_rev_it_type/*auto*/ keep_find_rev_it = std::find_if(keep_bms.rbegin(), keep_bms.rend(),
[it_delete](const std::pair<std::string, bm_rev_it_type>& kbm) {
return it_delete == kbm.second; });
if (keep_find_rev_it != keep_bms.rend()) {
it_delete++;
v_pl_done.push_back(pfc::print_guid(keep_find_rev_it->second->guid_playlist).c_str());
//
continue;
//
}
it_delete++;
}
int to_remove = (std::max)((int)(master_list.size() - keep_n_latest - keep_bms.size()), 0);
size_t c_removed = 0;
auto new_end = std::remove_if(master_list.begin(), master_list.end() - keep_n_latest_fixed,
[to_remove, &c_removed, keep_comment_prefix, keep_non_radio_seek](bookmark_t bm) {
bool pre_cond = (keep_comment_prefix.get_length() && bm.get_comment().startsWith(keep_comment_prefix)) ||
(keep_non_radio_seek && !bm.isRadio() && bm.get_time() != 0.0);
if (!pre_cond && ((c_removed < static_cast<size_t>(to_remove) && !(bool)bm.guid_playlist.Data1) || bm.subsong == 9999)) {
c_removed++;
return true;
}
return false;
});
std::vector<bookmark_t> back;
back.insert(back.begin(), master_list.end() - keep_n_latest_fixed, master_list.end());
master_list.erase(new_end, master_list.end());
master_list.insert(master_list.end(), back.begin(), back.end());
return master_list;
}