BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
744c6a1
Branches
Tags
master
Blinker
src
noarch
StringParser.cpp
allow negative coordinates for positions
Stefan Schuermans
commited
744c6a1
at 2011-11-18 22:27:52
StringParser.cpp
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <string> #include "StringParser.h" namespace Blinker { /** * @brief constructor * @param[in] str string to parse */ StringParser::StringParser(const std::string &str): m_str(str), m_it(str.begin()) { } /** * @brief parse fixed character * @param[in] chr character to expect * @return if expected character was found and processed */ bool StringParser::fixChr(char chr) { if (m_it == m_str.end() || *m_it != chr) return false; ++m_it; return true; } /** * @brief parse unsigned number * @param[out] uint number parsed from string * @return if parsing was successful */ bool StringParser::uintNo(unsigned int &uint) { bool ret = false; uint = 0; while (m_it != m_str.end() && *m_it >= '0' && *m_it <= '9') { uint *= 10; uint += *m_it - '0'; ++m_it; ret = true; } return ret; } /** * @brief parse unsigned number and check minimum * @param[in] min minimum value to expect * @param[out] uint number parsed from string * @return if parsing was successful */ bool StringParser::uintMin(unsigned int min, unsigned int &uint) { return uintNo(uint) && uint >= min; } /** * @brief parse signed number * @param[out] sint number parsed from string * @return if parsing was successful */ bool StringParser::sintNo(int &sint) { int sign; unsigned int uint; bool ret; sint = 0; if (m_it != m_str.end()) { switch (*m_it) { case '+': sign = 1; ++m_it; break; case '-': sign = -1; ++m_it; break; default: sign = 1; break; } } ret = uintNo(uint); sint = sign * uint; return ret; } /** * @brief check if parsing is done * @return if parsing is done (i.e. has arrived at the end of the string) */ bool StringParser::isDone() { return m_it == m_str.end(); } } // namespace Blinker