瀏覽代碼

cmSystemTools: Add StringToInt helper

Convert a string to a signed integer and reject any extra input.

Co-Author: Rolf Eike Beer <[email protected]>
Ruslan Baratov 11 年之前
父節點
當前提交
05d6531c7a
共有 2 個文件被更改,包括 11 次插入0 次删除
  1. 8 0
      Source/cmSystemTools.cxx
  2. 3 0
      Source/cmSystemTools.h

+ 8 - 0
Source/cmSystemTools.cxx

@@ -2923,3 +2923,11 @@ std::vector<std::string> cmSystemTools::tokenize(const std::string& str,
     }
   return tokens;
 }
+
+//----------------------------------------------------------------------------
+bool cmSystemTools::StringToInt(const char* str, int* value)
+{
+  char *endp;
+  *value = static_cast<int>(strtol(str, &endp, 10));
+  return (*endp == '\0') && (endp != str);
+}

+ 3 - 0
Source/cmSystemTools.h

@@ -458,6 +458,9 @@ public:
   static std::vector<std::string> 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
   {