CFx SDK Documentation 2026 SP0
Loading...
Searching...
No Matches
OdSetLocale.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2024, 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 Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2024 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
51class FIRSTDLL_EXPORT OdSetLocale
52{
53public:
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 else
73 {
74 _category = 0;
75 }
76 }
77 ~OdSetLocale()
78 {
79 reset();
80 }
81 void set(int category, const OdString& loc)
82 {
83 ODA_ASSERT(_origLocale.isEmpty());
84 _category = category;
85
86 OdString origLocale = ::_wsetlocale(_category, 0);
87 if (origLocale.iCompare(loc) != 0 && ::_wsetlocale(_category, loc) != 0)
88 {
89 _origLocale = origLocale;
90 }
91 }
92 void reset()
93 {
94 if (!_origLocale.isEmpty())
95 {
96 _wsetlocale(_category, _origLocale);
97#ifdef _DEBUG
98 OdString loc = _wsetlocale(_category, 0);
99 ODA_ASSERT(!loc.iCompare(_origLocale));
100#endif
101 _origLocale.empty();
102 }
103 }
104
105private:
106 FIRSTDLL_EXPORT_STATIC static OdMutex _mutex;
107
108 OdMutexAutoLock _autoLock;
109 OdString _origLocale;
110 int _category;
111};
112
113#define SET_LOCALE(category, locale) OdSetLocale loc(category, locale);
114#define RESET_LOCALE() loc.reset();
115#else // TD_USE_LOCALE
116
117#define SET_LOCALE(category, locale)
118#define RESET_LOCALE()
119
120#endif // TD_USE_LOCALE
121
122
123#include "TD_PackPop.h"
124
125#endif // _OD_SETLOCALE_H_
126
#define ODA_ASSERT(exp)
Definition DebugStuff.h:57
#define OD_T(x)
OdString OdString
Definition OdString.h:1258
#define FIRSTDLL_EXPORT
Definition RootExport.h:39
#define FIRSTDLL_EXPORT_STATIC
Definition RootExport.h:40
int iCompare(const OdChar *otherString) const
Definition OdString.h:403
OdString & format(const OdChar *formatString,...)
const OdChar * c_str() const
Definition OdString.h:205