portingtools/src/mappings.cpp

71 lines
1.9 KiB
C++
Raw Normal View History

2022-11-05 18:07:20 +01:00
#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];
2022-11-05 20:18:05 +01:00
if (this->renames.count(mapped)) {
BOOST_LOG_TRIVIAL(info)
<< "found rename " << mapped << " -> " << renames[mapped];
2022-11-05 18:07:20 +01:00
return renames[mapped];
2022-11-05 20:18:05 +01:00
}
2022-11-05 18:07:20 +01:00
return mapped;
}
} // namespace mappings