c_locale.cc revision 256281
190075Sobrien// Wrapper for underlying C-language localization -*- C++ -*- 290075Sobrien 390075Sobrien// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 490075Sobrien// Free Software Foundation, Inc. 590075Sobrien// 690075Sobrien// This file is part of the GNU ISO C++ Library. This library is free 790075Sobrien// software; you can redistribute it and/or modify it under the 890075Sobrien// terms of the GNU General Public License as published by the 990075Sobrien// Free Software Foundation; either version 2, or (at your option) 1090075Sobrien// any later version. 1190075Sobrien 1290075Sobrien// This library is distributed in the hope that it will be useful, 1390075Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1490075Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1590075Sobrien// GNU General Public License for more details. 1690075Sobrien 1790075Sobrien// You should have received a copy of the GNU General Public License along 1890075Sobrien// with this library; see the file COPYING. If not, write to the Free 1990075Sobrien// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 2090075Sobrien// USA. 2190075Sobrien 2290075Sobrien// As a special exception, you may use this file as part of a free software 2390075Sobrien// library without restriction. Specifically, if other files instantiate 2490075Sobrien// templates or use macros or inline functions from this file, or you compile 2590075Sobrien// this file and link it with other files to produce an executable, this 2690075Sobrien// file does not by itself cause the resulting executable to be covered by 2790075Sobrien// the GNU General Public License. This exception does not however 2890075Sobrien// invalidate any other reasons why the executable file might be covered by 2990075Sobrien// the GNU General Public License. 3090075Sobrien 3190075Sobrien// 3290075Sobrien// ISO C++ 14882: 22.8 Standard locale categories. 3390075Sobrien// 3490075Sobrien 3590075Sobrien// Written by Benjamin Kosnik <bkoz@redhat.com> 3690075Sobrien 3790075Sobrien#include <cerrno> // For errno 3890075Sobrien#include <cmath> // For isinf, finite, finitef, fabs 3990075Sobrien#include <cstdlib> // For strof, strtold 4090075Sobrien#include <locale> 4190075Sobrien 4290075Sobrien#ifdef _GLIBCXX_HAVE_IEEEFP_H 4390075Sobrien#include <ieeefp.h> 4490075Sobrien#endif 4590075Sobrien 4690075Sobrien_GLIBCXX_BEGIN_NAMESPACE(std) 4790075Sobrien 4890075Sobrien // Specializations for all types used in num_get. 4990075Sobrien template<> 5090075Sobrien void 5190075Sobrien __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err, 5290075Sobrien const __c_locale&) 5390075Sobrien { 5490075Sobrien // Assumes __s formatted for "C" locale. 5590075Sobrien errno = 0; 5690075Sobrien char* __old = strdup(setlocale(LC_ALL, NULL)); 5790075Sobrien setlocale(LC_ALL, "C"); 5890075Sobrien char* __sanity; 5990075Sobrien#if defined(_GLIBCXX_HAVE_STRTOF) 6090075Sobrien float __f = strtof(__s, &__sanity); 6190075Sobrien#else 6290075Sobrien double __d = strtod(__s, &__sanity); 6390075Sobrien float __f = static_cast<float>(__d); 6490075Sobrien#ifdef _GLIBCXX_HAVE_FINITEF 6590075Sobrien if (!finitef (__f)) 6690075Sobrien errno = ERANGE; 6790075Sobrien#elif defined (_GLIBCXX_HAVE_FINITE) 6890075Sobrien if (!finite (static_cast<double> (__f))) 6990075Sobrien errno = ERANGE; 7090075Sobrien#elif defined (_GLIBCXX_HAVE_ISINF) 7190075Sobrien if (isinf (static_cast<double> (__f))) 7290075Sobrien errno = ERANGE; 7390075Sobrien#else 7490075Sobrien if (fabs(__d) > numeric_limits<float>::max()) 7590075Sobrien errno = ERANGE; 7690075Sobrien#endif 7790075Sobrien#endif 7890075Sobrien if (__sanity != __s && errno != ERANGE) 7990075Sobrien __v = __f; 8090075Sobrien else 8190075Sobrien __err |= ios_base::failbit; 8290075Sobrien setlocale(LC_ALL, __old); 8390075Sobrien free(__old); 8490075Sobrien } 8590075Sobrien 8690075Sobrien template<> 8790075Sobrien void 8890075Sobrien __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err, 8990075Sobrien const __c_locale&) 9090075Sobrien { 9190075Sobrien // Assumes __s formatted for "C" locale. 9290075Sobrien errno = 0; 9390075Sobrien char* __old = strdup(setlocale(LC_ALL, NULL)); 9490075Sobrien setlocale(LC_ALL, "C"); 9590075Sobrien char* __sanity; 9690075Sobrien double __d = strtod(__s, &__sanity); 9790075Sobrien if (__sanity != __s && errno != ERANGE) 9890075Sobrien __v = __d; 9990075Sobrien else 10090075Sobrien __err |= ios_base::failbit; 10190075Sobrien setlocale(LC_ALL, __old); 10290075Sobrien free(__old); 10390075Sobrien } 10490075Sobrien 10590075Sobrien template<> 10690075Sobrien void 10790075Sobrien __convert_to_v(const char* __s, long double& __v, 10890075Sobrien ios_base::iostate& __err, const __c_locale&) 10990075Sobrien { 11090075Sobrien // Assumes __s formatted for "C" locale. 11190075Sobrien errno = 0; 11290075Sobrien char* __old = strdup(setlocale(LC_ALL, NULL)); 11390075Sobrien setlocale(LC_ALL, "C"); 11490075Sobrien#if defined(_GLIBCXX_HAVE_STRTOLD) 11590075Sobrien char* __sanity; 11690075Sobrien long double __ld = strtold(__s, &__sanity); 11790075Sobrien if (__sanity != __s && errno != ERANGE) 11890075Sobrien __v = __ld; 11990075Sobrien#else 12090075Sobrien typedef char_traits<char>::int_type int_type; 12190075Sobrien long double __ld; 12290075Sobrien int __p = sscanf(__s, "%Lf", &__ld); 12390075Sobrien if (__p && static_cast<int_type>(__p) != char_traits<char>::eof() 12490075Sobrien && errno != ERANGE) 12590075Sobrien __v = __ld; 12690075Sobrien#endif 12790075Sobrien else 12890075Sobrien __err |= ios_base::failbit; 12990075Sobrien setlocale(LC_ALL, __old); 13090075Sobrien free(__old); 13190075Sobrien } 13290075Sobrien 13390075Sobrien void 13490075Sobrien locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s, 13590075Sobrien __c_locale) 13690075Sobrien { 13790075Sobrien // Currently, the generic model only supports the "C" locale. 13890075Sobrien // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html 13990075Sobrien __cloc = NULL; 14090075Sobrien if (strcmp(__s, "C")) 14190075Sobrien __throw_runtime_error(__N("locale::facet::_S_create_c_locale " 14290075Sobrien "name not valid")); 14390075Sobrien } 14490075Sobrien 14590075Sobrien void 14690075Sobrien locale::facet::_S_destroy_c_locale(__c_locale& __cloc) 14790075Sobrien { __cloc = NULL; } 14890075Sobrien 14990075Sobrien __c_locale 15090075Sobrien locale::facet::_S_clone_c_locale(__c_locale&) 15190075Sobrien { return __c_locale(); } 15290075Sobrien 15390075Sobrien_GLIBCXX_END_NAMESPACE 15490075Sobrien 15590075Sobrien_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 15690075Sobrien 15790075Sobrien const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] = 15890075Sobrien { 15990075Sobrien "LC_CTYPE", 16090075Sobrien "LC_NUMERIC", 16190075Sobrien "LC_TIME", 16290075Sobrien "LC_COLLATE", 16390075Sobrien "LC_MONETARY", 16490075Sobrien "LC_MESSAGES" 16590075Sobrien }; 16690075Sobrien 16790075Sobrien_GLIBCXX_END_NAMESPACE 16890075Sobrien 16990075Sobrien_GLIBCXX_BEGIN_NAMESPACE(std) 17090075Sobrien 17190075Sobrien const char* const* const locale::_S_categories = __gnu_cxx::category_names; 17290075Sobrien 17390075Sobrien_GLIBCXX_END_NAMESPACE 17490075Sobrien 17590075Sobrien// XXX GLIBCXX_ABI Deprecated 17690075Sobrien#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT 17790075Sobrien#define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \ 17890075Sobrien extern "C" void ldbl (void) __attribute__ ((alias (#dbl))) 17990075Sobrien_GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi); 18090075Sobrien#endif // _GLIBCXX_LONG_DOUBLE_COMPAT 18190075Sobrien