diff --git a/.astylerc b/.astylerc new file mode 100644 index 0000000..e9ec1bc --- /dev/null +++ b/.astylerc @@ -0,0 +1,14 @@ +--style=attach +--indent-switches +--indent-namespaces +--indent-after-parens +--indent-col1-comments +--break-blocks +--pad-oper +--pad-header +--unpad-paren +--break-one-line-headers +--convert-tabs +--max-code-length=90 +--break-after-logical +--lineend=linux diff --git a/src/ansi.hpp b/src/ansi.hpp index 0a80425..b4625a0 100644 --- a/src/ansi.hpp +++ b/src/ansi.hpp @@ -1,9 +1,9 @@ #pragma once namespace ansi { -const char *reset = "\033[0m"; + const char *reset = "\033[0m"; -const char *cyan = "\033[0;36m"; -const char *green = "\033[0;32m"; -const char *yellow = "\033[0;33m"; + const char *cyan = "\033[0;36m"; + const char *green = "\033[0;32m"; + const char *yellow = "\033[0;33m"; } // namespace ansi diff --git a/src/client.cpp b/src/client.cpp index 8473b29..98c8313 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -9,45 +9,46 @@ #include namespace client { -void sendMsg(int msqid, data::RequestMapMsg *msg) { - if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) { - throw std::runtime_error(std::string("send error ") + strerror(errno)); - } -} + void sendMsg(int msqid, data::RequestMapMsg *msg) { + if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) { + throw std::runtime_error(std::string("send error ") + strerror(errno)); + } + } -std::string recvMsg(int msqid) { - data::ResponseMapMsg msg; - if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long), - data::clientbound_msg, 0) < 0) { - throw std::runtime_error(std::string("receive error ") + strerror(errno)); - } + std::string recvMsg(int msqid) { + data::ResponseMapMsg msg; - return std::string(&msg.data[0], msg.datalen); -} + if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long), + data::clientbound_msg, 0) < 0) { + throw std::runtime_error(std::string("receive error ") + strerror(errno)); + } -void map(key_t key) { - std::string inp; - std::cin >> inp; + return std::string(&msg.data[0], msg.datalen); + } - auto msqid = msgget(key, 0664); + void map(key_t key) { + std::string inp; + std::cin >> inp; - if (msqid < 0) { - throw std::runtime_error("msgget fail. is the server running?"); - } + auto msqid = msgget(key, 0664); - data::RequestMapMsg req; - req.msgtype = data::serverbound_msg; - req.datalen = inp.size(); - std::copy(inp.begin(), inp.end(), req.data); + if (msqid < 0) { + throw std::runtime_error("msgget fail. is the server running?"); + } - sendMsg(msqid, &req); - std::cout << recvMsg(msqid) << std::endl; -} + data::RequestMapMsg req; + req.msgtype = data::serverbound_msg; + req.datalen = inp.size(); + std::copy(inp.begin(), inp.end(), req.data); -void resolveResourceLoc(key_t key) { - std::string inp; - std::cin >> inp; + sendMsg(msqid, &req); + std::cout << recvMsg(msqid) << std::endl; + } - std::cout << resourceloc::resolve(inp) << std::endl; -} + void resolveResourceLoc(key_t key) { + std::string inp; + std::cin >> inp; + + std::cout << resourceloc::resolve(inp) << std::endl; + } } // namespace client diff --git a/src/client.hpp b/src/client.hpp index 0c33382..6751ee8 100644 --- a/src/client.hpp +++ b/src/client.hpp @@ -3,6 +3,6 @@ #include namespace client { -void map(key_t key); -void resolveResourceLoc(key_t key); + void map(key_t key); + void resolveResourceLoc(key_t key); } // namespace client diff --git a/src/csv.cpp b/src/csv.cpp index a13a16a..33da02f 100644 --- a/src/csv.cpp +++ b/src/csv.cpp @@ -4,70 +4,82 @@ #include namespace csv { -enum class CSVState { UnquotedField, QuotedField, QuotedQuote }; + enum class CSVState { UnquotedField, QuotedField, QuotedQuote }; -// this function has been made in collaboration with -// StackOverflow https://stackoverflow.com/a/30338543 -void readCSVRow(const std::string &row, std::vector &fields) { - fields.push_back(""); - CSVState state = CSVState::UnquotedField; - size_t i = 0; // index of the current field - for (char c : row) { - switch (state) { - case CSVState::UnquotedField: - switch (c) { - case ',': // end of field + // this function has been made in collaboration with + // StackOverflow https://stackoverflow.com/a/30338543 + void readCSVRow(const std::string &row, std::vector &fields) { fields.push_back(""); - i++; - break; - case '"': - state = CSVState::QuotedField; - break; - default: - fields[i].push_back(c); - break; - } - break; - case CSVState::QuotedField: - switch (c) { - case '"': - state = CSVState::QuotedQuote; - break; - default: - fields[i].push_back(c); - break; - } - break; - case CSVState::QuotedQuote: - switch (c) { - case ',': // , after closing quote - fields.push_back(""); - i++; - state = CSVState::UnquotedField; - break; - case '"': // "" -> " - fields[i].push_back('"'); - state = CSVState::QuotedField; - break; - default: // end of quote - state = CSVState::UnquotedField; - break; - } - break; + CSVState state = CSVState::UnquotedField; + size_t i = 0; // index of the current field + + for (char c : row) { + switch (state) { + case CSVState::UnquotedField: + switch (c) { + case ',': // end of field + fields.push_back(""); + i++; + break; + + case '"': + state = CSVState::QuotedField; + break; + + default: + fields[i].push_back(c); + break; + } + + break; + + case CSVState::QuotedField: + switch (c) { + case '"': + state = CSVState::QuotedQuote; + break; + + default: + fields[i].push_back(c); + break; + } + + break; + + case CSVState::QuotedQuote: + switch (c) { + case ',': // , after closing quote + fields.push_back(""); + i++; + state = CSVState::UnquotedField; + break; + + case '"': // "" -> " + fields[i].push_back('"'); + state = CSVState::QuotedField; + break; + + default: // end of quote + state = CSVState::UnquotedField; + break; + } + + break; + } + } } - } -} -bool CsvReader::operator>>(std::vector &line) { - line.clear(); - if (!m_stream) { - return false; - } + bool CsvReader::operator>>(std::vector &line) { + line.clear(); - std::string row; - std::getline(m_stream, row); + if (!m_stream) { + return false; + } - readCSVRow(row, line); + std::string row; + std::getline(m_stream, row); - return !!m_stream; -} + readCSVRow(row, line); + + return !!m_stream; + } } // namespace csv diff --git a/src/csv.hpp b/src/csv.hpp index b1e042f..e23bd76 100644 --- a/src/csv.hpp +++ b/src/csv.hpp @@ -3,14 +3,14 @@ #include #include namespace csv { -class CsvReader { -private: - std::ifstream m_stream; + class CsvReader { + private: + std::ifstream m_stream; -public: - CsvReader(auto stream) : m_stream(stream){}; - ~CsvReader() = default; + public: + CsvReader(auto stream) : m_stream(stream) {}; + ~CsvReader() = default; - bool operator>>(std::vector &line); -}; + bool operator>>(std::vector &line); + }; } // namespace csv diff --git a/src/data.cpp b/src/data.cpp index 847cafa..9d83b25 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -5,13 +5,13 @@ #include namespace data { -key_t getIpcKeyFromExeName(char *argv0) { - auto k = ftok(argv0, 'X'); + key_t getIpcKeyFromExeName(char *argv0) { + auto k = ftok(argv0, 'X'); - if (k < 0) { - throw std::runtime_error("failed to get IPC key"); - } + if (k < 0) { + throw std::runtime_error("failed to get IPC key"); + } - return k; -} + return k; + } } // namespace data diff --git a/src/data.hpp b/src/data.hpp index d21d491..14c6a9c 100644 --- a/src/data.hpp +++ b/src/data.hpp @@ -3,22 +3,22 @@ #include namespace data { -const int serverbound_msg = 1; -const int clientbound_msg = 2; + const int serverbound_msg = 1; + const int clientbound_msg = 2; -key_t getIpcKeyFromExeName(char *argv0); + key_t getIpcKeyFromExeName(char *argv0); -struct RequestMapMsg { - long msgtype; + struct RequestMapMsg { + long msgtype; - unsigned int datalen; - char data[128]; -}; + unsigned int datalen; + char data[128]; + }; -struct ResponseMapMsg { - long msgtype; + struct ResponseMapMsg { + long msgtype; - unsigned int datalen; - char data[128]; -}; + unsigned int datalen; + char data[128]; + }; } // namespace data diff --git a/src/main.cpp b/src/main.cpp index 512dc71..5f5c3f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,27 +6,27 @@ #include void main2(int argc, char *argv[]) { - if (argc < 2) { - throw std::runtime_error("not enough arguments!"); - } + if (argc < 2) { + throw std::runtime_error("not enough arguments!"); + } - if (strcmp(argv[1], "serve") == 0) { - server::run(data::getIpcKeyFromExeName(argv[0])); - } else if (strcmp(argv[1], "map") == 0) { - client::map(data::getIpcKeyFromExeName(argv[0])); - } else if (strcmp(argv[1], "resourceloc") == 0) { - client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0])); - } else { - throw std::runtime_error("unknown command!"); - } + if (strcmp(argv[1], "serve") == 0) { + server::run(data::getIpcKeyFromExeName(argv[0])); + } else if (strcmp(argv[1], "map") == 0) { + client::map(data::getIpcKeyFromExeName(argv[0])); + } else if (strcmp(argv[1], "resourceloc") == 0) { + client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0])); + } else { + throw std::runtime_error("unknown command!"); + } } int main(int argc, char *argv[]) { - try { - main2(argc, argv); - return 0; - } catch (const std::exception &e) { - BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what(); - return 1; - } + try { + main2(argc, argv); + return 0; + } catch (const std::exception &e) { + BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what(); + return 1; + } } diff --git a/src/mappings.cpp b/src/mappings.cpp index 4464af0..ebb18a3 100644 --- a/src/mappings.cpp +++ b/src/mappings.cpp @@ -18,95 +18,102 @@ namespace fs = boost::filesystem; namespace mappings { -Mappings Mappings::load() { - std::map mappings; - std::map renames; + Mappings Mappings::load() { + std::map mappings; + std::map 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; + // load mappings + fs::directory_iterator end_itr; - csv::CsvReader csv(itr->path().string()); + 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; - std::vector l; - while (csv >> l) { - if (l.size() < 2) { - BOOST_LOG_TRIVIAL(warning) << "found invalid mapping"; - continue; + csv::CsvReader csv(itr->path().string()); + + std::vector 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; } - mappings[l[0]] = {l[0], l[1], - l.size() >= 4 ? std::optional(l[3]) : std::nullopt}; - } + + if (fs::is_regular_file("renames.csv")) { + csv::CsvReader csv("renames.csv"); + + std::vector 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}; } - } 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()); + + void showMapInfo(Mapping mapping, std::optional rename) { + auto doc = mapping.doc.has_value() + ? std::string(ansi::yellow) + "\n\n\t" + *mapping.doc + "\n" + : ""; + + if (rename.has_value()) { + BOOST_LOG_TRIVIAL(info) + << "\nFound mapping:\n" + << ansi::cyan << "\n\tOriginal\t" << ansi::green << mapping.orig + << ansi::cyan << "\n\tRemapped\t" << ansi::green << mapping.name + << ansi::cyan << "\n\tRenamed \t" << ansi::green << *rename << doc + << ansi::reset; + } else { + BOOST_LOG_TRIVIAL(info) + << "\nFound mapping:\n" + << ansi::cyan << "\n\tOriginal\t" << ansi::green << mapping.orig + << ansi::cyan << "\n\tRemapped\t" << ansi::green << mapping.name << doc + << ansi::reset; + } } - throw; - } - if (fs::is_regular_file("renames.csv")) { - csv::CsvReader csv("renames.csv"); + std::string Mappings::map(std::string inp) { + if (!this->m_mappings.count(inp)) { + BOOST_LOG_TRIVIAL(warning) << "unknown mapping '" << inp << "'"; + return ""; + } - std::vector l; - while (csv >> l) { - if (l.size() < 2) { - BOOST_LOG_TRIVIAL(warning) << "found invalid rename"; - continue; - } + auto mapped = this->m_mappings[inp]; - renames[l[0]] = l[1]; + 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; } - } 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 rename) { - auto doc = mapping.doc.has_value() - ? std::string(ansi::yellow) + "\n\n\t" + *mapping.doc + "\n" - : ""; - if (rename.has_value()) { - BOOST_LOG_TRIVIAL(info) - << "\nFound mapping:\n" - << ansi::cyan << "\n\tOriginal\t" << ansi::green << mapping.orig - << ansi::cyan << "\n\tRemapped\t" << ansi::green << mapping.name - << ansi::cyan << "\n\tRenamed \t" << ansi::green << *rename << doc - << ansi::reset; - } else { - BOOST_LOG_TRIVIAL(info) - << "\nFound mapping:\n" - << ansi::cyan << "\n\tOriginal\t" << ansi::green << mapping.orig - << ansi::cyan << "\n\tRemapped\t" << ansi::green << mapping.name << doc - << ansi::reset; - } -} - -std::string Mappings::map(std::string inp) { - if (!this->m_mappings.count(inp)) { - BOOST_LOG_TRIVIAL(warning) << "unknown mapping '" << inp << "'"; - return ""; - } - - 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 diff --git a/src/mappings.hpp b/src/mappings.hpp index b873cec..d59501e 100644 --- a/src/mappings.hpp +++ b/src/mappings.hpp @@ -4,24 +4,24 @@ #include namespace mappings { -struct Mapping { - std::string orig; - std::string name; - std::optional doc; -}; + struct Mapping { + std::string orig; + std::string name; + std::optional doc; + }; -class Mappings { - std::map m_mappings; - std::map m_renames; + class Mappings { + std::map m_mappings; + std::map m_renames; -public: - Mappings(std::map mappings, - std::map renames) { - this->m_mappings = mappings; - this->m_renames = renames; - } - ~Mappings() = default; - static Mappings load(); - std::string map(std::string inp); -}; + public: + Mappings(std::map mappings, + std::map renames) { + this->m_mappings = mappings; + this->m_renames = renames; + } + ~Mappings() = default; + static Mappings load(); + std::string map(std::string inp); + }; } // namespace mappings diff --git a/src/resourceloc.cpp b/src/resourceloc.cpp index f9dc416..a3deaf1 100644 --- a/src/resourceloc.cpp +++ b/src/resourceloc.cpp @@ -9,26 +9,28 @@ #include namespace resourceloc { -std::string resolve(std::string inp) { - boost::algorithm::trim_if(inp, boost::is_any_of(" \n\r\"")); + std::string resolve(std::string inp) { + boost::algorithm::trim_if(inp, boost::is_any_of(" \n\r\"")); - std::vector components; - boost::algorithm::split(components, inp, boost::is_any_of("/")); + std::vector components; + boost::algorithm::split(components, inp, boost::is_any_of("/")); - if (components.size() < 3 || components[0] != "" || components[1] != "mods") { - BOOST_LOG_TRIVIAL(warning) << "invalid resourceloc"; - return ""; - } + if (components.size() < 3 || components[0] != "" || components[1] != "mods") { + BOOST_LOG_TRIVIAL(warning) << "invalid resourceloc"; + return ""; + } - std::string path_component; - for (unsigned int i = 3; i < components.size(); i++) { - path_component += components[i]; - if (i != components.size() - 1) { - path_component += "/"; + std::string path_component; + + for (unsigned int i = 3; i < components.size(); i++) { + path_component += components[i]; + + if (i != components.size() - 1) { + path_component += "/"; + } + } + + return boost::str(boost::format("new ResourceLocation(\"%1%\", \"%2%\")") % + components[2] % path_component); } - } - - return boost::str(boost::format("new ResourceLocation(\"%1%\", \"%2%\")") % - components[2] % path_component); -} } // namespace resourceloc diff --git a/src/resourceloc.hpp b/src/resourceloc.hpp index b86c13a..cd7133f 100644 --- a/src/resourceloc.hpp +++ b/src/resourceloc.hpp @@ -1,5 +1,5 @@ #pragma once #include namespace resourceloc { -std::string resolve(std::string inp); + std::string resolve(std::string inp); } diff --git a/src/server.cpp b/src/server.cpp index bcf7d74..1414fed 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -11,41 +11,42 @@ #include namespace server { -void sendToClient(int msqid, std::string msg) { - data::ResponseMapMsg pkt; - pkt.msgtype = data::clientbound_msg; - pkt.datalen = msg.size(); - std::copy(msg.begin(), msg.end(), pkt.data); + void sendToClient(int msqid, std::string msg) { + data::ResponseMapMsg pkt; + pkt.msgtype = data::clientbound_msg; + pkt.datalen = msg.size(); + std::copy(msg.begin(), msg.end(), pkt.data); - if (msgsnd(msqid, &pkt, sizeof(data::ResponseMapMsg) - sizeof(long), 0) < 0) { - BOOST_LOG_TRIVIAL(error) << "failed to send response " << strerror(errno); - } -} - -void run(key_t key) { - BOOST_LOG_TRIVIAL(info) << "starting server"; - - auto msqid = msgget(key, 0664 | IPC_CREAT); - - if (msqid < 0) { - throw std::runtime_error("msgget fail"); - } - - BOOST_LOG_TRIVIAL(info) << "msqid " << msqid; - - auto mappings = mappings::Mappings::load(); - - data::RequestMapMsg msg; - for (;;) { - if (msgrcv(msqid, &msg, sizeof(data::RequestMapMsg) - sizeof(long), - data::serverbound_msg, 0) < 0) { - BOOST_LOG_TRIVIAL(error) << "receive error " << strerror(errno); - continue; + if (msgsnd(msqid, &pkt, sizeof(data::ResponseMapMsg) - sizeof(long), 0) < 0) { + BOOST_LOG_TRIVIAL(error) << "failed to send response " << strerror(errno); + } } - std::string s(msg.data, msg.datalen); - BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'"; - sendToClient(msqid, mappings.map(s)); - } -} + void run(key_t key) { + BOOST_LOG_TRIVIAL(info) << "starting server"; + + auto msqid = msgget(key, 0664 | IPC_CREAT); + + if (msqid < 0) { + throw std::runtime_error("msgget fail"); + } + + BOOST_LOG_TRIVIAL(info) << "msqid " << msqid; + + auto mappings = mappings::Mappings::load(); + + data::RequestMapMsg msg; + + for (;;) { + if (msgrcv(msqid, &msg, sizeof(data::RequestMapMsg) - sizeof(long), + data::serverbound_msg, 0) < 0) { + BOOST_LOG_TRIVIAL(error) << "receive error " << strerror(errno); + continue; + } + + std::string s(msg.data, msg.datalen); + BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'"; + sendToClient(msqid, mappings.map(s)); + } + } } // namespace server diff --git a/src/server.hpp b/src/server.hpp index 380dd7f..2094149 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -3,5 +3,5 @@ #include namespace server { -void run(key_t key); + void run(key_t key); }