]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/resbund.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1997-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/05/97 aliu Fixed bug in chopLocale. Added scanForLocaleInFile
15 * based on code taken from scanForLocale. Added
16 * constructor which attempts to read resource bundle
17 * from a specific file, without searching other files.
18 * 02/11/97 aliu Added UErrorCode return values to constructors. Fixed
19 * infinite loops in scanForFile and scanForLocale.
20 * Modified getRawResourceData to not delete storage in
21 * localeData and resourceData which it doesn't own.
22 * Added Mac compatibility #ifdefs for tellp() and
24 * 03/04/97 aliu Modified to use ExpandingDataSink objects instead of
25 * the highly inefficient ostrstream objects.
26 * 03/13/97 aliu Rewrote to load in entire resource bundle and store
27 * it as a Hashtable of ResourceBundleData objects.
28 * Added state table to govern parsing of files.
29 * Modified to load locale index out of new file distinct
31 * 03/25/97 aliu Modified to support 2-d arrays, needed for timezone data.
32 * Added support for custom file suffixes. Again, needed
33 * to support timezone data. Improved error handling to
34 * detect duplicate tags and subtags.
35 * 04/07/97 aliu Fixed bug in getHashtableForLocale(). Fixed handling
36 * of failing UErrorCode values on entry to API methods.
37 * Fixed bugs in getArrayItem() for negative indices.
38 * 04/29/97 aliu Update to use new Hashtable deletion protocol.
39 * 05/06/97 aliu Flattened kTransitionTable for HP compiler.
40 * Fixed usage of CharString.
41 * 06/11/99 stephen Removed parsing of .txt files.
42 * Reworked to use new binary format.
44 * 06/14/99 stephen Removed methods taking a filename suffix.
45 * 06/22/99 stephen Added missing T_FileStream_close in parse()
46 * 11/09/99 weiv Added getLocale(), rewritten constructForLocale()
47 * March 2000 weiv complete overhaul.
48 ******************************************************************************
51 #include "unicode/utypes.h"
52 #include "unicode/resbund.h"
63 /*-----------------------------------------------------------------------------
64 * Implementation Notes
66 * Resource bundles are read in once, and thereafter cached.
67 * ResourceBundle statically keeps track of which files have been
68 * read, so we are guaranteed that each file is read at most once.
69 * Resource bundles can be loaded from different data directories and
70 * will be treated as distinct, even if they are for the same locale.
72 * Resource bundles are lightweight objects, which have pointers to
73 * one or more shared Hashtable objects containing all the data.
74 * Copying would be cheap, but there is no copy constructor, since
75 * there wasn't one in the original API.
77 * The ResourceBundle parsing mechanism is implemented as a transition
78 * network, for easy maintenance and modification. The network is
79 * implemented as a matrix (instead of in code) to make this even
80 * easier. The matrix contains Transition objects. Each Transition
81 * object describes a destination node and an action to take before
82 * moving to the destination node. The source node is encoded by the
83 * index of the object in the array that contains it. The pieces
84 * needed to understand the transition network are the enums for node
85 * IDs and actions, the parse() method, which walks through the
86 * network and implements the actions, and the network itself. The
87 * network guarantees certain conditions, for example, that a new
88 * resource will not be closed until one has been opened first; or
89 * that data will not be stored into a TaggedList until a TaggedList
90 * has been created. Nonetheless, the code in parse() does some
91 * consistency checks as it runs the network, and fails with an
92 * U_INTERNAL_PROGRAM_ERROR if one of these checks fails. If the input
93 * data has a bad format, an U_INVALID_FORMAT_ERROR is returned. If you
94 * see an U_INTERNAL_PROGRAM_ERROR the transition matrix has a bug in
97 * Old functionality of multiple locales in a single file is still
98 * supported. For this reason, LOCALE names override FILE names. If
99 * data for en_US is located in the en.txt file, once it is loaded,
100 * the code will not care where it came from (other than remembering
101 * which directory it came from). However, if there is an en_US
102 * resource in en_US.txt, that will take precedence. There is no
103 * limit to the number or type of resources that can be stored in a
104 * file, however, files are only searched in a specific way. If
105 * en_US_CA is requested, then first en_US_CA.txt is searched, then
106 * en_US.txt, then en.txt, then default.txt. So it only makes sense
107 * to put certain locales in certain files. In this example, it would
108 * be logical to put en_US_CA, en_US, and en into the en.txt file,
109 * since they would be found there if asked for. The extreme example
110 * is to place all locale resources into default.txt, which should
113 * Inheritance is implemented. For example, xx_YY_zz inherits as
114 * follows: xx_YY_zz, xx_YY, xx, default. Inheritance is implemented
115 * as an array of hashtables. There will be from 1 to 4 hashtables in
118 * Fallback files are implemented. The fallback pattern is Language
119 * Country Variant (LCV) -> LC -> L. Fallback is first done for the
120 * requested locale. Then it is done for the default locale, as
121 * returned by Locale::getDefault(). Then the special file
122 * default.txt is searched for the default locale. The overall FILE
123 * fallback path is LCV -> LC -> L -> dLCV -> dLC -> dL -> default.
125 * Note that although file name searching includes the default locale,
126 * once a ResourceBundle object is constructed, the inheritance path
127 * no longer includes the default locale. The path is LCV -> LC -> L
130 * File parsing is lazy. Nothing is parsed unless it is called for by
131 * someone. So when a ResourceBundle for xx_YY_zz is constructed,
132 * only that locale is parsed (along with anything else in the same
133 * file). Later, if the FooBar tag is asked for, and if it isn't
134 * found in xx_YY_zz, then xx_YY.txt will be parsed and checked, and
135 * so forth, until the chain is exhausted or the tag is found.
137 * Thread-safety is implemented around caches, both the cache that
138 * stores all the resouce data, and the cache that stores flags
139 * indicating whether or not a file has been visited. These caches
140 * delete their storage at static cleanup time, when the process
143 * ResourceBundle supports TableCollation as a special case. This
144 * involves having special ResourceBundle objects which DO own their
145 * data, since we don't want large collation rule strings in the
146 * ResourceBundle cache (these are already cached in the
147 * TableCollation cache). TableCollation files (.ctx files) have the
148 * same format as normal resource data files, with a different
149 * interpretation, from the standpoint of ResourceBundle. .ctx files
150 * are loaded into otherwise ordinary ResourceBundle objects. They
151 * don't inherit (that's implemented by TableCollation) and they own
152 * their data (as mentioned above). However, they still support
153 * possible multiple locales in a single .ctx file. (This is in
154 * practice a bad idea, since you only want the one locale you're
155 * looking for, and only one tag will be present
156 * ("CollationElements"), so you don't need an inheritance chain of
157 * multiple locales.) Up to 4 locale resources will be loaded from a
158 * .ctx file; everything after the first 4 is ignored (parsed and
159 * deleted). (Normal .txt files have no limit.) Instead of being
160 * loaded into the cache, and then looked up as needed, the locale
161 * resources are read straight into the ResourceBundle object.
163 * The Index, which used to reside in default.txt, has been moved to a
164 * new file, index.txt. This file contains a slightly modified format
165 * with the addition of the "InstalledLocales" tag; it looks like:
175 //-----------------------------------------------------------------------------
177 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ResourceBundle
)
179 ResourceBundle::ResourceBundle(UErrorCode
&err
)
180 :UObject(), fLocale(NULL
)
182 fResource
= ures_open(0, Locale::getDefault().getName(), &err
);
185 ResourceBundle::ResourceBundle(const ResourceBundle
&other
)
186 :UObject(other
), fLocale(NULL
)
188 UErrorCode status
= U_ZERO_ERROR
;
190 if (other
.fResource
) {
191 fResource
= ures_copyResb(0, other
.fResource
, &status
);
193 /* Copying a bad resource bundle */
198 ResourceBundle::ResourceBundle(UResourceBundle
*res
, UErrorCode
& err
)
199 :UObject(), fLocale(NULL
)
202 fResource
= ures_copyResb(0, res
, &err
);
204 /* Copying a bad resource bundle */
209 ResourceBundle::ResourceBundle(const char* path
, const Locale
& locale
, UErrorCode
& err
)
210 :UObject(), fLocale(NULL
)
212 fResource
= ures_open(path
, locale
.getName(), &err
);
216 ResourceBundle
& ResourceBundle::operator=(const ResourceBundle
& other
)
222 ures_close(fResource
);
225 if (fLocale
!= NULL
) {
229 UErrorCode status
= U_ZERO_ERROR
;
230 if (other
.fResource
) {
231 fResource
= ures_copyResb(0, other
.fResource
, &status
);
233 /* Copying a bad resource bundle */
239 ResourceBundle::~ResourceBundle()
242 ures_close(fResource
);
244 if(fLocale
!= NULL
) {
250 ResourceBundle::clone() const {
251 return new ResourceBundle(*this);
254 UnicodeString
ResourceBundle::getString(UErrorCode
& status
) const {
256 const UChar
*r
= ures_getString(fResource
, &len
, &status
);
257 return UnicodeString(TRUE
, r
, len
);
260 const uint8_t *ResourceBundle::getBinary(int32_t& len
, UErrorCode
& status
) const {
261 return ures_getBinary(fResource
, &len
, &status
);
264 const int32_t *ResourceBundle::getIntVector(int32_t& len
, UErrorCode
& status
) const {
265 return ures_getIntVector(fResource
, &len
, &status
);
268 uint32_t ResourceBundle::getUInt(UErrorCode
& status
) const {
269 return ures_getUInt(fResource
, &status
);
272 int32_t ResourceBundle::getInt(UErrorCode
& status
) const {
273 return ures_getInt(fResource
, &status
);
276 const char *ResourceBundle::getName(void) const {
277 return ures_getName(fResource
);
280 const char *ResourceBundle::getKey(void) const {
281 return ures_getKey(fResource
);
284 UResType
ResourceBundle::getType(void) const {
285 return ures_getType(fResource
);
288 int32_t ResourceBundle::getSize(void) const {
289 return ures_getSize(fResource
);
292 UBool
ResourceBundle::hasNext(void) const {
293 return ures_hasNext(fResource
);
296 void ResourceBundle::resetIterator(void) {
297 ures_resetIterator(fResource
);
300 ResourceBundle
ResourceBundle::getNext(UErrorCode
& status
) {
303 ures_initStackObject(&r
);
304 ures_getNextResource(fResource
, &r
, &status
);
305 ResourceBundle
res(&r
, status
);
306 if (U_SUCCESS(status
)) {
312 UnicodeString
ResourceBundle::getNextString(UErrorCode
& status
) {
314 const UChar
* r
= ures_getNextString(fResource
, &len
, 0, &status
);
315 return UnicodeString(TRUE
, r
, len
);
318 UnicodeString
ResourceBundle::getNextString(const char ** key
, UErrorCode
& status
) {
320 const UChar
* r
= ures_getNextString(fResource
, &len
, key
, &status
);
321 return UnicodeString(TRUE
, r
, len
);
324 ResourceBundle
ResourceBundle::get(int32_t indexR
, UErrorCode
& status
) const {
327 ures_initStackObject(&r
);
328 ures_getByIndex(fResource
, indexR
, &r
, &status
);
329 ResourceBundle
res(&r
, status
);
330 if (U_SUCCESS(status
)) {
336 UnicodeString
ResourceBundle::getStringEx(int32_t indexS
, UErrorCode
& status
) const {
338 const UChar
* r
= ures_getStringByIndex(fResource
, indexS
, &len
, &status
);
339 return UnicodeString(TRUE
, r
, len
);
342 ResourceBundle
ResourceBundle::get(const char* key
, UErrorCode
& status
) const {
345 ures_initStackObject(&r
);
346 ures_getByKey(fResource
, key
, &r
, &status
);
347 ResourceBundle
res(&r
, status
);
348 if (U_SUCCESS(status
)) {
354 ResourceBundle
ResourceBundle::getWithFallback(const char* key
, UErrorCode
& status
){
356 ures_initStackObject(&r
);
357 ures_getByKeyWithFallback(fResource
, key
, &r
, &status
);
358 ResourceBundle
res(&r
, status
);
359 if(U_SUCCESS(status
)){
364 UnicodeString
ResourceBundle::getStringEx(const char* key
, UErrorCode
& status
) const {
366 const UChar
* r
= ures_getStringByKey(fResource
, key
, &len
, &status
);
367 return UnicodeString(TRUE
, r
, len
);
371 ResourceBundle::getVersionNumber() const
373 return ures_getVersionNumberInternal(fResource
);
376 void ResourceBundle::getVersion(UVersionInfo versionInfo
) const {
377 ures_getVersion(fResource
, versionInfo
);
380 const Locale
&ResourceBundle::getLocale(void) const {
381 static UMutex gLocaleLock
;
382 Mutex
lock(&gLocaleLock
);
383 if (fLocale
!= NULL
) {
386 UErrorCode status
= U_ZERO_ERROR
;
387 const char *localeName
= ures_getLocaleInternal(fResource
, &status
);
388 ResourceBundle
*ncThis
= const_cast<ResourceBundle
*>(this);
389 ncThis
->fLocale
= new Locale(localeName
);
390 return ncThis
->fLocale
!= NULL
? *ncThis
->fLocale
: Locale::getDefault();
393 const Locale
ResourceBundle::getLocale(ULocDataLocaleType type
, UErrorCode
&status
) const
395 return ures_getLocaleByType(fResource
, type
, &status
);