From 5b1f9d18e6fd59c187d18827e90df28acfaa2382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 17 Jun 2021 00:21:19 +0200 Subject: [PATCH] adding an utils struct --- CMakeLists.txt | 2 +- sources/Parser/ParserYaml.cpp | 15 +++++++++++ sources/Parser/ParserYaml.hpp | 2 ++ sources/Utils/Utils.cpp | 51 +++++++++++++++++++++++++++++++++++ sources/Utils/Utils.hpp | 31 +++++++++++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 sources/Utils/Utils.cpp create mode 100644 sources/Utils/Utils.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5030f6db..ac648f39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,7 +147,7 @@ set(SOURCES sources/Runner/LobbyScene.cpp sources/Runner/ResumeLobbyScene.cpp sources/Runner/ScoreScene.cpp - sources/Parser/Node.cpp sources/Parser/Node.hpp) + sources/Parser/Node.cpp sources/Parser/Node.hpp sources/Utils/Utils.cpp sources/Utils/Utils.hpp) add_executable(bomberman sources/main.cpp ${SOURCES} diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index f49e4fd8..0a862bd1 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -441,4 +441,19 @@ namespace BBM { } return strings; } + + std::string ParserYAML::getHeader(const std::string &line) + { + std::stringstream ss(line); + std::string headerName; + std::string garbage; + + ss >> headerName >> garbage; + + + if (!garbage.empty()) { + throw ParserError("error on getHeader line: "); + } + return headerName; + } } \ No newline at end of file diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index 2e41c176..9c382218 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -93,6 +93,8 @@ namespace BBM { static bool _isFloat(const std::string &s); static std::vector _splitStr(const std::string &str, char delim); + static std::string getHeader(const std::string &line); + public: //! @brief All name that was into the file static std::vector playerName; diff --git a/sources/Utils/Utils.cpp b/sources/Utils/Utils.cpp new file mode 100644 index 00000000..c78edd88 --- /dev/null +++ b/sources/Utils/Utils.cpp @@ -0,0 +1,51 @@ +// +// Created by cbihan on 17/06/2021. +// + +#include +#include +#include +#include +#include "Utils.hpp" + +namespace BBM +{ + 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) + { + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); + } + + inline void Utils::trim(std::string &s) + { + ltrim(s); + rtrim(s); + } + + inline std::string Utils::ltrim_copy(std::string s) + { + ltrim(s); + return s; + } + + inline std::string Utils::rtrim_copy(std::string s) + { + rtrim(s); + return s; + } + + inline std::string Utils::trim_copy(std::string s) + { + trim(s); + return s; + } + +} \ No newline at end of file diff --git a/sources/Utils/Utils.hpp b/sources/Utils/Utils.hpp new file mode 100644 index 00000000..96b1f5e0 --- /dev/null +++ b/sources/Utils/Utils.hpp @@ -0,0 +1,31 @@ +// +// Created by cbihan on 17/06/2021. +// + +#pragma once + +namespace BBM +{ + struct Utils + { + //! @brief trim left end + static inline void ltrim(std::string &s); + + //! @brief trim right end + 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); + + //! @brief trim right end (copying) + static inline std::string rtrim_copy(std::string s); + + //! @brief trim from both ends (copying) + static inline std::string trim_copy(std::string s); + }; + + +} \ No newline at end of file