1e4b17023SJohn Marino // String Conversions -*- C++ -*- 2e4b17023SJohn Marino 3*5ce9237cSJohn Marino // Copyright (C) 2008, 2009, 2010, 2012 Free Software Foundation, Inc. 4e4b17023SJohn Marino // 5e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free 6e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the 7e4b17023SJohn Marino // terms of the GNU General Public License as published by the 8e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option) 9e4b17023SJohn Marino // any later version. 10e4b17023SJohn Marino 11e4b17023SJohn Marino // This library is distributed in the hope that it will be useful, 12e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of 13e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14e4b17023SJohn Marino // GNU General Public License for more details. 15e4b17023SJohn Marino 16e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional 17e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version 18e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation. 19e4b17023SJohn Marino 20e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and 21e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program; 22e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23e4b17023SJohn Marino // <http://www.gnu.org/licenses/>. 24e4b17023SJohn Marino 25e4b17023SJohn Marino /** @file ext/string_conversions.h 26e4b17023SJohn Marino * This file is a GNU extension to the Standard C++ Library. 27e4b17023SJohn Marino */ 28e4b17023SJohn Marino 29e4b17023SJohn Marino #ifndef _STRING_CONVERSIONS_H 30e4b17023SJohn Marino #define _STRING_CONVERSIONS_H 1 31e4b17023SJohn Marino 32e4b17023SJohn Marino #pragma GCC system_header 33e4b17023SJohn Marino 34*5ce9237cSJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__ 35*5ce9237cSJohn Marino # include <bits/c++0x_warning.h> 36*5ce9237cSJohn Marino #else 37*5ce9237cSJohn Marino 38e4b17023SJohn Marino #include <bits/c++config.h> 39e4b17023SJohn Marino #include <ext/numeric_traits.h> 40e4b17023SJohn Marino #include <bits/functexcept.h> 41e4b17023SJohn Marino #include <cstdlib> 42e4b17023SJohn Marino #include <cwchar> 43e4b17023SJohn Marino #include <cstdio> 44e4b17023SJohn Marino #include <cerrno> 45e4b17023SJohn Marino 46e4b17023SJohn Marino namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 47e4b17023SJohn Marino { 48e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION 49e4b17023SJohn Marino 50e4b17023SJohn Marino // Helper for all the sto* functions. 51e4b17023SJohn Marino template<typename _TRet, typename _Ret = _TRet, typename _CharT, 52e4b17023SJohn Marino typename... _Base> 53e4b17023SJohn Marino _Ret 54e4b17023SJohn Marino __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...), 55e4b17023SJohn Marino const char* __name, const _CharT* __str, std::size_t* __idx, 56e4b17023SJohn Marino _Base... __base) 57e4b17023SJohn Marino { 58e4b17023SJohn Marino _Ret __ret; 59e4b17023SJohn Marino 60e4b17023SJohn Marino _CharT* __endptr; 61e4b17023SJohn Marino errno = 0; 62e4b17023SJohn Marino const _TRet __tmp = __convf(__str, &__endptr, __base...); 63e4b17023SJohn Marino 64e4b17023SJohn Marino if (__endptr == __str) 65e4b17023SJohn Marino std::__throw_invalid_argument(__name); 66e4b17023SJohn Marino else if (errno == ERANGE 67e4b17023SJohn Marino || (std::__are_same<_Ret, int>::__value 68e4b17023SJohn Marino && (__tmp < __numeric_traits<int>::__min 69e4b17023SJohn Marino || __tmp > __numeric_traits<int>::__max))) 70e4b17023SJohn Marino std::__throw_out_of_range(__name); 71e4b17023SJohn Marino else 72e4b17023SJohn Marino __ret = __tmp; 73e4b17023SJohn Marino 74e4b17023SJohn Marino if (__idx) 75e4b17023SJohn Marino *__idx = __endptr - __str; 76e4b17023SJohn Marino 77e4b17023SJohn Marino return __ret; 78e4b17023SJohn Marino } 79e4b17023SJohn Marino 80e4b17023SJohn Marino // Helper for the to_string / to_wstring functions. 81e4b17023SJohn Marino template<typename _String, typename _CharT = typename _String::value_type> 82e4b17023SJohn Marino _String 83e4b17023SJohn Marino __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*, 84e4b17023SJohn Marino __builtin_va_list), std::size_t __n, 85e4b17023SJohn Marino const _CharT* __fmt, ...) 86e4b17023SJohn Marino { 87e4b17023SJohn Marino // XXX Eventually the result will be constructed in place in 88e4b17023SJohn Marino // the C++0x string, likely with the help of internal hooks. 89e4b17023SJohn Marino _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 90e4b17023SJohn Marino * __n)); 91e4b17023SJohn Marino 92e4b17023SJohn Marino __builtin_va_list __args; 93e4b17023SJohn Marino __builtin_va_start(__args, __fmt); 94e4b17023SJohn Marino 95e4b17023SJohn Marino const int __len = __convf(__s, __n, __fmt, __args); 96e4b17023SJohn Marino 97e4b17023SJohn Marino __builtin_va_end(__args); 98e4b17023SJohn Marino 99e4b17023SJohn Marino return _String(__s, __s + __len); 100e4b17023SJohn Marino } 101e4b17023SJohn Marino 102e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION 103e4b17023SJohn Marino } // namespace 104e4b17023SJohn Marino 105*5ce9237cSJohn Marino #endif // C++11 106*5ce9237cSJohn Marino 107e4b17023SJohn Marino #endif // _STRING_CONVERSIONS_H 108