From 1e9ee73bfeeaec6268702ff01be6e7970c2daa92 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Thu, 1 Oct 2015 13:48:51 +0200 Subject: [PATCH] Added Util::stringToBool utility function. --- lib/bitfields.cpp | 16 ++++++++++++++++ lib/bitfields.h | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/bitfields.cpp b/lib/bitfields.cpp index 6813299c..10bc7e3b 100644 --- a/lib/bitfields.cpp +++ b/lib/bitfields.cpp @@ -1,4 +1,5 @@ #include "bitfields.h" +#include /// Takes a pointer, offset bitcount and data bitcount, returning the unsigned int read from the givens. /// offsetBits may be > 7, in which case offsetBits / 8 is added to the pointer automatically. @@ -62,3 +63,18 @@ void Bit::setMSB(char * pointer, unsigned int offsetBits, unsigned int dataBits, }//Loop until we run out of dataBits, then return the result } +/// Parses a string reference to a boolean. +/// Returns true if the string, with whitespace removed and converted to lowercase, prefix-matches any of: "1", "yes", "true", "cont". +/// Returns false otherwise. +bool Util::stringToBool(std::string & str){ + std::string tmp; + tmp.reserve(4); + for (unsigned int i = 0; i < str.size() && tmp.size() < 4; ++i){ + if (!::isspace(str[i])){ + tmp.push_back((char)tolower(str[i])); + } + } + return (strncmp(tmp.c_str(), "1", 1) == 0 || strncmp(tmp.c_str(), "yes", 3) == 0 || strncmp(tmp.c_str(), "true", 4) == 0 || strncmp(tmp.c_str(), "cont", 4) == 0); +} + + diff --git a/lib/bitfields.h b/lib/bitfields.h index 7af1d8a6..bf57770b 100644 --- a/lib/bitfields.h +++ b/lib/bitfields.h @@ -1,5 +1,8 @@ +#include - +namespace Util{ + bool stringToBool(std::string & str); +} namespace Bit{ //bitfield getters