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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
**********************************************************************
* Copyright (C) 1997-2006, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*
* File resbund.cpp
*
* Modification History:
*
* Date Name Description
* 02/05/97 aliu Fixed bug in chopLocale. Added scanForLocaleInFile
* based on code taken from scanForLocale. Added
* constructor which attempts to read resource bundle
* from a specific file, without searching other files.
* 02/11/97 aliu Added UErrorCode return values to constructors. Fixed
* infinite loops in scanForFile and scanForLocale.
* Modified getRawResourceData to not delete storage in
* localeData and resourceData which it doesn't own.
* Added Mac compatibility #ifdefs for tellp() and
* ios::nocreate.
* 03/04/97 aliu Modified to use ExpandingDataSink objects instead of
* the highly inefficient ostrstream objects.
* 03/13/97 aliu Rewrote to load in entire resource bundle and store
* it as a Hashtable of ResourceBundleData objects.
* Added state table to govern parsing of files.
* Modified to load locale index out of new file distinct
* from default.txt.
* 03/25/97 aliu Modified to support 2-d arrays, needed for timezone data.
* Added support for custom file suffixes. Again, needed
* to support timezone data. Improved error handling to
* detect duplicate tags and subtags.
* 04/07/97 aliu Fixed bug in getHashtableForLocale(). Fixed handling
* of failing UErrorCode values on entry to API methods.
* Fixed bugs in getArrayItem() for negative indices.
* 04/29/97 aliu Update to use new Hashtable deletion protocol.
* 05/06/97 aliu Flattened kTransitionTable for HP compiler.
* Fixed usage of CharString.
* 06/11/99 stephen Removed parsing of .txt files.
* Reworked to use new binary format.
* Cleaned up.
* 06/14/99 stephen Removed methods taking a filename suffix.
* 06/22/99 stephen Added missing T_FileStream_close in parse()
* 11/09/99 weiv Added getLocale(), rewritten constructForLocale()
* March 2000 weiv complete overhaul.
******************************************************************************
*/
#include "unicode/utypes.h"
#include "unicode/resbund.h"
#include "umutex.h"
#include "uresimp.h"
U_NAMESPACE_BEGIN
/*-----------------------------------------------------------------------------
* Implementation Notes
*
* Resource bundles are read in once, and thereafter cached.
* ResourceBundle statically keeps track of which files have been
* read, so we are guaranteed that each file is read at most once.
* Resource bundles can be loaded from different data directories and
* will be treated as distinct, even if they are for the same locale.
*
* Resource bundles are lightweight objects, which have pointers to
* one or more shared Hashtable objects containing all the data.
* Copying would be cheap, but there is no copy constructor, since
* there wasn't one in the original API.
*
* The ResourceBundle parsing mechanism is implemented as a transition
* network, for easy maintenance and modification. The network is
* implemented as a matrix (instead of in code) to make this even
* easier. The matrix contains Transition objects. Each Transition
* object describes a destination node and an action to take before
* moving to the destination node. The source node is encoded by the
* index of the object in the array that contains it. The pieces
* needed to understand the transition network are the enums for node
* IDs and actions, the parse() method, which walks through the
* network and implements the actions, and the network itself. The
* network guarantees certain conditions, for example, that a new
* resource will not be closed until one has been opened first; or
* that data will not be stored into a TaggedList until a TaggedList
* has been created. Nonetheless, the code in parse() does some
* consistency checks as it runs the network, and fails with an
* U_INTERNAL_PROGRAM_ERROR if one of these checks fails. If the input
* data has a bad format, an U_INVALID_FORMAT_ERROR is returned. If you
* see an U_INTERNAL_PROGRAM_ERROR the transition matrix has a bug in
* it.
*
* Old functionality of multiple locales in a single file is still
* supported. For this reason, LOCALE names override FILE names. If
* data for en_US is located in the en.txt file, once it is loaded,
* the code will not care where it came from (other than remembering
* which directory it came from). However, if there is an en_US
* resource in en_US.txt, that will take precedence. There is no
* limit to the number or type of resources that can be stored in a
* file, however, files are only searched in a specific way. If
* en_US_CA is requested, then first en_US_CA.txt is searched, then
* en_US.txt, then en.txt, then default.txt. So it only makes sense
* to put certain locales in certain files. In this example, it would
* be logical to put en_US_CA, en_US, and en into the en.txt file,
* since they would be found there if asked for. The extreme example
* is to place all locale resources into default.txt, which should
* also work.
*
* Inheritance is implemented. For example, xx_YY_zz inherits as
* follows: xx_YY_zz, xx_YY, xx, default. Inheritance is implemented
* as an array of hashtables. There will be from 1 to 4 hashtables in
* the array.
*
* Fallback files are implemented. The fallback pattern is Language
* Country Variant (LCV) -> LC -> L. Fallback is first done for the
* requested locale. Then it is done for the default locale, as
* returned by Locale::getDefault(). Then the special file
* default.txt is searched for the default locale. The overall FILE
* fallback path is LCV -> LC -> L -> dLCV -> dLC -> dL -> default.
*
* Note that although file name searching includes the default locale,
* once a ResourceBundle object is constructed, the inheritance path
* no longer includes the default locale. The path is LCV -> LC -> L
* -> default.
*
* File parsing is lazy. Nothing is parsed unless it is called for by
* someone. So when a ResourceBundle for xx_YY_zz is constructed,
* only that locale is parsed (along with anything else in the same
* file). Later, if the FooBar tag is asked for, and if it isn't
* found in xx_YY_zz, then xx_YY.txt will be parsed and checked, and
* so forth, until the chain is exhausted or the tag is found.
*
* Thread-safety is implemented around caches, both the cache that
* stores all the resouce data, and the cache that stores flags
* indicating whether or not a file has been visited. These caches
* delete their storage at static cleanup time, when the process
* quits.
*
* ResourceBundle supports TableCollation as a special case. This
* involves having special ResourceBundle objects which DO own their
* data, since we don't want large collation rule strings in the
* ResourceBundle cache (these are already cached in the
* TableCollation cache). TableCollation files (.ctx files) have the
* same format as normal resource data files, with a different
* interpretation, from the standpoint of ResourceBundle. .ctx files
* are loaded into otherwise ordinary ResourceBundle objects. They
* don't inherit (that's implemented by TableCollation) and they own
* their data (as mentioned above). However, they still support
* possible multiple locales in a single .ctx file. (This is in
* practice a bad idea, since you only want the one locale you're
* looking for, and only one tag will be present
* ("CollationElements"), so you don't need an inheritance chain of
* multiple locales.) Up to 4 locale resources will be loaded from a
* .ctx file; everything after the first 4 is ignored (parsed and
* deleted). (Normal .txt files have no limit.) Instead of being
* loaded into the cache, and then looked up as needed, the locale
* resources are read straight into the ResourceBundle object.
*
* The Index, which used to reside in default.txt, has been moved to a
* new file, index.txt. This file contains a slightly modified format
* with the addition of the "InstalledLocales" tag; it looks like:
*
* Index {
* InstalledLocales {
* ar
* ..
* zh_TW
* }
* }
*/
//-----------------------------------------------------------------------------
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ResourceBundle)
ResourceBundle::ResourceBundle(UErrorCode &err)
:UObject(), fLocale(NULL)
{
fResource = ures_open(0, Locale::getDefault().getName(), &err);
}
ResourceBundle::ResourceBundle(const ResourceBundle &other)
:UObject(other), fLocale(NULL)
{
UErrorCode status = U_ZERO_ERROR;
if (other.fResource) {
fResource = ures_copyResb(0, other.fResource, &status);
} else {
/* Copying a bad resource bundle */
fResource = NULL;
}
}
ResourceBundle::ResourceBundle(UResourceBundle *res, UErrorCode& err)
:UObject(), fLocale(NULL)
{
if (res) {
fResource = ures_copyResb(0, res, &err);
} else {
/* Copying a bad resource bundle */
fResource = NULL;
}
}
ResourceBundle::ResourceBundle(const char* path, const Locale& locale, UErrorCode& err)
:UObject(), fLocale(NULL)
{
fResource = ures_open(path, locale.getName(), &err);
}
ResourceBundle& ResourceBundle::operator=(const ResourceBundle& other)
{
if(this == &other) {
return *this;
}
if(fResource != 0) {
ures_close(fResource);
fResource = NULL;
}
UErrorCode status = U_ZERO_ERROR;
if (other.fResource) {
fResource = ures_copyResb(0, other.fResource, &status);
} else {
/* Copying a bad resource bundle */
fResource = NULL;
}
return *this;
}
ResourceBundle::~ResourceBundle()
{
if(fResource != 0) {
ures_close(fResource);
}
if(fLocale != NULL) {
delete(fLocale);
}
}
ResourceBundle *
ResourceBundle::clone() const {
return new ResourceBundle(*this);
}
UnicodeString ResourceBundle::getString(UErrorCode& status) const {
int32_t len = 0;
const UChar *r = ures_getString(fResource, &len, &status);
return UnicodeString(TRUE, r, len);
}
const uint8_t *ResourceBundle::getBinary(int32_t& len, UErrorCode& status) const {
return ures_getBinary(fResource, &len, &status);
}
const int32_t *ResourceBundle::getIntVector(int32_t& len, UErrorCode& status) const {
return ures_getIntVector(fResource, &len, &status);
}
uint32_t ResourceBundle::getUInt(UErrorCode& status) const {
return ures_getUInt(fResource, &status);
}
int32_t ResourceBundle::getInt(UErrorCode& status) const {
return ures_getInt(fResource, &status);
}
const char *ResourceBundle::getName(void) const {
return ures_getName(fResource);
}
const char *ResourceBundle::getKey(void) const {
return ures_getKey(fResource);
}
UResType ResourceBundle::getType(void) const {
return ures_getType(fResource);
}
int32_t ResourceBundle::getSize(void) const {
return ures_getSize(fResource);
}
UBool ResourceBundle::hasNext(void) const {
return ures_hasNext(fResource);
}
void ResourceBundle::resetIterator(void) {
ures_resetIterator(fResource);
}
ResourceBundle ResourceBundle::getNext(UErrorCode& status) {
UResourceBundle r;
ures_initStackObject(&r);
ures_getNextResource(fResource, &r, &status);
ResourceBundle res(&r, status);
if (U_SUCCESS(status)) {
ures_close(&r);
}
return res;
}
UnicodeString ResourceBundle::getNextString(UErrorCode& status) {
int32_t len = 0;
const UChar* r = ures_getNextString(fResource, &len, 0, &status);
return UnicodeString(TRUE, r, len);
}
UnicodeString ResourceBundle::getNextString(const char ** key, UErrorCode& status) {
int32_t len = 0;
const UChar* r = ures_getNextString(fResource, &len, key, &status);
return UnicodeString(TRUE, r, len);
}
ResourceBundle ResourceBundle::get(int32_t indexR, UErrorCode& status) const {
UResourceBundle r;
ures_initStackObject(&r);
ures_getByIndex(fResource, indexR, &r, &status);
ResourceBundle res(&r, status);
if (U_SUCCESS(status)) {
ures_close(&r);
}
return res;
}
UnicodeString ResourceBundle::getStringEx(int32_t indexS, UErrorCode& status) const {
int32_t len = 0;
const UChar* r = ures_getStringByIndex(fResource, indexS, &len, &status);
return UnicodeString(TRUE, r, len);
}
ResourceBundle ResourceBundle::get(const char* key, UErrorCode& status) const {
UResourceBundle r;
ures_initStackObject(&r);
ures_getByKey(fResource, key, &r, &status);
ResourceBundle res(&r, status);
if (U_SUCCESS(status)) {
ures_close(&r);
}
return res;
}
ResourceBundle ResourceBundle::getWithFallback(const char* key, UErrorCode& status){
UResourceBundle r;
ures_initStackObject(&r);
ures_getByKeyWithFallback(fResource, key, &r, &status);
ResourceBundle res(&r, status);
if(U_SUCCESS(status)){
ures_close(&r);
}
return res;
}
UnicodeString ResourceBundle::getStringEx(const char* key, UErrorCode& status) const {
int32_t len = 0;
const UChar* r = ures_getStringByKey(fResource, key, &len, &status);
return UnicodeString(TRUE, r, len);
}
const char*
ResourceBundle::getVersionNumber() const
{
return ures_getVersionNumber(fResource);
}
void ResourceBundle::getVersion(UVersionInfo versionInfo) const {
ures_getVersion(fResource, versionInfo);
}
const Locale &ResourceBundle::getLocale(void) const
{
UBool needInit;
UMTX_CHECK(NULL, (fLocale == NULL), needInit);
if(needInit) {
UErrorCode status = U_ZERO_ERROR;
const char *localeName = ures_getLocale(fResource, &status);
Locale *tLocale = new Locale(localeName);
umtx_lock(NULL);
ResourceBundle *me = (ResourceBundle *)this; // semantically const
if (me->fLocale == NULL) {
me->fLocale = tLocale;
tLocale = NULL;
}
umtx_unlock(NULL);
delete tLocale;
}
return *fLocale;
}
const Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const
{
return ures_getLocaleByType(fResource, type, &status);
}
//eof
U_NAMESPACE_END
|