Warning, file /sdk/codevis/thirdparty/soci/include/private/soci-cstrtoi.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // 0002 // Copyright (C) 2020 Vadim Zeitlin. 0003 // Distributed under the Boost Software License, Version 1.0. 0004 // (See accompanying file LICENSE_1_0.txt or copy at 0005 // http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 0008 #ifndef SOCI_PRIVATE_SOCI_CSTRTOI_H_INCLUDED 0009 #define SOCI_PRIVATE_SOCI_CSTRTOI_H_INCLUDED 0010 0011 #include "soci/error.h" 0012 0013 #include <cstdlib> 0014 #include <limits> 0015 0016 namespace soci 0017 { 0018 0019 namespace details 0020 { 0021 0022 // Convert string to a signed value of the given type, checking for overflow. 0023 // 0024 // Fill the provided result parameter and return true on success or false on 0025 // error, e.g. if the string couldn't be converted at all, if anything remains 0026 // in the string after conversion or if the value is out of range. 0027 template <typename T> 0028 bool cstring_to_integer(T& result, char const* buf) 0029 { 0030 char * end; 0031 0032 // No strtoll() on MSVC versions prior to Visual Studio 2013 0033 #if !defined (_MSC_VER) || (_MSC_VER >= 1800) 0034 long long t = strtoll(buf, &end, 10); 0035 #else 0036 long long t = _strtoi64(buf, &end, 10); 0037 #endif 0038 0039 if (end == buf || *end != '\0') 0040 return false; 0041 0042 // successfully converted to long long 0043 // and no other characters were found in the buffer 0044 0045 const T max = (std::numeric_limits<T>::max)(); 0046 const T min = (std::numeric_limits<T>::min)(); 0047 if (t > static_cast<long long>(max) || t < static_cast<long long>(min)) 0048 return false; 0049 0050 result = static_cast<T>(t); 0051 0052 return true; 0053 } 0054 0055 // Similar to the above, but for the unsigned integral types. 0056 template <typename T> 0057 bool cstring_to_unsigned(T& result, char const* buf) 0058 { 0059 char * end; 0060 0061 // No strtoll() on MSVC versions prior to Visual Studio 2013 0062 #if !defined (_MSC_VER) || (_MSC_VER >= 1800) 0063 unsigned long long t = strtoull(buf, &end, 10); 0064 #else 0065 unsigned long long t = _strtoui64(buf, &end, 10); 0066 #endif 0067 0068 if (end == buf || *end != '\0') 0069 return false; 0070 0071 // successfully converted to unsigned long long 0072 // and no other characters were found in the buffer 0073 0074 const T max = (std::numeric_limits<T>::max)(); 0075 if (t > static_cast<unsigned long long>(max)) 0076 return false; 0077 0078 result = static_cast<T>(t); 0079 0080 return true; 0081 } 0082 0083 } // namespace details 0084 0085 } // namespace soci 0086 0087 #endif // SOCI_PRIVATE_SOCI_CSTRTOI_H_INCLUDED