Added Util::stringToBool utility function.

This commit is contained in:
Thulinma 2015-10-01 13:48:51 +02:00
parent d7fb7dd779
commit 1e9ee73bfe
2 changed files with 20 additions and 1 deletions

View file

@ -1,4 +1,5 @@
#include "bitfields.h" #include "bitfields.h"
#include <string.h>
/// Takes a pointer, offset bitcount and data bitcount, returning the unsigned int read from the givens. /// 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. /// 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 }//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);
}

View file

@ -1,5 +1,8 @@
#include <string>
namespace Util{
bool stringToBool(std::string & str);
}
namespace Bit{ namespace Bit{
//bitfield getters //bitfield getters