106 lines
3.1 KiB
C++
106 lines
3.1 KiB
C++
#include "mappings.hpp"
|
|
#include "csv.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/filesystem/exception.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <boost/system/detail/errc.hpp>
|
|
#include <fstream>
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
namespace mappings {
|
|
Mappings Mappings::load() {
|
|
std::map<std::string, Mapping> mappings;
|
|
std::map<std::string, std::string> renames;
|
|
|
|
// load mappings
|
|
fs::directory_iterator end_itr;
|
|
try {
|
|
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;
|
|
|
|
csv::CsvReader csv(itr->path().string());
|
|
|
|
std::vector<std::string> l;
|
|
while (csv >> l) {
|
|
if (l.size() < 2) {
|
|
BOOST_LOG_TRIVIAL(warning) << "found invalid mapping";
|
|
continue;
|
|
}
|
|
mappings[l[0]] = {l[0], l[1],
|
|
l.size() >= 4 ? std::optional(l[3]) : std::nullopt};
|
|
}
|
|
}
|
|
} catch (boost::filesystem::filesystem_error &e) {
|
|
if (e.code() == boost::system::errc::no_such_file_or_directory) {
|
|
throw std::runtime_error(
|
|
std::string("The mappings directory is missing!\n") + e.what());
|
|
}
|
|
throw;
|
|
}
|
|
|
|
if (fs::is_regular_file("renames.csv")) {
|
|
csv::CsvReader csv("renames.csv");
|
|
|
|
std::vector<std::string> l;
|
|
while (csv >> l) {
|
|
if (l.size() < 2) {
|
|
BOOST_LOG_TRIVIAL(warning) << "found invalid rename";
|
|
continue;
|
|
}
|
|
|
|
renames[l[0]] = l[1];
|
|
}
|
|
} else {
|
|
BOOST_LOG_TRIVIAL(warning) << "no renames found, skipping";
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(info) << "loaded " << mappings.size() << " mappings and "
|
|
<< renames.size() << " renames";
|
|
return {mappings, renames};
|
|
}
|
|
|
|
void showMapInfo(Mapping mapping, std::optional<std::string> rename) {
|
|
auto doc = mapping.doc.has_value() ? "\n\n\t" + *mapping.doc + "\n" : "";
|
|
if (rename.has_value()) {
|
|
BOOST_LOG_TRIVIAL(info)
|
|
<< "\nFound mapping:\n"
|
|
<< "\n\tOriginal\t" << mapping.orig << "\n\tRemapped\t" << mapping.name
|
|
<< "\n\tRenamed \t" << *rename << doc;
|
|
} else {
|
|
BOOST_LOG_TRIVIAL(info) << "\nFound mapping:\n"
|
|
<< "\n\tOriginal\t" << mapping.orig
|
|
<< "\n\tRemapped\t" << mapping.name << doc;
|
|
}
|
|
}
|
|
|
|
std::string Mappings::map(std::string inp) {
|
|
if (!this->m_mappings.count(inp)) {
|
|
BOOST_LOG_TRIVIAL(warning) << "unknown mapping '" << inp << "'";
|
|
return "<unknown>";
|
|
}
|
|
|
|
auto mapped = this->m_mappings[inp];
|
|
|
|
if (this->m_renames.count(mapped.name)) {
|
|
BOOST_LOG_TRIVIAL(info)
|
|
<< "found rename " << mapped.name << " -> " << m_renames[mapped.name];
|
|
showMapInfo(mapped, m_renames[mapped.name]);
|
|
return m_renames[mapped.name];
|
|
}
|
|
|
|
showMapInfo(mapped, std::nullopt);
|
|
return mapped.name;
|
|
}
|
|
} // namespace mappings
|