xref: /aosp_15_r20/external/cronet/third_party/icu/source/common/locresdata.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 1997-2012, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  loclikely.cpp
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2010feb25
16 *   created by: Markus W. Scherer
17 *
18 *   Code for miscellaneous locale-related resource bundle data access,
19 *   separated out from other .cpp files
20 *   that then do not depend on resource bundle code and this data.
21 */
22 
23 #include "unicode/utypes.h"
24 #include "unicode/putil.h"
25 #include "unicode/uloc.h"
26 #include "unicode/ures.h"
27 #include "bytesinkutil.h"
28 #include "charstr.h"
29 #include "cstring.h"
30 #include "ulocimp.h"
31 #include "uresimp.h"
32 
33 /*
34  * Lookup a resource bundle table item with fallback on the table level.
35  * Regular resource bundle lookups perform fallback to parent locale bundles
36  * and eventually the root bundle, but only for top-level items.
37  * This function takes the name of a top-level table and of an item in that table
38  * and performs a lookup of both, falling back until a bundle contains a table
39  * with this item.
40  *
41  * Note: Only the opening of entire bundles falls back through the default locale
42  * before root. Once a bundle is open, item lookups do not go through the
43  * default locale because that would result in a mix of languages that is
44  * unpredictable to the programmer and most likely useless.
45  */
46 U_CAPI const char16_t * U_EXPORT2
uloc_getTableStringWithFallback(const char * path,const char * locale,const char * tableKey,const char * subTableKey,const char * itemKey,int32_t * pLength,UErrorCode * pErrorCode)47 uloc_getTableStringWithFallback(const char *path, const char *locale,
48                               const char *tableKey, const char *subTableKey,
49                               const char *itemKey,
50                               int32_t *pLength,
51                               UErrorCode *pErrorCode)
52 {
53 /*    char localeBuffer[ULOC_FULLNAME_CAPACITY*4];*/
54     const char16_t *item=nullptr;
55     UErrorCode errorCode;
56     char explicitFallbackName[ULOC_FULLNAME_CAPACITY] = {0};
57 
58     /*
59      * open the bundle for the current locale
60      * this falls back through the locale's chain to root
61      */
62     errorCode=U_ZERO_ERROR;
63     icu::LocalUResourceBundlePointer rb(ures_open(path, locale, &errorCode));
64 
65     if(U_FAILURE(errorCode)) {
66         /* total failure, not even root could be opened */
67         *pErrorCode=errorCode;
68         return nullptr;
69     } else if(errorCode==U_USING_DEFAULT_WARNING ||
70                 (errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
71     ) {
72         /* set the "strongest" error code (success->fallback->default->failure) */
73         *pErrorCode=errorCode;
74     }
75 
76     for(;;){
77         icu::StackUResourceBundle table;
78         icu::StackUResourceBundle subTable;
79         ures_getByKeyWithFallback(rb.getAlias(), tableKey, table.getAlias(), &errorCode);
80 
81         if (subTableKey != nullptr) {
82             /*
83             ures_getByKeyWithFallback(table.getAlias(), subTableKey, subTable.getAlias(), &errorCode);
84             item = ures_getStringByKeyWithFallback(subTable.getAlias(), itemKey, pLength, &errorCode);
85             if(U_FAILURE(errorCode)){
86                 *pErrorCode = errorCode;
87             }
88 
89             break;*/
90 
91             ures_getByKeyWithFallback(table.getAlias(), subTableKey, table.getAlias(), &errorCode);
92         }
93         if(U_SUCCESS(errorCode)){
94             item = ures_getStringByKeyWithFallback(table.getAlias(), itemKey, pLength, &errorCode);
95             if(U_FAILURE(errorCode)){
96                 const char* replacement = nullptr;
97                 *pErrorCode = errorCode; /*save the errorCode*/
98                 errorCode = U_ZERO_ERROR;
99                 /* may be a deprecated code */
100                 if(uprv_strcmp(tableKey, "Countries")==0){
101                     replacement =  uloc_getCurrentCountryID(itemKey);
102                 }else if(uprv_strcmp(tableKey, "Languages")==0){
103                     replacement =  uloc_getCurrentLanguageID(itemKey);
104                 }
105                 /*pointer comparison is ok since uloc_getCurrentCountryID & uloc_getCurrentLanguageID return the key itself is replacement is not found*/
106                 if(replacement!=nullptr && itemKey != replacement){
107                     item = ures_getStringByKeyWithFallback(table.getAlias(), replacement, pLength, &errorCode);
108                     if(U_SUCCESS(errorCode)){
109                         *pErrorCode = errorCode;
110                         break;
111                     }
112                 }
113             }else{
114                 break;
115             }
116         }
117 
118         if(U_FAILURE(errorCode)){
119 
120             /* still can't figure out ?.. try the fallback mechanism */
121             int32_t len = 0;
122             const char16_t* fallbackLocale =  nullptr;
123             *pErrorCode = errorCode;
124             errorCode = U_ZERO_ERROR;
125 
126             fallbackLocale = ures_getStringByKeyWithFallback(table.getAlias(), "Fallback", &len, &errorCode);
127             if(U_FAILURE(errorCode)){
128                *pErrorCode = errorCode;
129                 break;
130             }
131 
132             u_UCharsToChars(fallbackLocale, explicitFallbackName, len);
133 
134             /* guard against recursive fallback */
135             if(uprv_strcmp(explicitFallbackName, locale)==0){
136                 *pErrorCode = U_INTERNAL_PROGRAM_ERROR;
137                 break;
138             }
139             rb.adoptInstead(ures_open(path, explicitFallbackName, &errorCode));
140             if(U_FAILURE(errorCode)){
141                 *pErrorCode = errorCode;
142                 break;
143             }
144             /* succeeded in opening the fallback bundle .. continue and try to fetch the item */
145         }else{
146             break;
147         }
148     }
149 
150     return item;
151 }
152 
153 static ULayoutType
_uloc_getOrientationHelper(const char * localeId,const char * key,UErrorCode * status)154 _uloc_getOrientationHelper(const char* localeId,
155                            const char* key,
156                            UErrorCode *status)
157 {
158     ULayoutType result = ULOC_LAYOUT_UNKNOWN;
159 
160     if (!U_FAILURE(*status)) {
161         icu::CharString localeBuffer;
162         {
163             icu::CharStringByteSink sink(&localeBuffer);
164             ulocimp_canonicalize(localeId, sink, status);
165         }
166 
167         if (!U_FAILURE(*status)) {
168             int32_t length = 0;
169             const char16_t* const value =
170                 uloc_getTableStringWithFallback(
171                     nullptr,
172                     localeBuffer.data(),
173                     "layout",
174                     nullptr,
175                     key,
176                     &length,
177                     status);
178 
179             if (!U_FAILURE(*status) && length != 0) {
180                 switch(value[0])
181                 {
182                 case 0x0062: /* 'b' */
183                     result = ULOC_LAYOUT_BTT;
184                     break;
185                 case 0x006C: /* 'l' */
186                     result = ULOC_LAYOUT_LTR;
187                     break;
188                 case 0x0072: /* 'r' */
189                     result = ULOC_LAYOUT_RTL;
190                     break;
191                 case 0x0074: /* 't' */
192                     result = ULOC_LAYOUT_TTB;
193                     break;
194                 default:
195                     *status = U_INTERNAL_PROGRAM_ERROR;
196                     break;
197                 }
198             }
199         }
200     }
201 
202     return result;
203 }
204 
205 U_CAPI ULayoutType U_EXPORT2
uloc_getCharacterOrientation(const char * localeId,UErrorCode * status)206 uloc_getCharacterOrientation(const char* localeId,
207                              UErrorCode *status)
208 {
209     return _uloc_getOrientationHelper(localeId, "characters", status);
210 }
211 
212 /**
213  * Get the layout line orientation for the specified locale.
214  *
215  * @param localeID locale name
216  * @param status Error status
217  * @return an enum indicating the layout orientation for lines.
218  */
219 U_CAPI ULayoutType U_EXPORT2
uloc_getLineOrientation(const char * localeId,UErrorCode * status)220 uloc_getLineOrientation(const char* localeId,
221                         UErrorCode *status)
222 {
223     return _uloc_getOrientationHelper(localeId, "lines", status);
224 }
225