new files

This commit is contained in:
Hieu Hoang 2017-02-16 11:34:29 +00:00
parent 023a946a5b
commit 2483c3595b
2 changed files with 48 additions and 0 deletions

24
probingpt/util.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "util.hh"
#include "util/exception.hh"
namespace probingpt
{
template<>
bool Scan<bool>(const std::string &input)
{
std::string lc = ToLower(input);
if (lc == "yes" || lc == "y" || lc == "true" || lc == "1") return true;
if (lc == "no" || lc == "n" || lc == "false" || lc == "0") return false;
UTIL_THROW2("Could not interpret " << input << " as a boolean. After lowercasing, valid values are yes, y, true, 1, no, n, false, and 0.");
}
const std::string ToLower(const std::string& str)
{
std::string lc(str);
std::transform(lc.begin(), lc.end(), lc.begin(), (int (*)(int))std::tolower);
return
lc ;
}
}

24
probingpt/util.hh Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <string>
#include <sstream>
namespace probingpt
{
//! convert string to variable of type T. Used to reading floats, int etc from files
template<typename T>
inline T Scan(const std::string &input)
{
std::stringstream stream(input);
T ret;
stream >> ret;
return ret;
}
//! Specialisation to understand yes/no y/n true/false 0/1
template<>
bool Scan<bool>(const std::string &input);
const std::string ToLower(const std::string& str);
}