From f8fa8f1b2bcdc0b9583571af350bbc21da93e25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 17 Jun 2021 14:18:54 +0200 Subject: [PATCH] fixing compil --- sources/Parser/ParserYaml.cpp | 90 ++++++++++------------------------- sources/Parser/ParserYaml.hpp | 6 +-- sources/Utils/Utils.cpp | 28 +++++++++++ sources/Utils/Utils.hpp | 5 ++ 4 files changed, 60 insertions(+), 69 deletions(-) diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index 6fa482d8..32bcad39 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -251,35 +251,27 @@ namespace BBM { map[std::make_tuple(pos.x, pos.y, pos.z)] = blockType; } - void ParserYAML::_loadBlocks(std::shared_ptr scene) + void ParserYAML::_loadBlocks(std::shared_ptr scene, Node &node) { - std::ifstream file("save/" + fileName + "_block.yml"); - std::string line; - std::vector lines; MapGenerator::MapBlock map; + int size = -1; - if (!file.good()) - throw (ParserError("File error")); - while (std::getline(file, line)) { - if (line.find("width:") != std::string::npos) { - Runner::mapWidth = std::atoi(line.substr(line.find(": ", 0) + 2, line.length()).c_str()); - continue; - } - if (line.find("height:") != std::string::npos) { - Runner::mapHeight = std::atoi(line.substr(line.find(": ", 0) + 2, line.length()).c_str()); - continue; - } - if (line.empty() || line.find("blocks:") != std::string::npos) - continue; - lines.push_back(line); + if (!Utils::tryParseInteger(node.getProperty("width"), size)) { + throw ParserError("width property must be an int"); } + Runner::mapWidth = size; + if (!Utils::tryParseInteger(node.getProperty("height"), size)) { + throw ParserError("width property must be an int"); + } + Runner::mapHeight = size; + for (int i = 0; i < Runner::mapWidth; i++) for (int j = 0; j < Runner::mapHeight; j++) map[std::make_tuple(i, 0, j)] = MapGenerator::NOTHING; - for (int index = 0; lines.size() != index; index++) { + /*for (int index = 0; lines.size() != index; index++) { _loadBlock(scene, lines, index, map); index--; - } + }*/ MapGenerator::loadMap(Runner::mapWidth, Runner::mapHeight, map, scene); } @@ -332,7 +324,7 @@ namespace BBM { Node blocksInfos = parseFile("assets/save_block.yml"); Node bonusesInfos = parseFile("assets/save_bonus.yml"); Node playerInfos = parseFile("assets/save_player.yml"); - _loadBlocks(gameScene); + _loadBlocks(gameScene, blocksInfos); _loadBonuses(gameScene); _loadPlayers(gameScene); } @@ -364,7 +356,7 @@ namespace BBM { try { auto start = line.find("[", 0) + 1; subStr = line.substr(start, line.find("]", 0) - 1 - start); - auto pos = _splitStr(subStr, ' '); + auto pos = Utils::splitStr(subStr, ' '); if (pos.size() != 3) throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); x = pos[0]; @@ -373,15 +365,15 @@ namespace BBM { } catch (const std::out_of_range &err) { throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); } - if (!_isFloat(x) || !_isFloat(y) || !_isFloat(z)) - throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); + //if (!_isFloat(x) || !_isFloat(y) || !_isFloat(z)) + // throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); return Vector3f(std::atof(x.c_str()), std::atof(y.c_str()), std::atof(z.c_str())); } int ParserYAML::_parseMaxBomb(std::string &line) { - if (line.find(": ") == std::string::npos || !_isInteger(line.substr(line.find(": ") + 2))) - throw (ParserError("Error with saved map: Couldn't parse max bomb.\n Loading default maps...")); + //if (line.find(": ") == std::string::npos || !_isInteger(line.substr(line.find(": ") + 2))) + // throw (ParserError("Error with saved map: Couldn't parse max bomb.\n Loading default maps...")); if (line.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse max bomb.\n Loading default maps...")); return (std::atoi(line.substr(line.find(": ") + 2).c_str())); @@ -389,8 +381,8 @@ namespace BBM { float ParserYAML::_parseExplosionRadius(std::string &line) { - if (line.find(": ") == std::string::npos || !_isFloat(line.substr(line.find(": ") + 2))) - throw (ParserError("Error with saved map: Couldn't parse explosion radius.\n Loading default maps...")); + //if (line.find(": ") == std::string::npos || !_isFloat(line.substr(line.find(": ") + 2))) + // throw (ParserError("Error with saved map: Couldn't parse explosion radius.\n Loading default maps...")); if (line.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse explosion radius.\n Loading default maps...")); return (std::atof(line.substr(line.find(": ") + 2).c_str())); @@ -398,8 +390,8 @@ namespace BBM { float ParserYAML::_parseSpeed(std::string &line) { - if (line.find(": ") == std::string::npos || !_isFloat(line.substr(line.find(": ") + 2))) - throw (ParserError("Error with saved map: Couldn't parse speed.\n Loading default maps...")); + //if (line.find(": ") == std::string::npos || !_isFloat(line.substr(line.find(": ") + 2))) + // throw (ParserError("Error with saved map: Couldn't parse speed.\n Loading default maps...")); if (line.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse speed.\n Loading default maps...")); return (std::atof(line.substr(line.find(": ") + 2).c_str())); @@ -407,8 +399,8 @@ namespace BBM { MapGenerator::BlockType ParserYAML::_parseBlockType(std::string &blockType) { - if (blockType.find(": ") == std::string::npos || !_isInteger(blockType.substr(blockType.find(": ") + 2))) - throw (ParserError("Error with saved map: Couldn't parse block type.\n Loading default maps...")); + //if (blockType.find(": ") == std::string::npos || !_isInteger(blockType.substr(blockType.find(": ") + 2))) + // throw (ParserError("Error with saved map: Couldn't parse block type.\n Loading default maps...")); if (blockType.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse block type.\n Loading default maps...")); return (static_cast(std::atoi(blockType.substr(blockType.find(": ") + 2).c_str()))); @@ -416,43 +408,13 @@ namespace BBM { Bonus::BonusType ParserYAML::_parseBonusType(std::string &bonusType) { - if (bonusType.find(": ") == std::string::npos || !_isInteger(bonusType.substr(bonusType.find(": ") + 2))) - throw (ParserError("Error with saved map: Couldn't parse bonus type.\n Loading default maps...")); + //if (bonusType.find(": ") == std::string::npos || !_isInteger(bonusType.substr(bonusType.find(": ") + 2))) + // throw (ParserError("Error with saved map: Couldn't parse bonus type.\n Loading default maps...")); if (bonusType.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse bonus type.\n Loading default maps...")); return (static_cast(std::atoi(bonusType.substr(bonusType.find(": ") + 2).c_str()))); } - bool ParserYAML::_isInteger(const std::string &s) - { - std::istringstream iss(s); - int i; - - iss >> std::noskipws >> i; - return iss.eof() && !iss.fail(); - } - - bool ParserYAML::_isFloat(const std::string &s) - { - std::istringstream iss(s); - float f; - - iss >> std::noskipws >> f; - return iss.eof() && !iss.fail(); - } - - std::vector ParserYAML::_splitStr(const std::string &str, char delim) - { - std::vector strings; - std::istringstream f(str); - std::string buffer; - - while (std::getline(f, buffer, delim)) {; - strings.push_back(buffer); - } - return strings; - } - std::string ParserYAML::parseHeader(const std::string &line) { std::stringstream ss(line); diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index 5f50b7ac..b5b01632 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -85,15 +85,11 @@ namespace BBM { static void _loadPlayers(std::shared_ptr scene); //!@param scene Scene to update //!@brief load all blocks into scene - static void _loadBlocks(std::shared_ptr scene); + static void _loadBlocks(std::shared_ptr scene, Node &node); //!@param scene Scene to update //!@brief load all blocks into scene static void _loadBonuses(std::shared_ptr scene); - static bool _isInteger(const std::string &s); - static bool _isFloat(const std::string &s); - static std::vector _splitStr(const std::string &str, char delim); - static std::string parseHeader(const std::string &line); static std::pair parseProperty(const std::string &line); diff --git a/sources/Utils/Utils.cpp b/sources/Utils/Utils.cpp index fd4f39ce..8e884951 100644 --- a/sources/Utils/Utils.cpp +++ b/sources/Utils/Utils.cpp @@ -58,4 +58,32 @@ namespace BBM return static_cast(numberOfMatches); } + bool Utils::tryParseInteger(const std::string &s, int &i) + { + std::istringstream iss(s); + + iss >> std::noskipws >> i; + return iss.eof() && !iss.fail(); + } + + bool Utils::tryParseFloat(const std::string &s, float &f) + { + std::istringstream iss(s); + + iss >> std::noskipws >> f; + return iss.eof() && !iss.fail(); + } + + std::vector Utils::splitStr(const std::string &str, char delim) + { + std::vector strings; + std::istringstream f(str); + std::string buffer; + + while (std::getline(f, buffer, delim)) {; + strings.push_back(buffer); + } + return strings; + } + } \ No newline at end of file diff --git a/sources/Utils/Utils.hpp b/sources/Utils/Utils.hpp index 4bca5c4d..d3220256 100644 --- a/sources/Utils/Utils.hpp +++ b/sources/Utils/Utils.hpp @@ -28,6 +28,11 @@ namespace BBM //! @brief find the frequency of a substring in a string static int findFrequency(const std::string &s, const std::string &pattern); + + + static bool tryParseInteger(const std::string &s, int &i); + static bool tryParseFloat(const std::string &s, float &f); + static std::vector splitStr(const std::string &str, char delim); };