1 // Created on: 2013-01-17
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
24 #include <Standard_CLocaleSentry.hxx>
26 #include <Standard_TypeDef.hxx>
33 //! CLocalePtr - static object representing C locale
40 : myLocale (newlocale (LC_ALL_MASK, "C", NULL))
42 : myLocale (_create_locale (LC_ALL, "C"))
51 freelocale (myLocale);
53 _free_locale (myLocale);
59 Standard_CLocaleSentry::clocale_t myLocale;
63 static CLocalePtr theCLocale;
67 // =======================================================================
68 // function : GetCLocale
70 // =======================================================================
71 Standard_CLocaleSentry::clocale_t Standard_CLocaleSentry::GetCLocale()
73 return theCLocale.myLocale;
76 // =======================================================================
77 // function : Standard_CLocaleSentry
79 // =======================================================================
80 Standard_CLocaleSentry::Standard_CLocaleSentry()
82 : myPrevLocale (uselocale (theCLocale.myLocale)) // switch to C locale within this thread only using xlocale API
84 : myPrevLocale (setlocale (LC_ALL, 0))
85 #if defined(_MSC_VER) && (_MSC_VER > 1400)
86 , myPrevTLocaleState (_configthreadlocale (_ENABLE_PER_THREAD_LOCALE))
90 #ifndef HAVE_XLOCALE_H
91 const char* aPrevLocale = (const char* )myPrevLocale;
92 if (aPrevLocale[0] == 'C' && aPrevLocale[1] == '\0')
94 myPrevLocale = NULL; // already C locale
97 // copy string as following setlocale calls may invalidate returned pointer
98 Standard_Size aLen = std::strlen (aPrevLocale) + 1;
99 myPrevLocale = new char[aLen];
100 memcpy (myPrevLocale, aPrevLocale, aLen);
102 setlocale (LC_ALL, "C");
106 // =======================================================================
107 // function : ~Standard_CLocaleSentry
109 // =======================================================================
110 Standard_CLocaleSentry::~Standard_CLocaleSentry()
112 #ifdef HAVE_XLOCALE_H
113 uselocale ((locale_t )myPrevLocale);
115 if (myPrevLocale != NULL)
117 const char* aPrevLocale = (const char* )myPrevLocale;
118 setlocale (LC_ALL, aPrevLocale);
119 delete[] aPrevLocale;
121 #if defined(_MSC_VER) && (_MSC_VER > 1400)
122 if (myPrevTLocaleState != _ENABLE_PER_THREAD_LOCALE)
124 _configthreadlocale (myPrevTLocaleState);