chore: format code
This commit is contained in:
parent
8d2041e946
commit
10adb7a9b2
14
.astylerc
Normal file
14
.astylerc
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -9,23 +9,24 @@
|
|||
#include <sys/msg.h>
|
||||
|
||||
namespace client {
|
||||
void sendMsg(int msqid, data::RequestMapMsg *msg) {
|
||||
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) {
|
||||
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));
|
||||
}
|
||||
|
||||
return std::string(&msg.data[0], msg.datalen);
|
||||
}
|
||||
}
|
||||
|
||||
void map(key_t key) {
|
||||
void map(key_t key) {
|
||||
std::string inp;
|
||||
std::cin >> inp;
|
||||
|
||||
|
@ -42,12 +43,12 @@ void map(key_t key) {
|
|||
|
||||
sendMsg(msqid, &req);
|
||||
std::cout << recvMsg(msqid) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void resolveResourceLoc(key_t key) {
|
||||
void resolveResourceLoc(key_t key) {
|
||||
std::string inp;
|
||||
std::cin >> inp;
|
||||
|
||||
std::cout << resourceloc::resolve(inp) << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace client
|
||||
|
|
|
@ -3,6 +3,6 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
namespace client {
|
||||
void map(key_t key);
|
||||
void resolveResourceLoc(key_t key);
|
||||
void map(key_t key);
|
||||
void resolveResourceLoc(key_t key);
|
||||
} // namespace client
|
||||
|
|
26
src/csv.cpp
26
src/csv.cpp
|
@ -4,14 +4,15 @@
|
|||
#include <vector>
|
||||
|
||||
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<std::string> &fields) {
|
||||
// this function has been made in collaboration with
|
||||
// StackOverflow https://stackoverflow.com/a/30338543
|
||||
void readCSVRow(const std::string &row, std::vector<std::string> &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:
|
||||
|
@ -20,24 +21,31 @@ void readCSVRow(const std::string &row, std::vector<std::string> &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
|
||||
|
@ -45,20 +53,24 @@ void readCSVRow(const std::string &row, std::vector<std::string> &fields) {
|
|||
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<std::string> &line) {
|
||||
}
|
||||
bool CsvReader::operator>>(std::vector<std::string> &line) {
|
||||
line.clear();
|
||||
|
||||
if (!m_stream) {
|
||||
return false;
|
||||
}
|
||||
|
@ -69,5 +81,5 @@ bool CsvReader::operator>>(std::vector<std::string> &line) {
|
|||
readCSVRow(row, line);
|
||||
|
||||
return !!m_stream;
|
||||
}
|
||||
}
|
||||
} // namespace csv
|
||||
|
|
10
src/csv.hpp
10
src/csv.hpp
|
@ -3,14 +3,14 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
namespace csv {
|
||||
class CsvReader {
|
||||
private:
|
||||
class CsvReader {
|
||||
private:
|
||||
std::ifstream m_stream;
|
||||
|
||||
public:
|
||||
CsvReader(auto stream) : m_stream(stream){};
|
||||
public:
|
||||
CsvReader(auto stream) : m_stream(stream) {};
|
||||
~CsvReader() = default;
|
||||
|
||||
bool operator>>(std::vector<std::string> &line);
|
||||
};
|
||||
};
|
||||
} // namespace csv
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <sys/ipc.h>
|
||||
|
||||
namespace data {
|
||||
key_t getIpcKeyFromExeName(char *argv0) {
|
||||
key_t getIpcKeyFromExeName(char *argv0) {
|
||||
auto k = ftok(argv0, 'X');
|
||||
|
||||
if (k < 0) {
|
||||
|
@ -13,5 +13,5 @@ key_t getIpcKeyFromExeName(char *argv0) {
|
|||
}
|
||||
|
||||
return k;
|
||||
}
|
||||
}
|
||||
} // namespace data
|
||||
|
|
14
src/data.hpp
14
src/data.hpp
|
@ -3,22 +3,22 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
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 {
|
||||
struct RequestMapMsg {
|
||||
long msgtype;
|
||||
|
||||
unsigned int datalen;
|
||||
char data[128];
|
||||
};
|
||||
};
|
||||
|
||||
struct ResponseMapMsg {
|
||||
struct ResponseMapMsg {
|
||||
long msgtype;
|
||||
|
||||
unsigned int datalen;
|
||||
char data[128];
|
||||
};
|
||||
};
|
||||
} // namespace data
|
||||
|
|
|
@ -18,12 +18,13 @@
|
|||
namespace fs = boost::filesystem;
|
||||
|
||||
namespace mappings {
|
||||
Mappings Mappings::load() {
|
||||
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()) ||
|
||||
|
@ -33,13 +34,16 @@ Mappings Mappings::load() {
|
|||
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};
|
||||
l.size() >= 4 ? std::optional(l[3]) : std::nullopt
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch (boost::filesystem::filesystem_error &e) {
|
||||
|
@ -47,6 +51,7 @@ Mappings Mappings::load() {
|
|||
throw std::runtime_error(
|
||||
std::string("The mappings directory is missing!\n") + e.what());
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
|
@ -54,6 +59,7 @@ Mappings Mappings::load() {
|
|||
csv::CsvReader csv("renames.csv");
|
||||
|
||||
std::vector<std::string> l;
|
||||
|
||||
while (csv >> l) {
|
||||
if (l.size() < 2) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "found invalid rename";
|
||||
|
@ -69,12 +75,13 @@ Mappings Mappings::load() {
|
|||
BOOST_LOG_TRIVIAL(info) << "loaded " << mappings.size() << " mappings and "
|
||||
<< renames.size() << " renames";
|
||||
return {mappings, renames};
|
||||
}
|
||||
}
|
||||
|
||||
void showMapInfo(Mapping mapping, std::optional<std::string> rename) {
|
||||
void showMapInfo(Mapping mapping, std::optional<std::string> 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"
|
||||
|
@ -89,9 +96,9 @@ void showMapInfo(Mapping mapping, std::optional<std::string> rename) {
|
|||
<< ansi::cyan << "\n\tRemapped\t" << ansi::green << mapping.name << doc
|
||||
<< ansi::reset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string Mappings::map(std::string inp) {
|
||||
std::string Mappings::map(std::string inp) {
|
||||
if (!this->m_mappings.count(inp)) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "unknown mapping '" << inp << "'";
|
||||
return "<unknown>";
|
||||
|
@ -108,5 +115,5 @@ std::string Mappings::map(std::string inp) {
|
|||
|
||||
showMapInfo(mapped, std::nullopt);
|
||||
return mapped.name;
|
||||
}
|
||||
}
|
||||
} // namespace mappings
|
||||
|
|
|
@ -4,17 +4,17 @@
|
|||
#include <string>
|
||||
|
||||
namespace mappings {
|
||||
struct Mapping {
|
||||
struct Mapping {
|
||||
std::string orig;
|
||||
std::string name;
|
||||
std::optional<std::string> doc;
|
||||
};
|
||||
};
|
||||
|
||||
class Mappings {
|
||||
class Mappings {
|
||||
std::map<std::string, Mapping> m_mappings;
|
||||
std::map<std::string, std::string> m_renames;
|
||||
|
||||
public:
|
||||
public:
|
||||
Mappings(std::map<std::string, Mapping> mappings,
|
||||
std::map<std::string, std::string> renames) {
|
||||
this->m_mappings = mappings;
|
||||
|
@ -23,5 +23,5 @@ public:
|
|||
~Mappings() = default;
|
||||
static Mappings load();
|
||||
std::string map(std::string inp);
|
||||
};
|
||||
};
|
||||
} // namespace mappings
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include <vector>
|
||||
|
||||
namespace resourceloc {
|
||||
std::string resolve(std::string inp) {
|
||||
std::string resolve(std::string inp) {
|
||||
boost::algorithm::trim_if(inp, boost::is_any_of(" \n\r\""));
|
||||
|
||||
std::vector<std::string> components;
|
||||
|
@ -21,8 +21,10 @@ std::string resolve(std::string inp) {
|
|||
}
|
||||
|
||||
std::string path_component;
|
||||
|
||||
for (unsigned int i = 3; i < components.size(); i++) {
|
||||
path_component += components[i];
|
||||
|
||||
if (i != components.size() - 1) {
|
||||
path_component += "/";
|
||||
}
|
||||
|
@ -30,5 +32,5 @@ std::string resolve(std::string inp) {
|
|||
|
||||
return boost::str(boost::format("new ResourceLocation(\"%1%\", \"%2%\")") %
|
||||
components[2] % path_component);
|
||||
}
|
||||
}
|
||||
} // namespace resourceloc
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
namespace resourceloc {
|
||||
std::string resolve(std::string inp);
|
||||
std::string resolve(std::string inp);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <sys/msg.h>
|
||||
|
||||
namespace server {
|
||||
void sendToClient(int msqid, std::string msg) {
|
||||
void sendToClient(int msqid, std::string msg) {
|
||||
data::ResponseMapMsg pkt;
|
||||
pkt.msgtype = data::clientbound_msg;
|
||||
pkt.datalen = msg.size();
|
||||
|
@ -20,9 +20,9 @@ void sendToClient(int msqid, std::string msg) {
|
|||
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) {
|
||||
void run(key_t key) {
|
||||
BOOST_LOG_TRIVIAL(info) << "starting server";
|
||||
|
||||
auto msqid = msgget(key, 0664 | IPC_CREAT);
|
||||
|
@ -36,6 +36,7 @@ void run(key_t key) {
|
|||
auto mappings = mappings::Mappings::load();
|
||||
|
||||
data::RequestMapMsg msg;
|
||||
|
||||
for (;;) {
|
||||
if (msgrcv(msqid, &msg, sizeof(data::RequestMapMsg) - sizeof(long),
|
||||
data::serverbound_msg, 0) < 0) {
|
||||
|
@ -47,5 +48,5 @@ void run(key_t key) {
|
|||
BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'";
|
||||
sendToClient(msqid, mappings.map(s));
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace server
|
||||
|
|
|
@ -3,5 +3,5 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
namespace server {
|
||||
void run(key_t key);
|
||||
void run(key_t key);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue