CFx SDK Documentation  2022 SP0
OdSetLocale.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 
25 
26 #ifndef _OD_SETLOCALE_H_
27 #define _OD_SETLOCALE_H_
28 
29 #include "TD_PackPush.h"
30 
31 
32 #if (defined(_WIN32) || defined(WIN64)) && !defined(_WIN32_WCE)
33  #define TD_USE_LOCALE
34 #endif
35 
36 
37 #ifdef TD_USE_LOCALE
38 
39 #include <locale.h>
40 #include "OdString.h"
41 #include "OdMutex.h"
42 
51 class FIRSTDLL_EXPORT OdSetLocale
52 {
53 public:
58  OdSetLocale(int category, const OdString& locale)
59  : _autoLock(_mutex)
60  {
61  set(category, locale);
62  }
63  OdSetLocale(int category, int ansiCp)
64  : _autoLock(_mutex)
65  {
66  if (ansiCp)
67  {
68  OdString loc;
69  loc.format(OD_T(".%d"),ansiCp);
70  set(category, loc.c_str());
71  }
72  }
73  ~OdSetLocale()
74  {
75  reset();
76  }
77  void set(int category, const OdString& loc)
78  {
79  ODA_ASSERT(_origLocale.isEmpty());
80  _category = category;
81 
82  OdString origLocale = ::_wsetlocale(_category, 0);
83  if (origLocale.iCompare(loc) != 0 && ::_wsetlocale(_category, loc) != 0)
84  {
85  _origLocale = origLocale;
86  }
87  }
88  void reset()
89  {
90  if (!_origLocale.isEmpty())
91  {
92  _wsetlocale(_category, _origLocale);
93 #ifdef _DEBUG
94  OdString loc = _wsetlocale(_category, 0);
95  ODA_ASSERT(!loc.iCompare(_origLocale));
96 #endif
97  _origLocale.empty();
98  }
99  }
100 
101 private:
102  FIRSTDLL_EXPORT_STATIC static OdMutex _mutex;
103 
104  OdMutexAutoLock _autoLock;
105  OdString _origLocale;
106  int _category;
107 };
108 
109 #define SET_LOCALE(category, locale) OdSetLocale loc(category, locale);
110 #define RESET_LOCALE() loc.reset();
111 #else // TD_USE_LOCALE
112 
113 #define SET_LOCALE(category, locale)
114 #define RESET_LOCALE()
115 
116 #endif // TD_USE_LOCALE
117 
118 
119 #include "TD_PackPop.h"
120 
121 #endif // _OD_SETLOCALE_H_
122 
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
#define OD_T(x)
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
#define FIRSTDLL_EXPORT_STATIC
Definition: RootExport.h:40
const OdChar * c_str() const
Definition: OdString.h:200
int iCompare(const OdChar *otherString) const
Definition: OdString.h:394
OdString & format(const OdChar *formatString,...)