25d30c722d219817f94bf689d59e99420163bef6
[occt.git] / src / Standard / Standard_CLocaleSentry.hxx
1 // Created on: 2013-01-17
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Standard_CLocaleSentry_H__
17 #define _Standard_CLocaleSentry_H__
18
19 #include <Standard_Macro.hxx>
20
21 #include <locale.h>
22
23 #ifndef HAVE_XLOCALE_H
24   //! "xlocale.h" available in Mac OS X and glibc (Linux) for a long time as an extension
25   //! and become part of POSIX since '2008.
26   //! Notice that this is impossible to test (_POSIX_C_SOURCE >= 200809L)
27   //! since POSIX didn't declared such identifier.
28   #if defined(__APPLE__)
29     #define HAVE_XLOCALE_H
30   #endif
31
32   //! We check _GNU_SOURCE for glibc extensions here and it is always defined by g++ compiler.
33   #if defined(_GNU_SOURCE) && !defined(__ANDROID__)
34     #define HAVE_XLOCALE_H
35   #endif
36 #endif // ifndef HAVE_LOCALE_H
37
38 #ifdef HAVE_XLOCALE_H
39   #include <xlocale.h>
40 #endif
41
42 #if !defined(__ANDROID__)
43
44 //! This class intended to temporary switch C locale and logically equivalent to setlocale(LC_ALL, "C").
45 //! It is intended to format text regardless of user locale settings (for import/export functionality).
46 //! Thus following calls to sprintf, atoi and other functions will use "C" locale.
47 //! Destructor of this class will return original locale.
48 //!
49 //! Notice that this functionality is platfrom dependent and intended only to workaround alien code
50 //! that doesn't setup locale correctly.
51 //!
52 //! Internally you should prefer more portable C++ locale interfaces
53 //! or OCCT wrappers to some C functions like Sprintf, Atof, Strtod.
54 class Standard_CLocaleSentry
55 {
56 public:
57
58   //! Setup current C locale to "C".
59   Standard_EXPORT Standard_CLocaleSentry();
60
61   //! Restore previous locale.
62   Standard_EXPORT ~Standard_CLocaleSentry();
63
64 public:
65
66 #ifdef HAVE_XLOCALE_H
67   typedef  locale_t clocale_t;
68 #elif defined(_WIN32)
69   typedef _locale_t clocale_t;
70 #else
71   typedef void*     clocale_t;
72 #endif
73
74   //! @return locale "C" instance (locale_t within xlocale or _locale_t within Windows)
75   //! to be used for _l functions with locale argument.
76   static Standard_EXPORT clocale_t GetCLocale();
77
78 private:
79
80   void* myPrevLocale;       //!< previous locale, platform-dependent pointer!
81 #ifdef _WIN32
82   int   myPrevTLocaleState; //!< previous thread-locale state, MSVCRT-specific
83 #endif
84
85 private:
86
87   //! Copying disallowed
88   Standard_CLocaleSentry            (const Standard_CLocaleSentry& );
89   Standard_CLocaleSentry& operator= (const Standard_CLocaleSentry& );
90
91 };
92
93 #else
94
95 //! C/C++ runtime on Android currently supports only C-locale, no need to call anything.
96 class Standard_CLocaleSentry
97 {
98 public:
99   Standard_CLocaleSentry() {}
100   typedef void* clocale_t;
101   static clocale_t GetCLocale() { return 0; }
102 };
103
104 #endif // __ANDROID__
105
106 #endif // _Standard_CLocaleSentry_H__