1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
WARNING: This file was generated by dkct.
Changes you make here will be lost if dkct is run again!
You should modify the original source and run dkct on it.
Original source: dk4loc.ctr
*/
/*
Copyright (C) 2015-2017, Dirk Krause
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file dk4loc.c The dk4loc module.
*/
#line 67 "dk4loc.ctr"
#include "dk4loc.h"
#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define STDLIB_H_INCLUDED 1
#endif
#if DK4_HAVE_STRING_H
#ifndef STRING_H_INCLUDED
#include <string.h>
#define STRING_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_CTYPE_H
#ifndef CTYPE_H_INCLUDED
#include <ctype.h>
#define CTYPE_H_INCLUDED 1
#endif
#endif
#ifndef DK4ENC_H_INCLUDED
#include "dk4enc.h"
#endif
#ifndef DK4STR8_H_INCLUDED
#include "dk4str8.h"
#endif
#if DK4_ON_WINDOWS
#ifndef DK4WREG8_H_INCLUDED
#include "dk4wreg8.h"
#endif
#ifndef DK4WREGC_H_INCLUDED
#include "dk4wregc.h"
#endif
#endif
#line 110 "dk4loc.ctr"
/** Keywords used by the module.
*/
static const char * const dk4loc_mod_kw_8[] = {
/* 0 */
"LANG",
/* 1 */
"utf8",
/* 2 */
"UTF-8",
/* 3 */
"Control Panel\\International",
/* 4 */
"LocaleName",
/* 5 */
"sLanguage",
NULL
#line 133 "dk4loc.ctr"
};
static
int
dk4loc_use_default(dkChar *lptr, size_t szl, dk4_er_t *erp)
{
int back = 0;
if ((NULL != lptr) && (2 < szl)) {
lptr[0] = dkT('e');
lptr[1] = dkT('n');
lptr[2] = dkT('\0');
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
return back;
}
int
dk4loc_get_settings(
dkChar *lang,
size_t szlang,
dkChar *reg,
size_t szreg,
int *enc,
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
char buf[64]; /* Buffer to process getenv result */
void *kptr; /* Registry key pointer */
dkChar *pd; /* Copy string */
char *p3; /* Region */
char *p4; /* Copy string */
size_t sz; /* String length */
int res; /* Result from RegQueryValueEx() */
int expand = 0; /* Flag: Expansion necessary. */
#else
char buf[64]; /* Buffer to process getenv result */
dkChar *pd; /* Copy string */
char *p1; /* Result from getenv */
char *p2; /* Encoding */
char *p3; /* Region */
char *p4; /* Copy string */
size_t sz; /* String length */
#endif
int back = 0;
#line 184 "dk4loc.ctr"
if ((NULL != lang) && (0 < szlang)) { *lang = dkT('\0'); }
if ((NULL != reg ) && (0 < szreg )) { *reg = dkT('\0'); }
if (NULL != enc) { *enc = DK4_ENCODING_ASCII; }
if ((NULL != lang) && (NULL != reg) && (NULL != enc)) {
if ((0 < szlang) && (0 < szreg)) {
#if DK4_ON_WINDOWS
#if DK4_CHAR_SIZE > 1
*enc = DK4_ENCODING_UTF16;
#else
*enc = DK4_ENCODING_ASCII;
#endif
kptr = dk4wreg_open_key_c8(DK4_WREGKEY_HKCU, dk4loc_mod_kw_8[3], 0, erp);
if (NULL != kptr) {
res = dk4wreg_get_string_c8(
kptr, dk4loc_mod_kw_8[4], buf, sizeof(buf), &expand, erp
);
if (0 != res) {
p3 = dk4str8_chr(buf, '-');
if (NULL != p3) { *(p3++) = '\0'; }
dk4str8_normalize(buf, NULL);
if (NULL != p3) { dk4str8_normalize(p3, NULL); }
sz = dk4str8_len(buf);
if (0 < sz) {
if (szlang > sz) {
/* strcpy(lang, buf); */
pd = lang; p4 = buf;
while('\0' != *p4) {
*(pd++) = (dkChar)dk4str8_tolower(*(p4++));
}
*pd = dkT('\0');
back = 1;
sz = dk4str8_len(p3);
if (0 < sz) {
if (szreg > sz) {
/* strcpy(reg, p3); */
pd = reg; p4 = p3;
while('\0' != *p4) {
#if 0
*(pd++) = (dkChar)(tolower((unsigned char)(*(p4++))));
#endif
*(pd++) = (dkChar)dk4str8_tolower(*(p4++));
}
*pd = dkT('\0');
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
back = 0;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
back = dk4loc_use_default(lang, szlang, erp);
}
} else {
res = dk4wreg_get_string_c8(
kptr, dk4loc_mod_kw_8[5], buf, sizeof(buf), &expand, erp
);
if (0 != res) {
dk4str8_normalize(buf, NULL);
sz = dk4str8_len(buf);
if (2 < sz) { buf[2] = '\0'; }
sz = dk4str8_len(buf);
if (0 < sz) {
if (szlang > sz) {
pd = lang; p4 = buf;
while ('\0' != *p4) {
*(pd++) = (dkChar)(tolower((unsigned char)(*(p4++))));
}
*pd = dkT('\0');
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
back = dk4loc_use_default(lang, szlang, erp);
}
} else {
back = dk4loc_use_default(lang, szlang, erp);
}
}
dk4wreg_close_key(kptr);
} else {
back = dk4loc_use_default(lang, szlang, erp);
}
#else
p1 = getenv(dk4loc_mod_kw_8[0]);
if (NULL != p1) {
#line 272 "dk4loc.ctr"
if (sizeof(buf) > dk4str8_len(p1)) {
#line 273 "dk4loc.ctr"
dk4str8_cpy_s(buf, sizeof(buf), p1, NULL);
p2 = dk4str8_chr(buf, '.');
if (NULL != p2) {
#line 276 "dk4loc.ctr"
*(p2++) = '\0';
#line 277 "dk4loc.ctr"
}
p3 = dk4str8_chr(buf, '_');
if (NULL != p3) {
#line 280 "dk4loc.ctr"
*(p3++) = '\0';
#line 281 "dk4loc.ctr"
}
#line 282 "dk4loc.ctr"
dk4str8_normalize(buf, NULL);
#line 283 "dk4loc.ctr"
if (NULL != p2) { dk4str8_normalize(p2, NULL); }
if (NULL != p3) { dk4str8_normalize(p3, NULL); }
sz = dk4str8_len(buf);
#line 286 "dk4loc.ctr"
if (0 < sz) {
if (szlang > sz) {
/* strcpy(lang, buf); */
pd = lang; p4 = buf;
while('\0' != *p4) {
*(pd++) = (dkChar)(tolower((unsigned char)(*(p4++))));
}
*pd = dkT('\0');
back = 1;
if (NULL != p3) {
sz = dk4str8_len(p3);
if (0 < sz) {
if (szreg > sz) {
/* strcpy(reg, p3); */
pd = reg; p4 = p3;
while('\0' != *p4) {
*(pd++) = (dkChar)(tolower((unsigned char)(*(p4++))));
}
*pd = dkT('\0');
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
back = 0;
}
}
}
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
#line 314 "dk4loc.ctr"
*enc = DK4_ENCODING_32;
#else
#line 317 "dk4loc.ctr"
*enc = DK4_ENCODING_UTF16;
#endif
#else
#line 321 "dk4loc.ctr"
*enc = DK4_ENCODING_ASCII;
if (NULL != p2) {
if (0 == dk4str8_casecmp(p2, dk4loc_mod_kw_8[1])) {
*enc = DK4_ENCODING_UTF8;
} else {
if (0 == dk4str8_casecmp(p2, dk4loc_mod_kw_8[2])) {
*enc = DK4_ENCODING_UTF8;
}
}
}
#endif
} else {
#line 333 "dk4loc.ctr"
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
#line 336 "dk4loc.ctr"
back = dk4loc_use_default(lang, szlang, erp);
}
} else {
#line 339 "dk4loc.ctr"
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
#line 342 "dk4loc.ctr"
back = dk4loc_use_default(lang, szlang, erp);
}
#endif
} else {
#line 346 "dk4loc.ctr"
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else {
#line 349 "dk4loc.ctr"
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
#line 351 "dk4loc.ctr"
return back;
}
|