Home | History | Annotate | Download | only in i18n
      1 /*
      2 ******************************************************************************
      3 *                                                                            *
      4 * Copyright (C) 2003-2009, International Business Machines                   *
      5 *                Corporation and others. All Rights Reserved.                *
      6 *                                                                            *
      7 ******************************************************************************
      8 *   file name:  ulocdata.c
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 2003Oct21
     14 *   created by: Ram Viswanadha,John Emmons
     15 */
     16 
     17 #include "cmemory.h"
     18 #include "unicode/ustring.h"
     19 #include "unicode/ulocdata.h"
     20 #include "umutex.h"
     21 #include "uresimp.h"
     22 #include "ureslocs.h"
     23 
     24 #define MEASUREMENT_SYSTEM  "MeasurementSystem"
     25 #define PAPER_SIZE          "PaperSize"
     26 
     27 /** A locale data object.
     28  *  For usage in C programs.
     29  *  @draft ICU 3.4
     30  */
     31 struct ULocaleData {
     32     /**
     33      * Controls the "No Substitute" behavior of this locale data object
     34      */
     35     UBool noSubstitute;
     36 
     37     /**
     38      * Pointer to the resource bundle associated with this locale data object
     39      */
     40     UResourceBundle *bundle;
     41 
     42     /**
     43      * Pointer to the lang resource bundle associated with this locale data object
     44      */
     45     UResourceBundle *langBundle;
     46 };
     47 
     48 U_CAPI ULocaleData* U_EXPORT2
     49 ulocdata_open(const char *localeID, UErrorCode *status)
     50 {
     51    ULocaleData *uld;
     52 
     53    if (U_FAILURE(*status)) {
     54        return NULL;
     55    }
     56 
     57    uld = (ULocaleData *)uprv_malloc(sizeof(ULocaleData));
     58    if (uld == NULL) {
     59       *status = U_MEMORY_ALLOCATION_ERROR;
     60       return(NULL);
     61    }
     62 
     63    uld->langBundle = NULL;
     64 
     65    uld->noSubstitute = FALSE;
     66    uld->bundle = ures_open(NULL, localeID, status);
     67    uld->langBundle = ures_open(U_ICUDATA_LANG, localeID, status);
     68 
     69    if (U_FAILURE(*status)) {
     70       uprv_free(uld);
     71       return NULL;
     72    }
     73 
     74    return uld;
     75 }
     76 
     77 U_CAPI void U_EXPORT2
     78 ulocdata_close(ULocaleData *uld)
     79 {
     80     if ( uld != NULL ) {
     81        ures_close(uld->langBundle);
     82        ures_close(uld->bundle);
     83        uprv_free(uld);
     84     }
     85 }
     86 
     87 U_CAPI void U_EXPORT2
     88 ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
     89 {
     90    uld->noSubstitute = setting;
     91 }
     92 
     93 U_CAPI UBool U_EXPORT2
     94 ulocdata_getNoSubstitute(ULocaleData *uld)
     95 {
     96    return uld->noSubstitute;
     97 }
     98 
     99 U_CAPI USet* U_EXPORT2
    100 ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
    101                         uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
    102 
    103     static const char* const exemplarSetTypes[] = { "ExemplarCharacters", "AuxExemplarCharacters" };
    104     const UChar *exemplarChars = NULL;
    105     int32_t len = 0;
    106     UErrorCode localStatus = U_ZERO_ERROR;
    107 
    108     if (U_FAILURE(*status))
    109         return NULL;
    110 
    111     exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus);
    112     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    113         localStatus = U_MISSING_RESOURCE_ERROR;
    114     }
    115 
    116     if (localStatus != U_ZERO_ERROR) {
    117         *status = localStatus;
    118     }
    119 
    120     if (U_FAILURE(*status))
    121         return NULL;
    122 
    123     if(fillIn != NULL)
    124         uset_applyPattern(fillIn, exemplarChars, len,
    125                           USET_IGNORE_SPACE | options, status);
    126     else
    127         fillIn = uset_openPatternOptions(exemplarChars, len,
    128                                          USET_IGNORE_SPACE | options, status);
    129 
    130     return fillIn;
    131 
    132 }
    133 
    134 U_CAPI int32_t U_EXPORT2
    135 ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
    136                       UChar *result, int32_t resultLength, UErrorCode *status){
    137 
    138     static const char* const delimiterKeys[] =  {
    139         "quotationStart",
    140         "quotationEnd",
    141         "alternateQuotationStart",
    142         "alternateQuotationEnd"
    143     };
    144 
    145     UResourceBundle *delimiterBundle;
    146     int32_t len = 0;
    147     const UChar *delimiter = NULL;
    148     UErrorCode localStatus = U_ZERO_ERROR;
    149 
    150     if (U_FAILURE(*status))
    151         return 0;
    152 
    153     delimiterBundle = ures_getByKey(uld->bundle, "delimiters", NULL, &localStatus);
    154 
    155     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    156         localStatus = U_MISSING_RESOURCE_ERROR;
    157     }
    158 
    159     if (localStatus != U_ZERO_ERROR) {
    160         *status = localStatus;
    161     }
    162 
    163     if (U_FAILURE(*status)){
    164         ures_close(delimiterBundle);
    165         return 0;
    166     }
    167 
    168     delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus);
    169     ures_close(delimiterBundle);
    170 
    171     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    172         localStatus = U_MISSING_RESOURCE_ERROR;
    173     }
    174 
    175     if (localStatus != U_ZERO_ERROR) {
    176         *status = localStatus;
    177     }
    178 
    179     if (U_FAILURE(*status)){
    180         return 0;
    181     }
    182 
    183     u_strncpy(result,delimiter, resultLength);
    184     return len;
    185 }
    186 
    187 U_CAPI UMeasurementSystem U_EXPORT2
    188 ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
    189 
    190     UResourceBundle* bundle=NULL;
    191     UResourceBundle* measurement=NULL;
    192     UMeasurementSystem system = UMS_LIMIT;
    193 
    194     if(status == NULL || U_FAILURE(*status)){
    195         return system;
    196     }
    197 
    198     bundle = ures_open(NULL, localeID, status);
    199 
    200     measurement = ures_getByKeyWithFallback(bundle, MEASUREMENT_SYSTEM, NULL, status);
    201 
    202     system = (UMeasurementSystem) ures_getInt(measurement, status);
    203 
    204     ures_close(bundle);
    205     ures_close(measurement);
    206 
    207     return system;
    208 
    209 }
    210 
    211 U_CAPI void U_EXPORT2
    212 ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
    213     UResourceBundle* bundle=NULL;
    214     UResourceBundle* paperSizeBundle = NULL;
    215     const int32_t* paperSize=NULL;
    216     int32_t len = 0;
    217 
    218     if(status == NULL || U_FAILURE(*status)){
    219         return;
    220     }
    221 
    222     bundle = ures_open(NULL, localeID, status);
    223     paperSizeBundle = ures_getByKeyWithFallback(bundle, PAPER_SIZE, NULL, status);
    224     paperSize = ures_getIntVector(paperSizeBundle, &len,  status);
    225 
    226     if(U_SUCCESS(*status)){
    227         if(len < 2){
    228             *status = U_INTERNAL_PROGRAM_ERROR;
    229         }else{
    230             *height = paperSize[0];
    231             *width  = paperSize[1];
    232         }
    233     }
    234 
    235     ures_close(bundle);
    236     ures_close(paperSizeBundle);
    237 
    238 }
    239 
    240 U_DRAFT void U_EXPORT2
    241 ulocdata_getCLDRVersion(UVersionInfo versionArray, UErrorCode *status) {
    242     UResourceBundle *rb = NULL;
    243     rb = ures_openDirect(NULL, "supplementalData", status);
    244     ures_getVersionByKey(rb, "cldrVersion", versionArray, status);
    245     ures_close(rb);
    246 }
    247 
    248 U_DRAFT int32_t U_EXPORT2
    249 ulocdata_getLocaleDisplayPattern(ULocaleData *uld,
    250                                  UChar *result,
    251                                  int32_t resultCapacity,
    252                                  UErrorCode *status) {
    253     UResourceBundle *patternBundle;
    254     int32_t len = 0;
    255     const UChar *pattern = NULL;
    256     UErrorCode localStatus = U_ZERO_ERROR;
    257 
    258     if (U_FAILURE(*status))
    259         return 0;
    260 
    261     patternBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", NULL, &localStatus);
    262 
    263     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    264         localStatus = U_MISSING_RESOURCE_ERROR;
    265     }
    266 
    267     if (localStatus != U_ZERO_ERROR) {
    268         *status = localStatus;
    269     }
    270 
    271     if (U_FAILURE(*status)){
    272         ures_close(patternBundle);
    273         return 0;
    274     }
    275 
    276     pattern = ures_getStringByKey(patternBundle, "pattern", &len, &localStatus);
    277     ures_close(patternBundle);
    278 
    279     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    280         localStatus = U_MISSING_RESOURCE_ERROR;
    281     }
    282 
    283     if (localStatus != U_ZERO_ERROR) {
    284         *status = localStatus;
    285     }
    286 
    287     if (U_FAILURE(*status)){
    288         return 0;
    289     }
    290 
    291     u_strncpy(result, pattern, resultCapacity);
    292     return len;
    293 }
    294 
    295 
    296 U_DRAFT int32_t U_EXPORT2
    297 ulocdata_getLocaleSeparator(ULocaleData *uld,
    298                             UChar *result,
    299                             int32_t resultCapacity,
    300                             UErrorCode *status)  {
    301     UResourceBundle *separatorBundle;
    302     int32_t len = 0;
    303     const UChar *separator = NULL;
    304     UErrorCode localStatus = U_ZERO_ERROR;
    305 
    306     if (U_FAILURE(*status))
    307         return 0;
    308 
    309     separatorBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", NULL, &localStatus);
    310 
    311     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    312         localStatus = U_MISSING_RESOURCE_ERROR;
    313     }
    314 
    315     if (localStatus != U_ZERO_ERROR) {
    316         *status = localStatus;
    317     }
    318 
    319     if (U_FAILURE(*status)){
    320         ures_close(separatorBundle);
    321         return 0;
    322     }
    323 
    324     separator = ures_getStringByKey(separatorBundle, "separator", &len, &localStatus);
    325     ures_close(separatorBundle);
    326 
    327     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
    328         localStatus = U_MISSING_RESOURCE_ERROR;
    329     }
    330 
    331     if (localStatus != U_ZERO_ERROR) {
    332         *status = localStatus;
    333     }
    334 
    335     if (U_FAILURE(*status)){
    336         return 0;
    337     }
    338 
    339     u_strncpy(result, separator, resultCapacity);
    340     return len;
    341 }
    342