0024512: clang++ compiler complains about extra semicolon
[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//
973c2be1 5// This file is part of Open CASCADE Technology software library.
91322f44 6//
973c2be1 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.
91322f44 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
91322f44 15
16#ifdef HAVE_CONFIG_H
17 #include <config.h>
18#endif
19
20#include <Standard_CLocaleSentry.hxx>
21
22#include <Standard_TypeDef.hxx>
23
24#include <cstring>
25
26namespace
27{
28
29 //! CLocalePtr - static object representing C locale
30 class CLocalePtr
31 {
32 public:
33
34 CLocalePtr()
35 #ifdef HAVE_XLOCALE_H
36 : myLocale (newlocale (LC_ALL_MASK, "C", NULL))
37 #elif defined(_WIN32)
38 : myLocale (_create_locale (LC_ALL, "C"))
39 #else
40 : myLocale (NULL)
41 #endif
42 {}
43
44 ~CLocalePtr()
45 {
46 #ifdef HAVE_XLOCALE_H
47 freelocale (myLocale);
48 #elif defined(_WIN32)
49 _free_locale (myLocale);
50 #endif
51 }
52
53 public:
54
55 Standard_CLocaleSentry::clocale_t myLocale;
56
57 };
58
59 static CLocalePtr theCLocale;
60
5640d653 61}
91322f44 62
63// =======================================================================
64// function : GetCLocale
65// purpose :
66// =======================================================================
67Standard_CLocaleSentry::clocale_t Standard_CLocaleSentry::GetCLocale()
68{
69 return theCLocale.myLocale;
70}
71
72// =======================================================================
73// function : Standard_CLocaleSentry
74// purpose :
75// =======================================================================
76Standard_CLocaleSentry::Standard_CLocaleSentry()
77#ifdef HAVE_XLOCALE_H
78: myPrevLocale (uselocale (theCLocale.myLocale)) // switch to C locale within this thread only using xlocale API
79#else
80: myPrevLocale (setlocale (LC_ALL, 0))
81#if defined(_MSC_VER) && (_MSC_VER > 1400)
82, myPrevTLocaleState (_configthreadlocale (_ENABLE_PER_THREAD_LOCALE))
83#endif
84#endif
85{
86#ifndef HAVE_XLOCALE_H
87 const char* aPrevLocale = (const char* )myPrevLocale;
88 if (aPrevLocale[0] == 'C' && aPrevLocale[1] == '\0')
89 {
90 myPrevLocale = NULL; // already C locale
91 return;
92 }
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);
97
98 setlocale (LC_ALL, "C");
99#endif
100}
101
102// =======================================================================
103// function : ~Standard_CLocaleSentry
104// purpose :
105// =======================================================================
106Standard_CLocaleSentry::~Standard_CLocaleSentry()
107{
108#ifdef HAVE_XLOCALE_H
109 uselocale ((locale_t )myPrevLocale);
110#else
111 if (myPrevLocale != NULL)
112 {
113 const char* aPrevLocale = (const char* )myPrevLocale;
114 setlocale (LC_ALL, aPrevLocale);
115 delete[] aPrevLocale;
116 }
117#if defined(_MSC_VER) && (_MSC_VER > 1400)
118 if (myPrevTLocaleState != _ENABLE_PER_THREAD_LOCALE)
119 {
120 _configthreadlocale (myPrevTLocaleState);
121 }
122#endif
123#endif
124}