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