0020716: Eliminate usage of "config.h" header file
[occt.git] / src / Standard / Standard_CLocaleSentry.cxx
CommitLineData
91322f44 1// Created on: 2013-01-17
2// Created by: Kirill GAVRILOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
91322f44 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
91322f44 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 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.
91322f44 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
91322f44 15
91322f44 16#include <Standard_CLocaleSentry.hxx>
17
18#include <Standard_TypeDef.hxx>
19
20#include <cstring>
21
22namespace
23{
24
25 //! CLocalePtr - static object representing C locale
26 class CLocalePtr
27 {
28 public:
29
30 CLocalePtr()
31 #ifdef HAVE_XLOCALE_H
32 : myLocale (newlocale (LC_ALL_MASK, "C", NULL))
33 #elif defined(_WIN32)
34 : myLocale (_create_locale (LC_ALL, "C"))
35 #else
36 : myLocale (NULL)
37 #endif
38 {}
39
40 ~CLocalePtr()
41 {
42 #ifdef HAVE_XLOCALE_H
43 freelocale (myLocale);
44 #elif defined(_WIN32)
45 _free_locale (myLocale);
46 #endif
47 }
48
49 public:
50
51 Standard_CLocaleSentry::clocale_t myLocale;
52
53 };
54
55 static CLocalePtr theCLocale;
56
5640d653 57}
91322f44 58
59// =======================================================================
60// function : GetCLocale
61// purpose :
62// =======================================================================
63Standard_CLocaleSentry::clocale_t Standard_CLocaleSentry::GetCLocale()
64{
65 return theCLocale.myLocale;
66}
67
68// =======================================================================
69// function : Standard_CLocaleSentry
70// purpose :
71// =======================================================================
72Standard_CLocaleSentry::Standard_CLocaleSentry()
73#ifdef HAVE_XLOCALE_H
74: myPrevLocale (uselocale (theCLocale.myLocale)) // switch to C locale within this thread only using xlocale API
75#else
76: myPrevLocale (setlocale (LC_ALL, 0))
77#if defined(_MSC_VER) && (_MSC_VER > 1400)
78, myPrevTLocaleState (_configthreadlocale (_ENABLE_PER_THREAD_LOCALE))
79#endif
80#endif
81{
82#ifndef HAVE_XLOCALE_H
83 const char* aPrevLocale = (const char* )myPrevLocale;
84 if (aPrevLocale[0] == 'C' && aPrevLocale[1] == '\0')
85 {
86 myPrevLocale = NULL; // already C locale
87 return;
88 }
89 // copy string as following setlocale calls may invalidate returned pointer
90 Standard_Size aLen = std::strlen (aPrevLocale) + 1;
91 myPrevLocale = new char[aLen];
92 memcpy (myPrevLocale, aPrevLocale, aLen);
93
94 setlocale (LC_ALL, "C");
95#endif
96}
97
98// =======================================================================
99// function : ~Standard_CLocaleSentry
100// purpose :
101// =======================================================================
102Standard_CLocaleSentry::~Standard_CLocaleSentry()
103{
104#ifdef HAVE_XLOCALE_H
105 uselocale ((locale_t )myPrevLocale);
106#else
107 if (myPrevLocale != NULL)
108 {
109 const char* aPrevLocale = (const char* )myPrevLocale;
110 setlocale (LC_ALL, aPrevLocale);
111 delete[] aPrevLocale;
112 }
113#if defined(_MSC_VER) && (_MSC_VER > 1400)
114 if (myPrevTLocaleState != _ENABLE_PER_THREAD_LOCALE)
115 {
116 _configthreadlocale (myPrevTLocaleState);
117 }
118#endif
119#endif
120}