68 lines
1.8 KiB
C++
68 lines
1.8 KiB
C++
|
#include "mappings.hpp"
|
||
|
#include <boost/algorithm/string.hpp>
|
||
|
#include <boost/algorithm/string/classification.hpp>
|
||
|
#include <boost/algorithm/string/split.hpp>
|
||
|
#include <boost/filesystem.hpp>
|
||
|
#include <boost/filesystem/directory.hpp>
|
||
|
#include <boost/log/trivial.hpp>
|
||
|
#include <fstream>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
|
namespace fs = boost::filesystem;
|
||
|
|
||
|
namespace mappings {
|
||
|
void loadFile(std::string path, std::map<std::string, std::string> *map) {
|
||
|
std::ifstream file(path);
|
||
|
std::string line;
|
||
|
while (std::getline(file, line)) {
|
||
|
std::vector<std::string> splits;
|
||
|
boost::algorithm::split(splits, line, boost::is_any_of(",\t|"));
|
||
|
|
||
|
if (splits.size() < 2) {
|
||
|
BOOST_LOG_TRIVIAL(warning) << "found invalid mapping '" << line << "'";
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
(*map)[splits[0]] = splits[1];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Mappings Mappings::load() {
|
||
|
std::map<std::string, std::string> mappings;
|
||
|
std::map<std::string, std::string> renames;
|
||
|
|
||
|
// load mappings
|
||
|
fs::directory_iterator end_itr;
|
||
|
for (fs::directory_iterator itr("mappings"); itr != end_itr; ++itr) {
|
||
|
if (fs::is_directory(itr->status()) ||
|
||
|
!boost::algorithm::ends_with(itr->path().string(), ".csv"))
|
||
|
continue;
|
||
|
|
||
|
loadFile(itr->path().string(), &mappings);
|
||
|
}
|
||
|
|
||
|
if (fs::is_regular_file("renames.csv")) {
|
||
|
loadFile("renames.csv", &renames);
|
||
|
}
|
||
|
|
||
|
BOOST_LOG_TRIVIAL(info) << "loaded " << mappings.size() << " mappings and "
|
||
|
<< renames.size() << " renames";
|
||
|
return {mappings, renames};
|
||
|
}
|
||
|
|
||
|
std::string Mappings::map(std::string inp) {
|
||
|
if (!this->mappings.count(inp)) {
|
||
|
BOOST_LOG_TRIVIAL(warning) << "unknown mapping '" << inp << "'";
|
||
|
return "<unknown>";
|
||
|
}
|
||
|
|
||
|
auto mapped = this->mappings[inp];
|
||
|
|
||
|
if (this->renames.count(mapped))
|
||
|
return renames[mapped];
|
||
|
|
||
|
return mapped;
|
||
|
}
|
||
|
} // namespace mappings
|