Commit 9fcf7ca2 authored by Alvarez, Gonzalo's avatar Alvarez, Gonzalo
Browse files

Ainur robustness fix

Ainur now makes sure an integer is provided when needed,
and similarly for a real number.
parent 1962284d
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -267,7 +267,11 @@ private:
	                     typename EnableIf<Loki::TypeTraits<T>::isIntegral,
	                     int>::Type = 0) const
	{
		t = atoi(label.c_str());
		try {
			t = PsimagLite::atoi(label.c_str());
		} catch (...) {
			err("FATAL: AinurState: Label " + label + " must be an integer\n");
		}
	}

	template<typename T>
@@ -276,7 +280,11 @@ private:
	                     typename EnableIf<Loki::TypeTraits<T>::isFloat,
	                     int>::Type = 0) const
	{
		t = atof(label.c_str());
		try {
			t = PsimagLite::atof(label.c_str());
		} catch (...) {
			err("FATAL: AinurState: Label " + label + " must be a real number\n");
		}
	}

	void convertInternal(String& t, String label) const
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ Please see full open source license included in file LICENSE.
#include <cstdlib>
#include <sstream>
#include <type_traits>
#include <cctype>
#ifdef USE_CUSTOM_ALLOCATOR
#include "MemoryCpu.h"
#endif
+28 −0
Original line number Diff line number Diff line
@@ -57,6 +57,34 @@ String basename(const String& path)
	              path.end());
}

int atoi(String str)
{
	const SizeType n = str.length();
	for (SizeType i = 0; i < n; ++i) {
		if (isdigit(str[i])) continue;
		if (str[i] == '-' || str[i] == '+') continue;
		throw RuntimeError("atoi received a non-digit\n");
	}

	return std::atoi(str.c_str());
}

int atof(String str)
{
	const SizeType n = str.length();
	for (SizeType i = 0; i < n; ++i) {
		if (isdigit(str[i])) continue;
		if (str[i] == '-' ||
		        str[i] == '+' ||
		        str[i] == 'e' ||
		        str[i] == 'E' ||
		        str[i] == '.') continue;
		throw RuntimeError("atof received a non-digit\n");
	}

	return std::atoi(str.c_str());
}

const int PsiApp::libSizeOfSizeType_ = sizeof(SizeType);

} // namespace PsimagLite
+4 −0
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@ std::istream& operator>>(std::istream&,std::pair<SizeType,SizeType>&);

SizeType log2Integer(SizeType x);

int atoi(String);

int atof(String);

void err(String);

struct MatchPathSeparator {