chore: format code
This commit is contained in:
parent
8d2041e946
commit
b76daa2927
15 changed files with 501 additions and 317 deletions
130
.clang-format
Normal file
130
.clang-format
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
---
|
||||||
|
AccessModifierOffset: 0
|
||||||
|
AlignAfterOpenBracket: BlockIndent
|
||||||
|
AlignArrayOfStructures: None
|
||||||
|
AlignConsecutiveAssignments: None
|
||||||
|
AlignConsecutiveMacros: None
|
||||||
|
AlignConsecutiveBitFields: None
|
||||||
|
AlignConsecutiveDeclarations: None
|
||||||
|
AlignEscapedNewlines: DontAlign
|
||||||
|
AlignOperands: DontAlign
|
||||||
|
AlignTrailingComments: false
|
||||||
|
AllowAllArgumentsOnNextLine: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: Empty
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortEnumsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: true
|
||||||
|
AlwaysBreakTemplateDeclarations: MultiLine
|
||||||
|
AttributeMacros: []
|
||||||
|
BinPackArguments: false
|
||||||
|
BinPackParameters: false
|
||||||
|
BitFieldColonSpacing: After
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: Never
|
||||||
|
AfterEnum: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
BeforeLambdaBody: false
|
||||||
|
BeforeWhile: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: false
|
||||||
|
SplitEmptyRecord: false
|
||||||
|
SplitEmptyNamespace: false
|
||||||
|
BreakAfterJavaFieldAnnotations: true
|
||||||
|
#BreakArrays: false
|
||||||
|
BreakBeforeBinaryOperators: All
|
||||||
|
BreakBeforeBraces: Custom
|
||||||
|
BreakBeforeConceptDeclarations: true
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: AfterColon
|
||||||
|
BreakInheritanceList: AfterColon
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 90
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: false
|
||||||
|
DeriveLineEnding: false
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
DisableFormat: false # wtf
|
||||||
|
EmptyLineAfterAccessModifier: Never
|
||||||
|
EmptyLineBeforeAccessModifier: Always
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: false
|
||||||
|
ForEachMacros: ["BOOST_FOREACH"]
|
||||||
|
IfMacros: []
|
||||||
|
IncludeBlocks: Regroup
|
||||||
|
IndentAccessModifiers: false
|
||||||
|
IndentCaseBlocks: false
|
||||||
|
IndentCaseLabels: true
|
||||||
|
IndentExternBlock: Indent
|
||||||
|
IndentGotoLabels: true
|
||||||
|
IndentPPDirectives: BeforeHash
|
||||||
|
#IndentRequiresClause: false
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
#InsertBraces: false
|
||||||
|
InsertTrailingCommas: Wrapped
|
||||||
|
JavaImportGroups: ["java"]
|
||||||
|
JavaScriptQuotes: Double
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||||
|
LambdaBodyIndentation: OuterScope
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: All
|
||||||
|
PackConstructorInitializers: NextLine
|
||||||
|
PointerAlignment: Left
|
||||||
|
QualifierAlignment: Left
|
||||||
|
ReferenceAlignment: Left
|
||||||
|
ReflowComments: true
|
||||||
|
#RemoveSemicolon: true
|
||||||
|
#RequiresClausePosition: OwnLine
|
||||||
|
#RequiresExpressionIndentation: OuterScope
|
||||||
|
SeparateDefinitionBlocks: Always
|
||||||
|
SortIncludes: CaseInsensitive
|
||||||
|
SortJavaStaticImport: Before
|
||||||
|
SortUsingDeclarations: true
|
||||||
|
SpaceAfterCStyleCast: true
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: false
|
||||||
|
SpaceAroundPointerQualifiers: After
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCaseColon: false
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: false
|
||||||
|
SpaceBeforeInheritanceColon: false
|
||||||
|
SpaceBeforeParens: ControlStatementsExceptControlMacros
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
SpaceInEmptyBlock: false
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesInAngles: Never
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
SpacesInContainerLiterals: false
|
||||||
|
SpacesInLineCommentPrefix:
|
||||||
|
Minimum: 0
|
||||||
|
Maximum: -1
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: c++20
|
||||||
|
StatementAttributeLikeMacros: []
|
||||||
|
StatementMacros: []
|
||||||
|
TabWidth: 4
|
||||||
|
TypenameMacros: []
|
||||||
|
UseCRLF: false # wtf
|
||||||
|
UseTab: Never
|
||||||
|
WhitespaceSensitiveMacros: ["BOOST_PP_STRINGSIZE"]
|
10
src/ansi.hpp
10
src/ansi.hpp
|
@ -1,9 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace ansi {
|
namespace ansi {
|
||||||
const char *reset = "\033[0m";
|
inline const char* reset = "\033[0m";
|
||||||
|
|
||||||
const char *cyan = "\033[0;36m";
|
inline const char* cyan = "\033[0;36m";
|
||||||
const char *green = "\033[0;32m";
|
inline const char* green = "\033[0;32m";
|
||||||
const char *yellow = "\033[0;33m";
|
inline const char* yellow = "\033[0;33m";
|
||||||
} // namespace ansi
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "data.hpp"
|
#include "data.hpp"
|
||||||
#include "resourceloc.hpp"
|
#include "resourceloc.hpp"
|
||||||
|
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
@ -9,45 +10,52 @@
|
||||||
#include <sys/msg.h>
|
#include <sys/msg.h>
|
||||||
|
|
||||||
namespace client {
|
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) {
|
if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
|
||||||
throw std::runtime_error(std::string("send error ") + strerror(errno));
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::string(&msg.data[0], msg.datalen);
|
||||||
|
}
|
||||||
|
|
||||||
|
void map(key_t key) {
|
||||||
|
std::string inp;
|
||||||
|
std::cin >> inp;
|
||||||
|
|
||||||
|
auto msqid = msgget(key, 0664);
|
||||||
|
|
||||||
|
if (msqid < 0) {
|
||||||
|
throw std::runtime_error("msgget fail. is the server running?");
|
||||||
|
}
|
||||||
|
|
||||||
|
data::RequestMapMsg req;
|
||||||
|
req.msgtype = data::serverbound_msg;
|
||||||
|
req.datalen = inp.size();
|
||||||
|
std::copy(inp.begin(), inp.end(), req.data);
|
||||||
|
|
||||||
|
sendMsg(msqid, &req);
|
||||||
|
std::cout << recvMsg(msqid) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void resolveResourceLoc(key_t key) {
|
||||||
|
std::string inp;
|
||||||
|
std::cin >> inp;
|
||||||
|
|
||||||
|
std::cout << resourceloc::resolve(inp) << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|
||||||
std::string inp;
|
|
||||||
std::cin >> inp;
|
|
||||||
|
|
||||||
auto msqid = msgget(key, 0664);
|
|
||||||
|
|
||||||
if (msqid < 0) {
|
|
||||||
throw std::runtime_error("msgget fail. is the server running?");
|
|
||||||
}
|
|
||||||
|
|
||||||
data::RequestMapMsg req;
|
|
||||||
req.msgtype = data::serverbound_msg;
|
|
||||||
req.datalen = inp.size();
|
|
||||||
std::copy(inp.begin(), inp.end(), req.data);
|
|
||||||
|
|
||||||
sendMsg(msqid, &req);
|
|
||||||
std::cout << recvMsg(msqid) << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace client {
|
namespace client {
|
||||||
void map(key_t key);
|
void map(key_t key);
|
||||||
void resolveResourceLoc(key_t key);
|
void resolveResourceLoc(key_t key);
|
||||||
} // namespace client
|
}
|
||||||
|
|
144
src/csv.cpp
144
src/csv.cpp
|
@ -1,73 +1,91 @@
|
||||||
#include "csv.hpp"
|
#include "csv.hpp"
|
||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace csv {
|
namespace csv {
|
||||||
enum class CSVState { UnquotedField, QuotedField, QuotedQuote };
|
enum class CSVState {
|
||||||
|
UnquotedField,
|
||||||
|
QuotedField,
|
||||||
|
QuotedQuote
|
||||||
|
};
|
||||||
|
|
||||||
// this function has been made in collaboration with
|
// this function has been made in collaboration with
|
||||||
// StackOverflow https://stackoverflow.com/a/30338543
|
// StackOverflow https://stackoverflow.com/a/30338543
|
||||||
void readCSVRow(const std::string &row, std::vector<std::string> &fields) {
|
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:
|
|
||||||
switch (c) {
|
|
||||||
case ',': // end of field
|
|
||||||
fields.push_back("");
|
fields.push_back("");
|
||||||
i++;
|
CSVState state = CSVState::UnquotedField;
|
||||||
break;
|
size_t i = 0; // index of the current field
|
||||||
case '"':
|
|
||||||
state = CSVState::QuotedField;
|
for (char c : row) {
|
||||||
break;
|
switch (state) {
|
||||||
default:
|
case CSVState::UnquotedField:
|
||||||
fields[i].push_back(c);
|
switch (c) {
|
||||||
break;
|
case ',': // end of field
|
||||||
}
|
fields.push_back("");
|
||||||
break;
|
i++;
|
||||||
case CSVState::QuotedField:
|
break;
|
||||||
switch (c) {
|
|
||||||
case '"':
|
case '"':
|
||||||
state = CSVState::QuotedQuote;
|
state = CSVState::QuotedField;
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
fields[i].push_back(c);
|
default:
|
||||||
break;
|
fields[i].push_back(c);
|
||||||
}
|
break;
|
||||||
break;
|
}
|
||||||
case CSVState::QuotedQuote:
|
|
||||||
switch (c) {
|
break;
|
||||||
case ',': // , after closing quote
|
|
||||||
fields.push_back("");
|
case CSVState::QuotedField:
|
||||||
i++;
|
switch (c) {
|
||||||
state = CSVState::UnquotedField;
|
case '"':
|
||||||
break;
|
state = CSVState::QuotedQuote;
|
||||||
case '"': // "" -> "
|
break;
|
||||||
fields[i].push_back('"');
|
|
||||||
state = CSVState::QuotedField;
|
default:
|
||||||
break;
|
fields[i].push_back(c);
|
||||||
default: // end of quote
|
break;
|
||||||
state = CSVState::UnquotedField;
|
}
|
||||||
break;
|
|
||||||
}
|
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<std::string>& line) {
|
||||||
|
line.clear();
|
||||||
|
|
||||||
|
if (!m_stream) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string row;
|
||||||
|
std::getline(m_stream, row);
|
||||||
|
|
||||||
|
readCSVRow(row, line);
|
||||||
|
|
||||||
|
return !!m_stream;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bool CsvReader::operator>>(std::vector<std::string> &line) {
|
|
||||||
line.clear();
|
|
||||||
if (!m_stream) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string row;
|
|
||||||
std::getline(m_stream, row);
|
|
||||||
|
|
||||||
readCSVRow(row, line);
|
|
||||||
|
|
||||||
return !!m_stream;
|
|
||||||
}
|
|
||||||
} // namespace csv
|
|
||||||
|
|
19
src/csv.hpp
19
src/csv.hpp
|
@ -2,15 +2,16 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace csv {
|
namespace csv {
|
||||||
class CsvReader {
|
class CsvReader {
|
||||||
private:
|
private:
|
||||||
std::ifstream m_stream;
|
std::ifstream m_stream;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CsvReader(auto stream) : m_stream(stream){};
|
CsvReader(auto stream): m_stream(stream){};
|
||||||
~CsvReader() = default;
|
~CsvReader() = default;
|
||||||
|
|
||||||
bool operator>>(std::vector<std::string> &line);
|
bool operator>>(std::vector<std::string>& line);
|
||||||
};
|
};
|
||||||
} // namespace csv
|
}
|
||||||
|
|
15
src/data.cpp
15
src/data.cpp
|
@ -1,17 +1,18 @@
|
||||||
#include "data.hpp"
|
#include "data.hpp"
|
||||||
|
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
|
|
||||||
namespace data {
|
namespace data {
|
||||||
key_t getIpcKeyFromExeName(char *argv0) {
|
key_t getIpcKeyFromExeName(char* argv0) {
|
||||||
auto k = ftok(argv0, 'X');
|
auto k = ftok(argv0, 'X');
|
||||||
|
|
||||||
if (k < 0) {
|
if (k < 0) {
|
||||||
throw std::runtime_error("failed to get IPC key");
|
throw std::runtime_error("failed to get IPC key");
|
||||||
}
|
}
|
||||||
|
|
||||||
return k;
|
return k;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} // namespace data
|
|
||||||
|
|
28
src/data.hpp
28
src/data.hpp
|
@ -3,22 +3,22 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace data {
|
namespace data {
|
||||||
const int serverbound_msg = 1;
|
const int serverbound_msg = 1;
|
||||||
const int clientbound_msg = 2;
|
const int clientbound_msg = 2;
|
||||||
|
|
||||||
key_t getIpcKeyFromExeName(char *argv0);
|
key_t getIpcKeyFromExeName(char* argv0);
|
||||||
|
|
||||||
struct RequestMapMsg {
|
struct RequestMapMsg {
|
||||||
long msgtype;
|
long msgtype;
|
||||||
|
|
||||||
unsigned int datalen;
|
unsigned int datalen;
|
||||||
char data[128];
|
char data[128];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ResponseMapMsg {
|
struct ResponseMapMsg {
|
||||||
long msgtype;
|
long msgtype;
|
||||||
|
|
||||||
unsigned int datalen;
|
unsigned int datalen;
|
||||||
char data[128];
|
char data[128];
|
||||||
};
|
};
|
||||||
} // namespace data
|
}
|
||||||
|
|
43
src/main.cpp
43
src/main.cpp
|
@ -1,32 +1,33 @@
|
||||||
#include "client.hpp"
|
#include "client.hpp"
|
||||||
#include "data.hpp"
|
#include "data.hpp"
|
||||||
#include "server.hpp"
|
#include "server.hpp"
|
||||||
|
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
void main2(int argc, char *argv[]) {
|
void main2(int argc, char* argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
throw std::runtime_error("not enough arguments!");
|
throw std::runtime_error("not enough arguments!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(argv[1], "serve") == 0) {
|
if (strcmp(argv[1], "serve") == 0) {
|
||||||
server::run(data::getIpcKeyFromExeName(argv[0]));
|
server::run(data::getIpcKeyFromExeName(argv[0]));
|
||||||
} else if (strcmp(argv[1], "map") == 0) {
|
} else if (strcmp(argv[1], "map") == 0) {
|
||||||
client::map(data::getIpcKeyFromExeName(argv[0]));
|
client::map(data::getIpcKeyFromExeName(argv[0]));
|
||||||
} else if (strcmp(argv[1], "resourceloc") == 0) {
|
} else if (strcmp(argv[1], "resourceloc") == 0) {
|
||||||
client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0]));
|
client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0]));
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("unknown command!");
|
throw std::runtime_error("unknown command!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
try {
|
try {
|
||||||
main2(argc, argv);
|
main2(argc, argv);
|
||||||
return 0;
|
return 0;
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception& e) {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what();
|
BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
171
src/mappings.cpp
171
src/mappings.cpp
|
@ -1,6 +1,8 @@
|
||||||
#include "mappings.hpp"
|
#include "mappings.hpp"
|
||||||
|
|
||||||
#include "ansi.hpp"
|
#include "ansi.hpp"
|
||||||
#include "csv.hpp"
|
#include "csv.hpp"
|
||||||
|
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
#include <boost/algorithm/string/classification.hpp>
|
#include <boost/algorithm/string/classification.hpp>
|
||||||
#include <boost/algorithm/string/split.hpp>
|
#include <boost/algorithm/string/split.hpp>
|
||||||
|
@ -18,95 +20,102 @@
|
||||||
namespace fs = boost::filesystem;
|
namespace fs = boost::filesystem;
|
||||||
|
|
||||||
namespace mappings {
|
namespace mappings {
|
||||||
Mappings Mappings::load() {
|
Mappings Mappings::load() {
|
||||||
std::map<std::string, Mapping> mappings;
|
std::map<std::string, Mapping> mappings;
|
||||||
std::map<std::string, std::string> renames;
|
std::map<std::string, std::string> renames;
|
||||||
|
|
||||||
// load mappings
|
// load mappings
|
||||||
fs::directory_iterator end_itr;
|
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());
|
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<std::string> l;
|
csv::CsvReader csv(itr->path().string());
|
||||||
while (csv >> l) {
|
|
||||||
if (l.size() < 2) {
|
std::vector<std::string> l;
|
||||||
BOOST_LOG_TRIVIAL(warning) << "found invalid mapping";
|
|
||||||
continue;
|
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<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 };
|
||||||
}
|
}
|
||||||
} catch (boost::filesystem::filesystem_error &e) {
|
|
||||||
if (e.code() == boost::system::errc::no_such_file_or_directory) {
|
void showMapInfo(Mapping mapping, std::optional<std::string> rename) {
|
||||||
throw std::runtime_error(
|
auto doc = mapping.doc.has_value()
|
||||||
std::string("The mappings directory is missing!\n") + e.what());
|
? 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")) {
|
std::string Mappings::map(std::string inp) {
|
||||||
csv::CsvReader csv("renames.csv");
|
if (!this->m_mappings.count(inp)) {
|
||||||
|
BOOST_LOG_TRIVIAL(warning) << "unknown mapping '" << inp << "'";
|
||||||
|
return "<unknown>";
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> l;
|
auto mapped = this->m_mappings[inp];
|
||||||
while (csv >> l) {
|
|
||||||
if (l.size() < 2) {
|
|
||||||
BOOST_LOG_TRIVIAL(warning) << "found invalid rename";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
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<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"
|
|
||||||
<< 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 "<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
|
|
||||||
|
|
|
@ -4,24 +4,25 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace mappings {
|
namespace mappings {
|
||||||
struct Mapping {
|
struct Mapping {
|
||||||
std::string orig;
|
std::string orig;
|
||||||
std::string name;
|
std::string name;
|
||||||
std::optional<std::string> doc;
|
std::optional<std::string> doc;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Mappings {
|
class Mappings {
|
||||||
std::map<std::string, Mapping> m_mappings;
|
std::map<std::string, Mapping> m_mappings;
|
||||||
std::map<std::string, std::string> m_renames;
|
std::map<std::string, std::string> m_renames;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Mappings(std::map<std::string, Mapping> mappings,
|
Mappings(
|
||||||
std::map<std::string, std::string> renames) {
|
std::map<std::string, Mapping> mappings,
|
||||||
this->m_mappings = mappings;
|
std::map<std::string, std::string> renames
|
||||||
this->m_renames = renames;
|
):
|
||||||
}
|
m_mappings(mappings), m_renames(renames) {}
|
||||||
~Mappings() = default;
|
|
||||||
static Mappings load();
|
~Mappings() = default;
|
||||||
std::string map(std::string inp);
|
static Mappings load();
|
||||||
};
|
std::string map(std::string inp);
|
||||||
} // namespace mappings
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "resourceloc.hpp"
|
#include "resourceloc.hpp"
|
||||||
|
|
||||||
#include <boost/algorithm/string/split.hpp>
|
#include <boost/algorithm/string/split.hpp>
|
||||||
#include <boost/algorithm/string/trim.hpp>
|
#include <boost/algorithm/string/trim.hpp>
|
||||||
#include <boost/format.hpp>
|
#include <boost/format.hpp>
|
||||||
|
@ -9,26 +10,30 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace resourceloc {
|
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\""));
|
boost::algorithm::trim_if(inp, boost::is_any_of(" \n\r\""));
|
||||||
|
|
||||||
std::vector<std::string> components;
|
std::vector<std::string> components;
|
||||||
boost::algorithm::split(components, inp, boost::is_any_of("/"));
|
boost::algorithm::split(components, inp, boost::is_any_of("/"));
|
||||||
|
|
||||||
if (components.size() < 3 || components[0] != "" || components[1] != "mods") {
|
if (components.size() < 3 || components[0] != "" || components[1] != "mods") {
|
||||||
BOOST_LOG_TRIVIAL(warning) << "invalid resourceloc";
|
BOOST_LOG_TRIVIAL(warning) << "invalid resourceloc";
|
||||||
return "<invalid>";
|
return "<invalid>";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string path_component;
|
std::string path_component;
|
||||||
for (unsigned int i = 3; i < components.size(); i++) {
|
|
||||||
path_component += components[i];
|
for (unsigned int i = 3; i < components.size(); i++) {
|
||||||
if (i != components.size() - 1) {
|
path_component += components[i];
|
||||||
path_component += "/";
|
|
||||||
|
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
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace resourceloc {
|
namespace resourceloc {
|
||||||
std::string resolve(std::string inp);
|
std::string resolve(std::string inp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
#include "server.hpp"
|
#include "server.hpp"
|
||||||
|
|
||||||
#include "data.hpp"
|
#include "data.hpp"
|
||||||
#include "mappings.hpp"
|
#include "mappings.hpp"
|
||||||
#include "resourceloc.hpp"
|
#include "resourceloc.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
@ -11,41 +13,48 @@
|
||||||
#include <sys/msg.h>
|
#include <sys/msg.h>
|
||||||
|
|
||||||
namespace server {
|
namespace server {
|
||||||
void sendToClient(int msqid, std::string msg) {
|
void sendToClient(int msqid, std::string msg) {
|
||||||
data::ResponseMapMsg pkt;
|
data::ResponseMapMsg pkt;
|
||||||
pkt.msgtype = data::clientbound_msg;
|
pkt.msgtype = data::clientbound_msg;
|
||||||
pkt.datalen = msg.size();
|
pkt.datalen = msg.size();
|
||||||
std::copy(msg.begin(), msg.end(), pkt.data);
|
std::copy(msg.begin(), msg.end(), pkt.data);
|
||||||
|
|
||||||
if (msgsnd(msqid, &pkt, sizeof(data::ResponseMapMsg) - sizeof(long), 0) < 0) {
|
if (msgsnd(msqid, &pkt, sizeof(data::ResponseMapMsg) - sizeof(long), 0) < 0) {
|
||||||
BOOST_LOG_TRIVIAL(error) << "failed to send response " << strerror(errno);
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string s(msg.data, msg.datalen);
|
void run(key_t key) {
|
||||||
BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'";
|
BOOST_LOG_TRIVIAL(info) << "starting server";
|
||||||
sendToClient(msqid, mappings.map(s));
|
|
||||||
}
|
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
|
|
||||||
|
|
|
@ -3,5 +3,5 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace server {
|
namespace server {
|
||||||
void run(key_t key);
|
void run(key_t key);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue