Change to use sets instead of vectors
This simplifies the editing handlers.
This commit is contained in:
43
src/main.cpp
43
src/main.cpp
@@ -28,34 +28,32 @@ using namespace std;
|
||||
set<string> additions;
|
||||
string change;
|
||||
map<string, string> changes;
|
||||
vector<string> deletions;
|
||||
set<string> deletions;
|
||||
bool should_list = false;
|
||||
|
||||
static void handle_additions(vector<string>& cur_tags) {
|
||||
set<string> tags {cur_tags.begin(), cur_tags.end()};
|
||||
static void handle_additions(set<string>& tags) {
|
||||
tags.insert(additions.begin(), additions.end());
|
||||
cur_tags.clear();
|
||||
copy(tags.begin(), tags.end(), back_inserter(cur_tags));
|
||||
}
|
||||
|
||||
static void handle_changes(vector<string>& tags) {
|
||||
static void handle_changes(set<string>& tags) {
|
||||
for (const auto& elem : changes) {
|
||||
auto it = find(tags.begin(), tags.end(), elem.first);
|
||||
auto it = tags.find(elem.first);
|
||||
if (it != tags.end()) {
|
||||
*it = elem.second;
|
||||
tags.erase(*it);
|
||||
tags.insert(elem.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_deletions(vector<string>& tags) {
|
||||
for (const string& tag : deletions) {
|
||||
tags.erase(remove(tags.begin(), tags.end(), tag), tags.end());
|
||||
}
|
||||
static void handle_deletions(set<string>& tags) {
|
||||
set_difference(tags.begin(), tags.end(),
|
||||
deletions.begin(), deletions.end(),
|
||||
inserter(tags, tags.begin()));
|
||||
}
|
||||
|
||||
static void process(const char* path) {
|
||||
const char* tagname {"user.xdg.tags"};
|
||||
vector<string> tags;
|
||||
set<string> tags;
|
||||
vector<char> buf(1024);
|
||||
ssize_t sz = 0;
|
||||
do {
|
||||
@@ -72,24 +70,31 @@ static void process(const char* path) {
|
||||
} while (sz == -1 && errno == ERANGE);
|
||||
if (sz > 0) {
|
||||
string val(buf.data(), sz);
|
||||
tags = sc::split(val, ",");
|
||||
vector<string> cur_tags = sc::split(val, ",");
|
||||
tags.insert(cur_tags.begin(), cur_tags.end());
|
||||
}
|
||||
bool edits = additions.size() > 0 || changes.size() > 0 || deletions.size() > 0;
|
||||
if (edits) {
|
||||
if (should_list) {
|
||||
cout << path << '\n';
|
||||
cout << " before: " << sc::io::greenf << sc::join(tags, ", ") << sc::io::reset << '\n';
|
||||
cout << " before: " << sc::io::greenf
|
||||
<< sc::join(tags.begin(), tags.end(), ", ")
|
||||
<< sc::io::reset << '\n';
|
||||
}
|
||||
handle_deletions(tags);
|
||||
handle_changes(tags);
|
||||
handle_additions(tags);
|
||||
if (should_list) {
|
||||
cout << " after: " << sc::io::yellowf << sc::join(tags, ", ") << sc::io::reset << '\n';
|
||||
cout << " after: " << sc::io::yellowf
|
||||
<< sc::join(tags.begin(), tags.end(), ", ")
|
||||
<< sc::io::reset << '\n';
|
||||
}
|
||||
string joined {sc::join(tags, ",")};
|
||||
string joined {sc::join(tags.begin(), tags.end(), ",")};
|
||||
throw_if_min1(setxattr(path, tagname, joined.c_str(), joined.size(), 0));
|
||||
} else {
|
||||
cout << path << ": " << sc::io::yellowf << sc::join(tags, ", ") << sc::io::reset << '\n';
|
||||
cout << path << ": " << sc::io::yellowf
|
||||
<< sc::join(tags.begin(), tags.end(), ", ")
|
||||
<< sc::io::reset << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +136,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
case 'a': additions.insert(arg); break;
|
||||
case 'c': change = arg; break;
|
||||
case 'd': deletions.push_back(arg); break;
|
||||
case 'd': deletions.insert(arg); break;
|
||||
case 'h': print_help(); return EXIT_SUCCESS;
|
||||
case 'i': changes.emplace(change, arg); break;
|
||||
case 'l': should_list = true; break;
|
||||
|
Reference in New Issue
Block a user