intl/icu/source/common/resbund.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 1997-2013, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 *
michael@0 7 * File resbund.cpp
michael@0 8 *
michael@0 9 * Modification History:
michael@0 10 *
michael@0 11 * Date Name Description
michael@0 12 * 02/05/97 aliu Fixed bug in chopLocale. Added scanForLocaleInFile
michael@0 13 * based on code taken from scanForLocale. Added
michael@0 14 * constructor which attempts to read resource bundle
michael@0 15 * from a specific file, without searching other files.
michael@0 16 * 02/11/97 aliu Added UErrorCode return values to constructors. Fixed
michael@0 17 * infinite loops in scanForFile and scanForLocale.
michael@0 18 * Modified getRawResourceData to not delete storage in
michael@0 19 * localeData and resourceData which it doesn't own.
michael@0 20 * Added Mac compatibility #ifdefs for tellp() and
michael@0 21 * ios::nocreate.
michael@0 22 * 03/04/97 aliu Modified to use ExpandingDataSink objects instead of
michael@0 23 * the highly inefficient ostrstream objects.
michael@0 24 * 03/13/97 aliu Rewrote to load in entire resource bundle and store
michael@0 25 * it as a Hashtable of ResourceBundleData objects.
michael@0 26 * Added state table to govern parsing of files.
michael@0 27 * Modified to load locale index out of new file distinct
michael@0 28 * from default.txt.
michael@0 29 * 03/25/97 aliu Modified to support 2-d arrays, needed for timezone data.
michael@0 30 * Added support for custom file suffixes. Again, needed
michael@0 31 * to support timezone data. Improved error handling to
michael@0 32 * detect duplicate tags and subtags.
michael@0 33 * 04/07/97 aliu Fixed bug in getHashtableForLocale(). Fixed handling
michael@0 34 * of failing UErrorCode values on entry to API methods.
michael@0 35 * Fixed bugs in getArrayItem() for negative indices.
michael@0 36 * 04/29/97 aliu Update to use new Hashtable deletion protocol.
michael@0 37 * 05/06/97 aliu Flattened kTransitionTable for HP compiler.
michael@0 38 * Fixed usage of CharString.
michael@0 39 * 06/11/99 stephen Removed parsing of .txt files.
michael@0 40 * Reworked to use new binary format.
michael@0 41 * Cleaned up.
michael@0 42 * 06/14/99 stephen Removed methods taking a filename suffix.
michael@0 43 * 06/22/99 stephen Added missing T_FileStream_close in parse()
michael@0 44 * 11/09/99 weiv Added getLocale(), rewritten constructForLocale()
michael@0 45 * March 2000 weiv complete overhaul.
michael@0 46 ******************************************************************************
michael@0 47 */
michael@0 48
michael@0 49 #include "unicode/utypes.h"
michael@0 50 #include "unicode/resbund.h"
michael@0 51
michael@0 52 #include "mutex.h"
michael@0 53 #include "uassert.h"
michael@0 54 #include "umutex.h"
michael@0 55
michael@0 56 #include "uresimp.h"
michael@0 57
michael@0 58 U_NAMESPACE_BEGIN
michael@0 59
michael@0 60 /*-----------------------------------------------------------------------------
michael@0 61 * Implementation Notes
michael@0 62 *
michael@0 63 * Resource bundles are read in once, and thereafter cached.
michael@0 64 * ResourceBundle statically keeps track of which files have been
michael@0 65 * read, so we are guaranteed that each file is read at most once.
michael@0 66 * Resource bundles can be loaded from different data directories and
michael@0 67 * will be treated as distinct, even if they are for the same locale.
michael@0 68 *
michael@0 69 * Resource bundles are lightweight objects, which have pointers to
michael@0 70 * one or more shared Hashtable objects containing all the data.
michael@0 71 * Copying would be cheap, but there is no copy constructor, since
michael@0 72 * there wasn't one in the original API.
michael@0 73 *
michael@0 74 * The ResourceBundle parsing mechanism is implemented as a transition
michael@0 75 * network, for easy maintenance and modification. The network is
michael@0 76 * implemented as a matrix (instead of in code) to make this even
michael@0 77 * easier. The matrix contains Transition objects. Each Transition
michael@0 78 * object describes a destination node and an action to take before
michael@0 79 * moving to the destination node. The source node is encoded by the
michael@0 80 * index of the object in the array that contains it. The pieces
michael@0 81 * needed to understand the transition network are the enums for node
michael@0 82 * IDs and actions, the parse() method, which walks through the
michael@0 83 * network and implements the actions, and the network itself. The
michael@0 84 * network guarantees certain conditions, for example, that a new
michael@0 85 * resource will not be closed until one has been opened first; or
michael@0 86 * that data will not be stored into a TaggedList until a TaggedList
michael@0 87 * has been created. Nonetheless, the code in parse() does some
michael@0 88 * consistency checks as it runs the network, and fails with an
michael@0 89 * U_INTERNAL_PROGRAM_ERROR if one of these checks fails. If the input
michael@0 90 * data has a bad format, an U_INVALID_FORMAT_ERROR is returned. If you
michael@0 91 * see an U_INTERNAL_PROGRAM_ERROR the transition matrix has a bug in
michael@0 92 * it.
michael@0 93 *
michael@0 94 * Old functionality of multiple locales in a single file is still
michael@0 95 * supported. For this reason, LOCALE names override FILE names. If
michael@0 96 * data for en_US is located in the en.txt file, once it is loaded,
michael@0 97 * the code will not care where it came from (other than remembering
michael@0 98 * which directory it came from). However, if there is an en_US
michael@0 99 * resource in en_US.txt, that will take precedence. There is no
michael@0 100 * limit to the number or type of resources that can be stored in a
michael@0 101 * file, however, files are only searched in a specific way. If
michael@0 102 * en_US_CA is requested, then first en_US_CA.txt is searched, then
michael@0 103 * en_US.txt, then en.txt, then default.txt. So it only makes sense
michael@0 104 * to put certain locales in certain files. In this example, it would
michael@0 105 * be logical to put en_US_CA, en_US, and en into the en.txt file,
michael@0 106 * since they would be found there if asked for. The extreme example
michael@0 107 * is to place all locale resources into default.txt, which should
michael@0 108 * also work.
michael@0 109 *
michael@0 110 * Inheritance is implemented. For example, xx_YY_zz inherits as
michael@0 111 * follows: xx_YY_zz, xx_YY, xx, default. Inheritance is implemented
michael@0 112 * as an array of hashtables. There will be from 1 to 4 hashtables in
michael@0 113 * the array.
michael@0 114 *
michael@0 115 * Fallback files are implemented. The fallback pattern is Language
michael@0 116 * Country Variant (LCV) -> LC -> L. Fallback is first done for the
michael@0 117 * requested locale. Then it is done for the default locale, as
michael@0 118 * returned by Locale::getDefault(). Then the special file
michael@0 119 * default.txt is searched for the default locale. The overall FILE
michael@0 120 * fallback path is LCV -> LC -> L -> dLCV -> dLC -> dL -> default.
michael@0 121 *
michael@0 122 * Note that although file name searching includes the default locale,
michael@0 123 * once a ResourceBundle object is constructed, the inheritance path
michael@0 124 * no longer includes the default locale. The path is LCV -> LC -> L
michael@0 125 * -> default.
michael@0 126 *
michael@0 127 * File parsing is lazy. Nothing is parsed unless it is called for by
michael@0 128 * someone. So when a ResourceBundle for xx_YY_zz is constructed,
michael@0 129 * only that locale is parsed (along with anything else in the same
michael@0 130 * file). Later, if the FooBar tag is asked for, and if it isn't
michael@0 131 * found in xx_YY_zz, then xx_YY.txt will be parsed and checked, and
michael@0 132 * so forth, until the chain is exhausted or the tag is found.
michael@0 133 *
michael@0 134 * Thread-safety is implemented around caches, both the cache that
michael@0 135 * stores all the resouce data, and the cache that stores flags
michael@0 136 * indicating whether or not a file has been visited. These caches
michael@0 137 * delete their storage at static cleanup time, when the process
michael@0 138 * quits.
michael@0 139 *
michael@0 140 * ResourceBundle supports TableCollation as a special case. This
michael@0 141 * involves having special ResourceBundle objects which DO own their
michael@0 142 * data, since we don't want large collation rule strings in the
michael@0 143 * ResourceBundle cache (these are already cached in the
michael@0 144 * TableCollation cache). TableCollation files (.ctx files) have the
michael@0 145 * same format as normal resource data files, with a different
michael@0 146 * interpretation, from the standpoint of ResourceBundle. .ctx files
michael@0 147 * are loaded into otherwise ordinary ResourceBundle objects. They
michael@0 148 * don't inherit (that's implemented by TableCollation) and they own
michael@0 149 * their data (as mentioned above). However, they still support
michael@0 150 * possible multiple locales in a single .ctx file. (This is in
michael@0 151 * practice a bad idea, since you only want the one locale you're
michael@0 152 * looking for, and only one tag will be present
michael@0 153 * ("CollationElements"), so you don't need an inheritance chain of
michael@0 154 * multiple locales.) Up to 4 locale resources will be loaded from a
michael@0 155 * .ctx file; everything after the first 4 is ignored (parsed and
michael@0 156 * deleted). (Normal .txt files have no limit.) Instead of being
michael@0 157 * loaded into the cache, and then looked up as needed, the locale
michael@0 158 * resources are read straight into the ResourceBundle object.
michael@0 159 *
michael@0 160 * The Index, which used to reside in default.txt, has been moved to a
michael@0 161 * new file, index.txt. This file contains a slightly modified format
michael@0 162 * with the addition of the "InstalledLocales" tag; it looks like:
michael@0 163 *
michael@0 164 * Index {
michael@0 165 * InstalledLocales {
michael@0 166 * ar
michael@0 167 * ..
michael@0 168 * zh_TW
michael@0 169 * }
michael@0 170 * }
michael@0 171 */
michael@0 172 //-----------------------------------------------------------------------------
michael@0 173
michael@0 174 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ResourceBundle)
michael@0 175
michael@0 176 ResourceBundle::ResourceBundle(UErrorCode &err)
michael@0 177 :UObject(), fLocale(NULL)
michael@0 178 {
michael@0 179 fResource = ures_open(0, Locale::getDefault().getName(), &err);
michael@0 180 }
michael@0 181
michael@0 182 ResourceBundle::ResourceBundle(const ResourceBundle &other)
michael@0 183 :UObject(other), fLocale(NULL)
michael@0 184 {
michael@0 185 UErrorCode status = U_ZERO_ERROR;
michael@0 186
michael@0 187 if (other.fResource) {
michael@0 188 fResource = ures_copyResb(0, other.fResource, &status);
michael@0 189 } else {
michael@0 190 /* Copying a bad resource bundle */
michael@0 191 fResource = NULL;
michael@0 192 }
michael@0 193 }
michael@0 194
michael@0 195 ResourceBundle::ResourceBundle(UResourceBundle *res, UErrorCode& err)
michael@0 196 :UObject(), fLocale(NULL)
michael@0 197 {
michael@0 198 if (res) {
michael@0 199 fResource = ures_copyResb(0, res, &err);
michael@0 200 } else {
michael@0 201 /* Copying a bad resource bundle */
michael@0 202 fResource = NULL;
michael@0 203 }
michael@0 204 }
michael@0 205
michael@0 206 ResourceBundle::ResourceBundle(const char* path, const Locale& locale, UErrorCode& err)
michael@0 207 :UObject(), fLocale(NULL)
michael@0 208 {
michael@0 209 fResource = ures_open(path, locale.getName(), &err);
michael@0 210 }
michael@0 211
michael@0 212
michael@0 213 ResourceBundle& ResourceBundle::operator=(const ResourceBundle& other)
michael@0 214 {
michael@0 215 if(this == &other) {
michael@0 216 return *this;
michael@0 217 }
michael@0 218 if(fResource != 0) {
michael@0 219 ures_close(fResource);
michael@0 220 fResource = NULL;
michael@0 221 }
michael@0 222 if (fLocale != NULL) {
michael@0 223 delete fLocale;
michael@0 224 fLocale = NULL;
michael@0 225 }
michael@0 226 UErrorCode status = U_ZERO_ERROR;
michael@0 227 if (other.fResource) {
michael@0 228 fResource = ures_copyResb(0, other.fResource, &status);
michael@0 229 } else {
michael@0 230 /* Copying a bad resource bundle */
michael@0 231 fResource = NULL;
michael@0 232 }
michael@0 233 return *this;
michael@0 234 }
michael@0 235
michael@0 236 ResourceBundle::~ResourceBundle()
michael@0 237 {
michael@0 238 if(fResource != 0) {
michael@0 239 ures_close(fResource);
michael@0 240 }
michael@0 241 if(fLocale != NULL) {
michael@0 242 delete(fLocale);
michael@0 243 }
michael@0 244 }
michael@0 245
michael@0 246 ResourceBundle *
michael@0 247 ResourceBundle::clone() const {
michael@0 248 return new ResourceBundle(*this);
michael@0 249 }
michael@0 250
michael@0 251 UnicodeString ResourceBundle::getString(UErrorCode& status) const {
michael@0 252 int32_t len = 0;
michael@0 253 const UChar *r = ures_getString(fResource, &len, &status);
michael@0 254 return UnicodeString(TRUE, r, len);
michael@0 255 }
michael@0 256
michael@0 257 const uint8_t *ResourceBundle::getBinary(int32_t& len, UErrorCode& status) const {
michael@0 258 return ures_getBinary(fResource, &len, &status);
michael@0 259 }
michael@0 260
michael@0 261 const int32_t *ResourceBundle::getIntVector(int32_t& len, UErrorCode& status) const {
michael@0 262 return ures_getIntVector(fResource, &len, &status);
michael@0 263 }
michael@0 264
michael@0 265 uint32_t ResourceBundle::getUInt(UErrorCode& status) const {
michael@0 266 return ures_getUInt(fResource, &status);
michael@0 267 }
michael@0 268
michael@0 269 int32_t ResourceBundle::getInt(UErrorCode& status) const {
michael@0 270 return ures_getInt(fResource, &status);
michael@0 271 }
michael@0 272
michael@0 273 const char *ResourceBundle::getName(void) const {
michael@0 274 return ures_getName(fResource);
michael@0 275 }
michael@0 276
michael@0 277 const char *ResourceBundle::getKey(void) const {
michael@0 278 return ures_getKey(fResource);
michael@0 279 }
michael@0 280
michael@0 281 UResType ResourceBundle::getType(void) const {
michael@0 282 return ures_getType(fResource);
michael@0 283 }
michael@0 284
michael@0 285 int32_t ResourceBundle::getSize(void) const {
michael@0 286 return ures_getSize(fResource);
michael@0 287 }
michael@0 288
michael@0 289 UBool ResourceBundle::hasNext(void) const {
michael@0 290 return ures_hasNext(fResource);
michael@0 291 }
michael@0 292
michael@0 293 void ResourceBundle::resetIterator(void) {
michael@0 294 ures_resetIterator(fResource);
michael@0 295 }
michael@0 296
michael@0 297 ResourceBundle ResourceBundle::getNext(UErrorCode& status) {
michael@0 298 UResourceBundle r;
michael@0 299
michael@0 300 ures_initStackObject(&r);
michael@0 301 ures_getNextResource(fResource, &r, &status);
michael@0 302 ResourceBundle res(&r, status);
michael@0 303 if (U_SUCCESS(status)) {
michael@0 304 ures_close(&r);
michael@0 305 }
michael@0 306 return res;
michael@0 307 }
michael@0 308
michael@0 309 UnicodeString ResourceBundle::getNextString(UErrorCode& status) {
michael@0 310 int32_t len = 0;
michael@0 311 const UChar* r = ures_getNextString(fResource, &len, 0, &status);
michael@0 312 return UnicodeString(TRUE, r, len);
michael@0 313 }
michael@0 314
michael@0 315 UnicodeString ResourceBundle::getNextString(const char ** key, UErrorCode& status) {
michael@0 316 int32_t len = 0;
michael@0 317 const UChar* r = ures_getNextString(fResource, &len, key, &status);
michael@0 318 return UnicodeString(TRUE, r, len);
michael@0 319 }
michael@0 320
michael@0 321 ResourceBundle ResourceBundle::get(int32_t indexR, UErrorCode& status) const {
michael@0 322 UResourceBundle r;
michael@0 323
michael@0 324 ures_initStackObject(&r);
michael@0 325 ures_getByIndex(fResource, indexR, &r, &status);
michael@0 326 ResourceBundle res(&r, status);
michael@0 327 if (U_SUCCESS(status)) {
michael@0 328 ures_close(&r);
michael@0 329 }
michael@0 330 return res;
michael@0 331 }
michael@0 332
michael@0 333 UnicodeString ResourceBundle::getStringEx(int32_t indexS, UErrorCode& status) const {
michael@0 334 int32_t len = 0;
michael@0 335 const UChar* r = ures_getStringByIndex(fResource, indexS, &len, &status);
michael@0 336 return UnicodeString(TRUE, r, len);
michael@0 337 }
michael@0 338
michael@0 339 ResourceBundle ResourceBundle::get(const char* key, UErrorCode& status) const {
michael@0 340 UResourceBundle r;
michael@0 341
michael@0 342 ures_initStackObject(&r);
michael@0 343 ures_getByKey(fResource, key, &r, &status);
michael@0 344 ResourceBundle res(&r, status);
michael@0 345 if (U_SUCCESS(status)) {
michael@0 346 ures_close(&r);
michael@0 347 }
michael@0 348 return res;
michael@0 349 }
michael@0 350
michael@0 351 ResourceBundle ResourceBundle::getWithFallback(const char* key, UErrorCode& status){
michael@0 352 UResourceBundle r;
michael@0 353 ures_initStackObject(&r);
michael@0 354 ures_getByKeyWithFallback(fResource, key, &r, &status);
michael@0 355 ResourceBundle res(&r, status);
michael@0 356 if(U_SUCCESS(status)){
michael@0 357 ures_close(&r);
michael@0 358 }
michael@0 359 return res;
michael@0 360 }
michael@0 361 UnicodeString ResourceBundle::getStringEx(const char* key, UErrorCode& status) const {
michael@0 362 int32_t len = 0;
michael@0 363 const UChar* r = ures_getStringByKey(fResource, key, &len, &status);
michael@0 364 return UnicodeString(TRUE, r, len);
michael@0 365 }
michael@0 366
michael@0 367 const char*
michael@0 368 ResourceBundle::getVersionNumber() const
michael@0 369 {
michael@0 370 return ures_getVersionNumberInternal(fResource);
michael@0 371 }
michael@0 372
michael@0 373 void ResourceBundle::getVersion(UVersionInfo versionInfo) const {
michael@0 374 ures_getVersion(fResource, versionInfo);
michael@0 375 }
michael@0 376
michael@0 377 static UMutex gLocaleLock = U_MUTEX_INITIALIZER;
michael@0 378 const Locale &ResourceBundle::getLocale(void) const {
michael@0 379 Mutex lock(&gLocaleLock);
michael@0 380 if (fLocale != NULL) {
michael@0 381 return *fLocale;
michael@0 382 }
michael@0 383 UErrorCode status = U_ZERO_ERROR;
michael@0 384 const char *localeName = ures_getLocaleInternal(fResource, &status);
michael@0 385 ResourceBundle *ncThis = const_cast<ResourceBundle *>(this);
michael@0 386 ncThis->fLocale = new Locale(localeName);
michael@0 387 return ncThis->fLocale != NULL ? *ncThis->fLocale : Locale::getDefault();
michael@0 388 }
michael@0 389
michael@0 390 const Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const
michael@0 391 {
michael@0 392 return ures_getLocaleByType(fResource, type, &status);
michael@0 393 }
michael@0 394
michael@0 395 U_NAMESPACE_END
michael@0 396 //eof

mercurial