1 // Created on: 2013-01-17
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <Standard_CLocaleSentry.hxx>
22 #include <Standard_TypeDef.hxx>
29 //! CLocalePtr - static object representing C locale
36 : myLocale (newlocale (LC_ALL_MASK, "C", NULL))
38 : myLocale (_create_locale (LC_ALL, "C"))
47 freelocale (myLocale);
49 _free_locale (myLocale);
55 Standard_CLocaleSentry::clocale_t myLocale;
59 static CLocalePtr theCLocale;
63 // =======================================================================
64 // function : GetCLocale
66 // =======================================================================
67 Standard_CLocaleSentry::clocale_t Standard_CLocaleSentry::GetCLocale()
69 return theCLocale.myLocale;
72 // =======================================================================
73 // function : Standard_CLocaleSentry
75 // =======================================================================
76 Standard_CLocaleSentry::Standard_CLocaleSentry()
78 : myPrevLocale (uselocale (theCLocale.myLocale)) // switch to C locale within this thread only using xlocale API
80 : myPrevLocale (setlocale (LC_ALL, 0))
81 #if defined(_MSC_VER) && (_MSC_VER > 1400)
82 , myPrevTLocaleState (_configthreadlocale (_ENABLE_PER_THREAD_LOCALE))
86 #ifndef HAVE_XLOCALE_H
87 const char* aPrevLocale = (const char* )myPrevLocale;
88 if (aPrevLocale[0] == 'C' && aPrevLocale[1] == '\0')
90 myPrevLocale = NULL; // already C locale
93 // copy string as following setlocale calls may invalidate returned pointer
94 Standard_Size aLen = std::strlen (aPrevLocale) + 1;
95 myPrevLocale = new char[aLen];
96 memcpy (myPrevLocale, aPrevLocale, aLen);
98 setlocale (LC_ALL, "C");
102 // =======================================================================
103 // function : ~Standard_CLocaleSentry
105 // =======================================================================
106 Standard_CLocaleSentry::~Standard_CLocaleSentry()
108 #ifdef HAVE_XLOCALE_H
109 uselocale ((locale_t )myPrevLocale);
111 if (myPrevLocale != NULL)
113 const char* aPrevLocale = (const char* )myPrevLocale;
114 setlocale (LC_ALL, aPrevLocale);
115 delete[] aPrevLocale;
117 #if defined(_MSC_VER) && (_MSC_VER > 1400)
118 if (myPrevTLocaleState != _ENABLE_PER_THREAD_LOCALE)
120 _configthreadlocale (myPrevTLocaleState);