1 //===-- StringConvert.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 #include <stdlib.h> 12 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Host/StringConvert.h" 17 18 namespace lldb_private 19 { 20 namespace StringConvert 21 { 22 23 int32_t 24 ToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr) 25 { 26 if (s && s[0]) 27 { 28 char *end = nullptr; 29 const long sval = ::strtol (s, &end, base); 30 if (*end == '\0') 31 { 32 if (success_ptr) 33 *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN)); 34 return (int32_t)sval; // All characters were used, return the result 35 } 36 } 37 if (success_ptr) 38 *success_ptr = false; 39 return fail_value; 40 } 41 42 uint32_t 43 ToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr) 44 { 45 if (s && s[0]) 46 { 47 char *end = nullptr; 48 const unsigned long uval = ::strtoul (s, &end, base); 49 if (*end == '\0') 50 { 51 if (success_ptr) 52 *success_ptr = (uval <= UINT32_MAX); 53 return (uint32_t)uval; // All characters were used, return the result 54 } 55 } 56 if (success_ptr) 57 *success_ptr = false; 58 return fail_value; 59 } 60 61 int64_t 62 ToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr) 63 { 64 if (s && s[0]) 65 { 66 char *end = nullptr; 67 int64_t uval = ::strtoll (s, &end, base); 68 if (*end == '\0') 69 { 70 if (success_ptr) 71 *success_ptr = true; 72 return uval; // All characters were used, return the result 73 } 74 } 75 if (success_ptr) 76 *success_ptr = false; 77 return fail_value; 78 } 79 80 uint64_t 81 ToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr) 82 { 83 if (s && s[0]) 84 { 85 char *end = nullptr; 86 uint64_t uval = ::strtoull (s, &end, base); 87 if (*end == '\0') 88 { 89 if (success_ptr) 90 *success_ptr = true; 91 return uval; // All characters were used, return the result 92 } 93 } 94 if (success_ptr) *success_ptr = false; 95 return fail_value; 96 } 97 98 double 99 ToDouble (const char *s, double fail_value, bool *success_ptr) 100 { 101 if (s && s[0]) 102 { 103 char *end = nullptr; 104 double val = strtod (s, &end); 105 if (*end == '\0') 106 { 107 if (success_ptr) 108 *success_ptr = true; 109 return val; // All characters were used, return the result 110 } 111 } 112 if (success_ptr) 113 *success_ptr = false; 114 return fail_value; 115 } 116 } 117 } 118