mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-03 10:26:29 +00:00
adding theoritcally basic parsing
This commit is contained in:
@@ -43,4 +43,14 @@ namespace BBM
|
||||
}
|
||||
return childs;
|
||||
}
|
||||
|
||||
void Node::setName(const std::string &name)
|
||||
{
|
||||
this->_name = name;
|
||||
}
|
||||
|
||||
void Node::setProperty(const std::pair<std::string, std::string> &propertyNameValue)
|
||||
{
|
||||
this->setProperty(propertyNameValue.first, propertyNameValue.second);
|
||||
}
|
||||
}
|
||||
@@ -26,11 +26,14 @@ namespace BBM
|
||||
|
||||
std::string getName() const;
|
||||
|
||||
void setName(const std::string &name);
|
||||
|
||||
void addChildNode(const Node &childNode);
|
||||
|
||||
std::vector<Node> getChildNodes(const std::string &childNodeName);
|
||||
|
||||
void setProperty(const std::string &propertyName, const std::string &propertyValue);
|
||||
void setProperty(const std::pair<std::string, std::string> &propertyNameValue);
|
||||
|
||||
std::string getProperty(const std::string &propertyName) const;
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <Component/Renderer/Drawable2DComponent.hpp>
|
||||
#include <System/Lobby/LobbySystem.hpp>
|
||||
#include <filesystem>
|
||||
#include "Utils/Utils.hpp"
|
||||
|
||||
namespace RAY3D = RAY::Drawables::Drawables3D;
|
||||
namespace RAY2D = RAY::Drawables::Drawables2D;
|
||||
@@ -442,7 +443,7 @@ namespace BBM {
|
||||
return strings;
|
||||
}
|
||||
|
||||
std::string ParserYAML::getHeader(const std::string &line)
|
||||
std::string ParserYAML::parseHeader(const std::string &line)
|
||||
{
|
||||
std::stringstream ss(line);
|
||||
std::string headerName;
|
||||
@@ -452,8 +453,59 @@ namespace BBM {
|
||||
|
||||
|
||||
if (!garbage.empty()) {
|
||||
throw ParserError("error on getHeader line: ");
|
||||
throw ParserError("ill formed header, line: " + Utils::trimCopy(line));
|
||||
}
|
||||
if (headerName.back() != ':') {
|
||||
throw ParserError("header not ended with ':' , line: " + Utils::trimCopy(line));
|
||||
}
|
||||
return headerName;
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> ParserYAML::parseProperty(const std::string &line)
|
||||
{
|
||||
std::stringstream ss(line);
|
||||
std::string propertyName;
|
||||
std::string propertyValue;
|
||||
std::string garbage;
|
||||
|
||||
ss >> propertyName >> propertyValue >> garbage;
|
||||
|
||||
if (!garbage.empty()) {
|
||||
throw ParserError("ill formed property, line: " + Utils::trimCopy(line));
|
||||
}
|
||||
if (propertyName.back() != ':') {
|
||||
throw ParserError("property name not ended with ':' , line: " + Utils::trimCopy(line));
|
||||
}
|
||||
|
||||
return std::make_pair(propertyName, propertyValue);
|
||||
}
|
||||
|
||||
bool ParserYAML::isHeader(const std::string &line)
|
||||
{
|
||||
return line.find(':') == line.back();
|
||||
}
|
||||
|
||||
Node ParserYAML::parseFile(const std::string &path)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
|
||||
return parseNode(file, "root");
|
||||
}
|
||||
|
||||
Node ParserYAML::parseNode(std::ifstream &file, const std::string &nodeName)
|
||||
{
|
||||
std::string line;
|
||||
Node node(nodeName);
|
||||
|
||||
while(std::getline(file, line)) {
|
||||
if (line.empty())
|
||||
continue;
|
||||
if (isHeader(line)) {
|
||||
node.addChildNode(parseNode(file, parseHeader(line)));
|
||||
} else {
|
||||
node.setProperty(parseProperty(line));
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <Wal.hpp>
|
||||
#include "Items/Bonus.hpp"
|
||||
#include "Node.hpp"
|
||||
#include "Map/Map.hpp"
|
||||
#include "Component/Controllable/ControllableComponent.hpp"
|
||||
|
||||
@@ -93,7 +94,17 @@ namespace BBM {
|
||||
static bool _isFloat(const std::string &s);
|
||||
static std::vector<std::string> _splitStr(const std::string &str, char delim);
|
||||
|
||||
static std::string getHeader(const std::string &line);
|
||||
static std::string parseHeader(const std::string &line);
|
||||
|
||||
static std::pair<std::string ,std::string> parseProperty(const std::string &line);
|
||||
|
||||
static bool isHeader(const std::string &line);
|
||||
|
||||
static Node parseFile(const std::string &path);
|
||||
|
||||
static Node parseNode(std::ifstream &file, const std::string &nodeName);
|
||||
|
||||
static constexpr const char* indent = " ";
|
||||
|
||||
public:
|
||||
//! @brief All name that was into the file
|
||||
|
||||
@@ -10,14 +10,14 @@
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
inline void Utils::ltrim(std::string &s)
|
||||
inline void Utils::lTrim(std::string &s)
|
||||
{
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
}
|
||||
|
||||
inline void Utils::rtrim(std::string &s)
|
||||
inline void Utils::rTrim(std::string &s)
|
||||
{
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
@@ -26,23 +26,23 @@ namespace BBM
|
||||
|
||||
inline void Utils::trim(std::string &s)
|
||||
{
|
||||
ltrim(s);
|
||||
rtrim(s);
|
||||
lTrim(s);
|
||||
rTrim(s);
|
||||
}
|
||||
|
||||
inline std::string Utils::ltrim_copy(std::string s)
|
||||
inline std::string Utils::lTrimCopy(std::string s)
|
||||
{
|
||||
ltrim(s);
|
||||
lTrim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string Utils::rtrim_copy(std::string s)
|
||||
inline std::string Utils::rTrimCopy(std::string s)
|
||||
{
|
||||
rtrim(s);
|
||||
rTrim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string Utils::trim_copy(std::string s)
|
||||
inline std::string Utils::trimCopy(std::string s)
|
||||
{
|
||||
trim(s);
|
||||
return s;
|
||||
|
||||
@@ -9,22 +9,22 @@ namespace BBM
|
||||
struct Utils
|
||||
{
|
||||
//! @brief trim left end
|
||||
static inline void ltrim(std::string &s);
|
||||
static inline void lTrim(std::string &s);
|
||||
|
||||
//! @brief trim right end
|
||||
static inline void rtrim(std::string &s);
|
||||
static inline void rTrim(std::string &s);
|
||||
|
||||
//! @brief trim from both ends
|
||||
static inline void trim(std::string &s);
|
||||
|
||||
//! @brief trim left end (copying)
|
||||
static inline std::string ltrim_copy(std::string s);
|
||||
static inline std::string lTrimCopy(std::string s);
|
||||
|
||||
//! @brief trim right end (copying)
|
||||
static inline std::string rtrim_copy(std::string s);
|
||||
static inline std::string rTrimCopy(std::string s);
|
||||
|
||||
//! @brief trim from both ends (copying)
|
||||
static inline std::string trim_copy(std::string s);
|
||||
static inline std::string trimCopy(std::string s);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user