From 05d6531c7a8ecfad513a0e76b44b273b70fa919b Mon Sep 17 00:00:00 2001 From: Ruslan Baratov Date: Wed, 26 Nov 2014 01:45:26 +0300 Subject: [PATCH] cmSystemTools: Add StringToInt helper Convert a string to a signed integer and reject any extra input. Co-Author: Rolf Eike Beer --- Source/cmSystemTools.cxx | 8 ++++++++ Source/cmSystemTools.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index baac7b878..9852dd6f1 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2923,3 +2923,11 @@ std::vector cmSystemTools::tokenize(const std::string& str, } return tokens; } + +//---------------------------------------------------------------------------- +bool cmSystemTools::StringToInt(const char* str, int* value) +{ + char *endp; + *value = static_cast(strtol(str, &endp, 10)); + return (*endp == '\0') && (endp != str); +} diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 4455dd1d8..763389b5f 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -458,6 +458,9 @@ public: static std::vector tokenize(const std::string& str, const std::string& sep); + /** Convert string to int. Expected that the whole string is an integer */ + static bool StringToInt(const char* str, int* value); + #ifdef _WIN32 struct WindowsFileRetry {