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 | |
16 | #ifndef _Standard_CLocaleSentry_H__ |
17 | #define _Standard_CLocaleSentry_H__ |
18 | |
19 | #include <Standard_Macro.hxx> |
20 | |
21 | #include <locale.h> |
22 | |
59e353b7 |
23 | #if defined(__APPLE__) |
24 | #include <xlocale.h> |
25 | #endif |
26 | |
27 | #ifndef OCCT_CLOCALE_POSIX2008 |
28 | //! @def OCCT_CLOCALE_POSIX2008 |
29 | //! |
30 | //! POSIX.1-2008 extends C locale API by providing methods like newlocale/freelocale/uselocale. |
31 | //! Presence of this extension cannot be checked in straightforward way (like (_POSIX_C_SOURCE >= 200809L)) |
32 | //! due to missing such declarations in standard. |
33 | //! On macOS new functions are declared within "xlocale.h" header (the same is for glibc, but this header has been removed since glibc 2.26). |
0304f711 |
34 | #if defined(__APPLE__) |
59e353b7 |
35 | #define OCCT_CLOCALE_POSIX2008 |
0304f711 |
36 | #endif |
37 | |
38 | //! We check _GNU_SOURCE for glibc extensions here and it is always defined by g++ compiler. |
39 | #if defined(_GNU_SOURCE) && !defined(__ANDROID__) |
59e353b7 |
40 | #define OCCT_CLOCALE_POSIX2008 |
91322f44 |
41 | #endif |
42 | #endif |
43 | |
9bf6baed |
44 | #if !defined(__ANDROID__) |
45 | |
91322f44 |
46 | //! This class intended to temporary switch C locale and logically equivalent to setlocale(LC_ALL, "C"). |
47 | //! It is intended to format text regardless of user locale settings (for import/export functionality). |
48 | //! Thus following calls to sprintf, atoi and other functions will use "C" locale. |
49 | //! Destructor of this class will return original locale. |
50 | //! |
51 | //! Notice that this functionality is platfrom dependent and intended only to workaround alien code |
52 | //! that doesn't setup locale correctly. |
53 | //! |
54 | //! Internally you should prefer more portable C++ locale interfaces |
55 | //! or OCCT wrappers to some C functions like Sprintf, Atof, Strtod. |
56 | class Standard_CLocaleSentry |
57 | { |
58 | public: |
59 | |
60 | //! Setup current C locale to "C". |
61 | Standard_EXPORT Standard_CLocaleSentry(); |
62 | |
63 | //! Restore previous locale. |
9bf6baed |
64 | Standard_EXPORT ~Standard_CLocaleSentry(); |
91322f44 |
65 | |
66 | public: |
67 | |
59e353b7 |
68 | #ifdef OCCT_CLOCALE_POSIX2008 |
91322f44 |
69 | typedef locale_t clocale_t; |
e2c4ce48 |
70 | #elif defined(_MSC_VER) |
91322f44 |
71 | typedef _locale_t clocale_t; |
72 | #else |
73 | typedef void* clocale_t; |
74 | #endif |
75 | |
76 | //! @return locale "C" instance (locale_t within xlocale or _locale_t within Windows) |
77 | //! to be used for _l functions with locale argument. |
78 | static Standard_EXPORT clocale_t GetCLocale(); |
79 | |
80 | private: |
81 | |
82 | void* myPrevLocale; //!< previous locale, platform-dependent pointer! |
e2c4ce48 |
83 | #ifdef _MSC_VER |
91322f44 |
84 | int myPrevTLocaleState; //!< previous thread-locale state, MSVCRT-specific |
85 | #endif |
86 | |
87 | private: |
88 | |
89 | //! Copying disallowed |
90 | Standard_CLocaleSentry (const Standard_CLocaleSentry& ); |
91 | Standard_CLocaleSentry& operator= (const Standard_CLocaleSentry& ); |
92 | |
93 | }; |
94 | |
9bf6baed |
95 | #else |
96 | |
97 | //! C/C++ runtime on Android currently supports only C-locale, no need to call anything. |
98 | class Standard_CLocaleSentry |
99 | { |
100 | public: |
101 | Standard_CLocaleSentry() {} |
102 | typedef void* clocale_t; |
103 | static clocale_t GetCLocale() { return 0; } |
104 | }; |
105 | |
106 | #endif // __ANDROID__ |
107 | |
91322f44 |
108 | #endif // _Standard_CLocaleSentry_H__ |