0022898: IGES import fails in german environment
[occt.git] / src / Standard / Standard_CLocaleSentry.cxx
CommitLineData
91322f44 1// Created on: 2013-01-17
2// Created by: Kirill GAVRILOV
3// Copyright (c) 2013 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
20#ifdef HAVE_CONFIG_H
21 #include <config.h>
22#endif
23
24#include <Standard_CLocaleSentry.hxx>
25
26#include <Standard_TypeDef.hxx>
27
28#include <cstring>
29
30namespace
31{
32
33 //! CLocalePtr - static object representing C locale
34 class CLocalePtr
35 {
36 public:
37
38 CLocalePtr()
39 #ifdef HAVE_XLOCALE_H
40 : myLocale (newlocale (LC_ALL_MASK, "C", NULL))
41 #elif defined(_WIN32)
42 : myLocale (_create_locale (LC_ALL, "C"))
43 #else
44 : myLocale (NULL)
45 #endif
46 {}
47
48 ~CLocalePtr()
49 {
50 #ifdef HAVE_XLOCALE_H
51 freelocale (myLocale);
52 #elif defined(_WIN32)
53 _free_locale (myLocale);
54 #endif
55 }
56
57 public:
58
59 Standard_CLocaleSentry::clocale_t myLocale;
60
61 };
62
63 static CLocalePtr theCLocale;
64
65};
66
67// =======================================================================
68// function : GetCLocale
69// purpose :
70// =======================================================================
71Standard_CLocaleSentry::clocale_t Standard_CLocaleSentry::GetCLocale()
72{
73 return theCLocale.myLocale;
74}
75
76// =======================================================================
77// function : Standard_CLocaleSentry
78// purpose :
79// =======================================================================
80Standard_CLocaleSentry::Standard_CLocaleSentry()
81#ifdef HAVE_XLOCALE_H
82: myPrevLocale (uselocale (theCLocale.myLocale)) // switch to C locale within this thread only using xlocale API
83#else
84: myPrevLocale (setlocale (LC_ALL, 0))
85#if defined(_MSC_VER) && (_MSC_VER > 1400)
86, myPrevTLocaleState (_configthreadlocale (_ENABLE_PER_THREAD_LOCALE))
87#endif
88#endif
89{
90#ifndef HAVE_XLOCALE_H
91 const char* aPrevLocale = (const char* )myPrevLocale;
92 if (aPrevLocale[0] == 'C' && aPrevLocale[1] == '\0')
93 {
94 myPrevLocale = NULL; // already C locale
95 return;
96 }
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);
101
102 setlocale (LC_ALL, "C");
103#endif
104}
105
106// =======================================================================
107// function : ~Standard_CLocaleSentry
108// purpose :
109// =======================================================================
110Standard_CLocaleSentry::~Standard_CLocaleSentry()
111{
112#ifdef HAVE_XLOCALE_H
113 uselocale ((locale_t )myPrevLocale);
114#else
115 if (myPrevLocale != NULL)
116 {
117 const char* aPrevLocale = (const char* )myPrevLocale;
118 setlocale (LC_ALL, aPrevLocale);
119 delete[] aPrevLocale;
120 }
121#if defined(_MSC_VER) && (_MSC_VER > 1400)
122 if (myPrevTLocaleState != _ENABLE_PER_THREAD_LOCALE)
123 {
124 _configthreadlocale (myPrevTLocaleState);
125 }
126#endif
127#endif
128}