1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24
25 /* ifndef guards are limited in their scope on purpose (to allow including this again with different INTLSTR_DOMAIN) */
26 #ifndef _intlstr_h_
27 #define _intlstr_h_
28
29 #include <pal/palcommon.h>
30
31 PAL_BEGIN_EXTERNC
32
33 /*
34 **==============================================================================
35 **
36 ** Intlstr - datatype for representing localized strings
37 **
38 **==============================================================================
39 */
40
41 typedef struct _Intlstr
42 {
43 krisbash 1.1 /* "public" fields */
44 _Null_terminated_
45 const PAL_Char* str; /* localized string */
46
47 /* "private" fields */
48 PAL_Boolean _formattedString;
49 } Intlstr;
50
51 #define Intlstr_Null {NULL, 0}
52
53 /*
54 **==============================================================================
55 **
56 ** Intlstr_Free function
57 **
58 **==============================================================================
59 */
60
61 PAL_INLINE void _Intlstr_FreeFormattedString(_Frees_ptr_opt_ PAL_Char* localizedString);
62
63 PAL_INLINE void Intlstr_Free(Intlstr localizedString)
64 krisbash 1.1 {
65 if (localizedString._formattedString)
66 {
67 _Intlstr_FreeFormattedString((PAL_Char*)localizedString.str);
68 }
69 }
70
71 /*
72 **==============================================================================
73 **
74 ** Internal helper functions
75 **
76 **==============================================================================
77 */
78
79 PAL_INLINE Intlstr _Intlstr_ReturnFixedString(const _In_opt_z_ PAL_Char* str)
80 {
81 Intlstr result;
82 result.str = str;
83 result._formattedString = PAL_FALSE;
84 return result;
85 krisbash 1.1 }
86
87 PAL_INLINE Intlstr _Intlstr_ReturnFormattedString(const _In_opt_z_ PAL_Char* str)
88 {
89 Intlstr result;
90 result.str = str;
91 result._formattedString = PAL_TRUE;
92 return result;
93 }
94
95 #if defined(_MSC_VER)
96
97 #include <windows.h> /* needed for LocalFree */
98
99 _Ret_z_
100 _Success_(return != NULL)
101 const PAL_Char* _Intlstr_GetString_LoadString(HINSTANCE hInstance, UINT uID);
102
103 _Ret_z_
104 _Success_(return != NULL)
105 PAL_Char* _Intlstr_FormatString_FormatMessage(HINSTANCE hInstance, UINT uID, ...);
106 krisbash 1.1
107 PAL_INLINE void _Intlstr_FreeFormattedString(_Frees_ptr_opt_ PAL_Char* localizedString)
108 {
109 /* LocalFree is marked as "obsolete" by "findapi" tool, but as explained in more details
110 in bug WinBlue #355130 it is ok to use LocalFree until FormatMessage is fixed */
111 LocalFree(localizedString);
112 }
113
114 #elif defined(CONFIG_ENABLE_GETTEXT)
115
116 _Ret_z_ \
117 _Check_return_ \
118 _Success_(return != NULL) \
119 const PAL_Char* _Intlstr_GetString_GetText(const char* domain_name, int id, const char* msgid);
120
121 _Ret_z_
122 _Success_(return != NULL)
123 PAL_Char* _Intlstr_FormatString_gettext_and_snprintf(const char* domain_name, int id, const char* msgid, ...);
124
125 PAL_INLINE void _Intlstr_FreeFormattedString(_Frees_ptr_opt_ PAL_Char* localizedString)
126 {
127 krisbash 1.1 PAL_Free(localizedString);
128 }
129
130 #else
131
132 #include <pal/format.h>
133
134 PAL_INLINE void _Intlstr_FreeFormattedString(_Frees_ptr_opt_ PAL_Char* localizedString)
135 {
136 PAL_Free(localizedString);
137 }
138
139 #endif
140
141 PAL_END_EXTERNC
142
143 #endif /* !defined(_intlstr_h_) */
144
145 /*
146 **==============================================================================
147 **
148 krisbash 1.1 ** Translating user-provided INTLSTR_DOMAIN into OS-specific _INTLSTR_DOMAIN
149 **
150 **==============================================================================
151 */
152
153 #if defined (_INTLSTR_DOMAIN)
154 #undef _INTLSTR_DOMAIN
155 #endif
156
157 #if defined(INTLSTR_DOMAIN)
158
159 #if defined(_MSC_VER)
160
161 #define _INTLSTR_DOMAIN (GetModuleHandleA(INTLSTR_DOMAIN))
162
163 #else
164
165 #define _INTLSTR_DOMAIN (INTLSTR_DOMAIN)
166
167 #endif
168
169 krisbash 1.1 #else
170
171 #if defined(_MSC_VER)
172
173 #define _INTLSTR_DOMAIN (GetModuleHandle(NULL))
174
175 #else
176
177 #define _INTLSTR_DOMAIN ("omi")
178
179 #endif
180
181 #endif
182
183 /*
184 **==============================================================================
185 **
186 ** Intlstr_Define0 macro for translating user-provided localized string definitions into OS-specific
187 ** Intlstr_<stringname> helper functions (written on top of _Intlstr_GetString_Xxx)
188 **
189 **==============================================================================
190 krisbash 1.1 */
191
192 #if defined(_MSC_VER)
193
194 #define Intlstr_Define0(id, name, text) \
195 _Check_return_ \
196 _Success_(return.str != NULL) \
197 PAL_INLINE Intlstr Intlstr_ ## name() \
198 { \
199 return _Intlstr_ReturnFixedString(_Intlstr_GetString_LoadString(_INTLSTR_DOMAIN, id)); \
200 }
201
202 #elif defined(CONFIG_ENABLE_GETTEXT)
203
204 #define Intlstr_Define0(id, name, text) \
205 _Check_return_ \
206 _Success_(return.str != NULL) \
207 PAL_INLINE Intlstr Intlstr_ ## name() \
208 { \
209 return _Intlstr_ReturnFixedString(_Intlstr_GetString_GetText(_INTLSTR_DOMAIN, id, text)); \
210 }
211 krisbash 1.1
212 #else
213
214 #define Intlstr_Define0(id, name, text) \
215 _Check_return_ \
216 _Success_(return.str != NULL) \
217 PAL_INLINE Intlstr Intlstr_ ## name() \
218 { \
219 return _Intlstr_ReturnFixedString(PAL_T(text)); \
220 }
221
222 #endif
223
224
225 /*
226 **==============================================================================
227 **
228 ** Intlstr_Define1/2/3 macros for translating user-provided localized string templates
229 ** into OS-specific Intlstr_<stringname> helper functions
230 **
231 **==============================================================================
232 krisbash 1.1 */
233
234 #if defined(_MSC_VER)
235
236 #define Intlstr_Define1(id, name, parameter1_type, parameter1_name, text) \
237 _Check_return_ \
238 _Success_(return.str != NULL) \
239 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name) \
240 { \
241 return _Intlstr_ReturnFormattedString(_Intlstr_FormatString_FormatMessage(_INTLSTR_DOMAIN, id, parameter1_name)); \
242 }
243
244 #define Intlstr_Define2(id, name, parameter1_type, parameter1_name, parameter2_type, parameter2_name, text) \
245 _Check_return_ \
246 _Success_(return.str != NULL) \
247 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name, parameter2_type parameter2_name) \
248 { \
249 return _Intlstr_ReturnFormattedString(_Intlstr_FormatString_FormatMessage(_INTLSTR_DOMAIN, id, parameter1_name, parameter2_name)); \
250 }
251
252 #define Intlstr_Define3(id, name, parameter1_type, parameter1_name, parameter2_type, parameter2_name, parameter3_type, parameter3_name, text) \
253 krisbash 1.1 _Check_return_ \
254 _Success_(return.str != NULL) \
255 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name, parameter2_type parameter2_name, parameter3_type parameter3_name) \
256 { \
257 return _Intlstr_ReturnFormattedString(_Intlstr_FormatString_FormatMessage(_INTLSTR_DOMAIN, id, parameter1_name, parameter2_name, parameter3_name)); \
258 }
259
260 #elif defined(CONFIG_ENABLE_GETTEXT)
261
262 #define Intlstr_Define1(id, name, parameter1_type, parameter1_name, text) \
263 _Check_return_ \
264 _Success_(return.str != NULL) \
265 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name) \
266 { \
267 return _Intlstr_ReturnFormattedString(_Intlstr_FormatString_gettext_and_snprintf(_INTLSTR_DOMAIN, id, text, parameter1_name)); \
268 }
269
270 #define Intlstr_Define2(id, name, parameter1_type, parameter1_name, parameter2_type, parameter2_name, text) \
271 _Check_return_ \
272 _Success_(return.str != NULL) \
273 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name, parameter2_type parameter2_name) \
274 krisbash 1.1 { \
275 return _Intlstr_ReturnFormattedString(_Intlstr_FormatString_gettext_and_snprintf(_INTLSTR_DOMAIN, id, text, parameter1_name, parameter2_name)); \
276 }
277
278 #define Intlstr_Define3(id, name, parameter1_type, parameter1_name, parameter2_type, parameter2_name, parameter3_type, parameter3_name, text) \
279 _Check_return_ \
280 _Success_(return.str != NULL) \
281 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name, parameter2_type parameter2_name, parameter3_type parameter3_name) \
282 { \
283 return _Intlstr_ReturnFormattedString(_Intlstr_FormatString_gettext_and_snprintf(_INTLSTR_DOMAIN, id, text, parameter1_name, parameter2_name, parameter3_name)); \
284 }
285
286 #else
287
288 #define Intlstr_Define1(id, name, parameter1_type, parameter1_name, text) \
289 _Check_return_ \
290 _Success_(return.str != NULL) \
291 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name) \
292 { \
293 return _Intlstr_ReturnFormattedString(Stprintf_StrDup(PAL_T(text), parameter1_name)); \
294 }
295 krisbash 1.1
296 #define Intlstr_Define2(id, name, parameter1_type, parameter1_name, parameter2_type, parameter2_name, text) \
297 _Check_return_ \
298 _Success_(return.str != NULL) \
299 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name, parameter2_type parameter2_name) \
300 { \
301 return _Intlstr_ReturnFormattedString(Stprintf_StrDup(PAL_T(text), parameter1_name, parameter2_name)); \
302 }
303
304 #define Intlstr_Define3(id, name, parameter1_type, parameter1_name, parameter2_type, parameter2_name, parameter3_type, parameter3_name, text) \
305 _Check_return_ \
306 _Success_(return.str != NULL) \
307 PAL_INLINE Intlstr Intlstr_ ## name(parameter1_type parameter1_name, parameter2_type parameter2_name, parameter3_type parameter3_name) \
308 { \
309 return _Intlstr_ReturnFormattedString(Stprintf_StrDup(PAL_T(text), parameter1_name, parameter2_name, parameter3_name)); \
310 }
311
312 #endif
313
314
|