Improve check argument parsing

This commit is contained in:
Jean Flach 2014-11-13 15:57:10 +01:00
parent bb04f66b25
commit f85be08480
4 changed files with 28 additions and 17 deletions

View File

@ -192,7 +192,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("unit")) {
try {
printInfo.unit = parseBUnit(vm["unit"].as<wstring>().c_str());
printInfo.unit = parseBUnit(vm["unit"].as<wstring>());
} catch (std::invalid_argument) {
wcout << L"Unknown unit Type " << vm["unit"].as<wstring>() << endl;
return 3;

View File

@ -164,7 +164,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct&
if (vm.count("unit")) {
try{
printInfo.unit = parseTUnit(vm["unit"].as<wstring>().c_str());
printInfo.unit = parseTUnit(vm["unit"].as<wstring>());
} catch (std::invalid_argument) {
} wcout << L"Unknown unit type " << vm["unit"].as<wstring>() << endl;

View File

@ -23,6 +23,8 @@
#include "boost\algorithm\string.hpp"
#include "boost\lexical_cast.hpp"
using namespace boost::algorithm;
using std::wstring;
threshold parse(const wstring& stri)
@ -32,6 +34,9 @@ threshold parse(const wstring& stri)
wstring str = stri;
//kill whitespace
str.erase((std::remove(str.begin(), str.end(), L" "), str.end()));
bool low = (str.at(0) == L'!');
if (low)
str = wstring(str.begin() + 1, str.end());
@ -74,23 +79,26 @@ threshold parse(const wstring& stri)
}
}
Bunit parseBUnit(const wchar_t *str)
Bunit parseBUnit(const wstring& str)
{
if (!wcscmp(str, L"B"))
wstring wstr = to_upper_copy(str);
if (wstr == L"B")
return BunitB;
if (!wcscmp(str, L"kB"))
if (wstr == L"kB")
return BunitkB;
if (!wcscmp(str, L"MB"))
if (wstr == L"MB")
return BunitMB;
if (!wcscmp(str, L"GB"))
if (wstr == L"GB")
return BunitGB;
if (!wcscmp(str, L"TB"))
if (wstr == L"TB")
return BunitTB;
throw std::invalid_argument("Unknown unit type");
}
wstring BunitStr(const Bunit& unit) {
wstring BunitStr(const Bunit& unit)
{
switch (unit) {
case BunitB:
return L"B";
@ -106,20 +114,23 @@ wstring BunitStr(const Bunit& unit) {
return NULL;
}
Tunit parseTUnit(const wchar_t *str) {
if (!wcscmp(str, L"ms"))
Tunit parseTUnit(const wstring& str) {
wstring wstr = to_lower_copy(str);
if (wstr == L"ms")
return TunitMS;
if (!wcscmp(str, L"s"))
if (wstr == L"s")
return TunitS;
if (!wcscmp(str, L"m"))
if (wstr == L"m")
return TunitM;
if (!wcscmp(str, L"h"))
if (wstr == L"h")
return TunitH;
throw std::invalid_argument("Unknown unit type");
}
wstring TunitStr(const Tunit& unit) {
wstring TunitStr(const Tunit& unit)
{
switch (unit) {
case TunitMS:
return L"ms";

View File

@ -76,8 +76,8 @@ public:
};
threshold parse(const std::wstring&);
Bunit parseBUnit(const wchar_t *);
Bunit parseBUnit(const std::wstring&);
std::wstring BunitStr(const Bunit&);
Tunit parseTUnit(const wchar_t *);
Tunit parseTUnit(const std::wstring&);
std::wstring TunitStr(const Tunit&);
#endif