Coverage Report

Created: 2021-08-22 09:07

/src/skia/third_party/externals/icu/source/common/locresdata.cpp
Line
Count
Source (jump to first uncovered line)
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 "cstring.h"
28
#include "ulocimp.h"
29
#include "uresimp.h"
30
31
/*
32
 * Lookup a resource bundle table item with fallback on the table level.
33
 * Regular resource bundle lookups perform fallback to parent locale bundles
34
 * and eventually the root bundle, but only for top-level items.
35
 * This function takes the name of a top-level table and of an item in that table
36
 * and performs a lookup of both, falling back until a bundle contains a table
37
 * with this item.
38
 *
39
 * Note: Only the opening of entire bundles falls back through the default locale
40
 * before root. Once a bundle is open, item lookups do not go through the
41
 * default locale because that would result in a mix of languages that is
42
 * unpredictable to the programmer and most likely useless.
43
 */
44
U_CAPI const UChar * U_EXPORT2
45
uloc_getTableStringWithFallback(const char *path, const char *locale,
46
                              const char *tableKey, const char *subTableKey,
47
                              const char *itemKey,
48
                              int32_t *pLength,
49
                              UErrorCode *pErrorCode)
50
0
{
51
/*    char localeBuffer[ULOC_FULLNAME_CAPACITY*4];*/
52
0
    const UChar *item=NULL;
53
0
    UErrorCode errorCode;
54
0
    char explicitFallbackName[ULOC_FULLNAME_CAPACITY] = {0};
55
56
    /*
57
     * open the bundle for the current locale
58
     * this falls back through the locale's chain to root
59
     */
60
0
    errorCode=U_ZERO_ERROR;
61
0
    icu::LocalUResourceBundlePointer rb(ures_open(path, locale, &errorCode));
62
63
0
    if(U_FAILURE(errorCode)) {
64
        /* total failure, not even root could be opened */
65
0
        *pErrorCode=errorCode;
66
0
        return NULL;
67
0
    } else if(errorCode==U_USING_DEFAULT_WARNING ||
68
0
                (errorCode==U_USING_FALLBACK_WARNING && *pErrorCode!=U_USING_DEFAULT_WARNING)
69
0
    ) {
70
        /* set the "strongest" error code (success->fallback->default->failure) */
71
0
        *pErrorCode=errorCode;
72
0
    }
73
74
0
    for(;;){
75
0
        icu::StackUResourceBundle table;
76
0
        icu::StackUResourceBundle subTable;
77
0
        ures_getByKeyWithFallback(rb.getAlias(), tableKey, table.getAlias(), &errorCode);
78
79
0
        if (subTableKey != NULL) {
80
            /*
81
            ures_getByKeyWithFallback(table.getAlias(), subTableKey, subTable.getAlias(), &errorCode);
82
            item = ures_getStringByKeyWithFallback(subTable.getAlias(), itemKey, pLength, &errorCode);
83
            if(U_FAILURE(errorCode)){
84
                *pErrorCode = errorCode;
85
            }
86
            
87
            break;*/
88
            
89
0
            ures_getByKeyWithFallback(table.getAlias(), subTableKey, table.getAlias(), &errorCode);
90
0
        }
91
0
        if(U_SUCCESS(errorCode)){
92
0
            item = ures_getStringByKeyWithFallback(table.getAlias(), itemKey, pLength, &errorCode);
93
0
            if(U_FAILURE(errorCode)){
94
0
                const char* replacement = NULL;
95
0
                *pErrorCode = errorCode; /*save the errorCode*/
96
0
                errorCode = U_ZERO_ERROR;
97
                /* may be a deprecated code */
98
0
                if(uprv_strcmp(tableKey, "Countries")==0){
99
0
                    replacement =  uloc_getCurrentCountryID(itemKey);
100
0
                }else if(uprv_strcmp(tableKey, "Languages")==0){
101
0
                    replacement =  uloc_getCurrentLanguageID(itemKey);
102
0
                }
103
                /*pointer comparison is ok since uloc_getCurrentCountryID & uloc_getCurrentLanguageID return the key itself is replacement is not found*/
104
0
                if(replacement!=NULL && itemKey != replacement){
105
0
                    item = ures_getStringByKeyWithFallback(table.getAlias(), replacement, pLength, &errorCode);
106
0
                    if(U_SUCCESS(errorCode)){
107
0
                        *pErrorCode = errorCode;
108
0
                        break;
109
0
                    }
110
0
                }
111
0
            }else{
112
0
                break;
113
0
            }
114
0
        }
115
        
116
0
        if(U_FAILURE(errorCode)){    
117
118
            /* still can't figure out ?.. try the fallback mechanism */
119
0
            int32_t len = 0;
120
0
            const UChar* fallbackLocale =  NULL;
121
0
            *pErrorCode = errorCode;
122
0
            errorCode = U_ZERO_ERROR;
123
124
0
            fallbackLocale = ures_getStringByKeyWithFallback(table.getAlias(), "Fallback", &len, &errorCode);
125
0
            if(U_FAILURE(errorCode)){
126
0
               *pErrorCode = errorCode;
127
0
                break;
128
0
            }
129
            
130
0
            u_UCharsToChars(fallbackLocale, explicitFallbackName, len);
131
            
132
            /* guard against recursive fallback */
133
0
            if(uprv_strcmp(explicitFallbackName, locale)==0){
134
0
                *pErrorCode = U_INTERNAL_PROGRAM_ERROR;
135
0
                break;
136
0
            }
137
0
            rb.adoptInstead(ures_open(path, explicitFallbackName, &errorCode));
138
0
            if(U_FAILURE(errorCode)){
139
0
                *pErrorCode = errorCode;
140
0
                break;
141
0
            }
142
            /* succeeded in opening the fallback bundle .. continue and try to fetch the item */
143
0
        }else{
144
0
            break;
145
0
        }
146
0
    }
147
148
0
    return item;
149
0
}
150
151
static ULayoutType
152
_uloc_getOrientationHelper(const char* localeId,
153
                           const char* key,
154
                           UErrorCode *status)
155
0
{
156
0
    ULayoutType result = ULOC_LAYOUT_UNKNOWN;
157
158
0
    if (!U_FAILURE(*status)) {
159
0
        int32_t length = 0;
160
0
        char localeBuffer[ULOC_FULLNAME_CAPACITY];
161
162
0
        uloc_canonicalize(localeId, localeBuffer, sizeof(localeBuffer), status);
163
164
0
        if (!U_FAILURE(*status)) {
165
0
            const UChar* const value =
166
0
                uloc_getTableStringWithFallback(
167
0
                    NULL,
168
0
                    localeBuffer,
169
0
                    "layout",
170
0
                    NULL,
171
0
                    key,
172
0
                    &length,
173
0
                    status);
174
175
0
            if (!U_FAILURE(*status) && length != 0) {
176
0
                switch(value[0])
177
0
                {
178
0
                case 0x0062: /* 'b' */
179
0
                    result = ULOC_LAYOUT_BTT;
180
0
                    break;
181
0
                case 0x006C: /* 'l' */
182
0
                    result = ULOC_LAYOUT_LTR;
183
0
                    break;
184
0
                case 0x0072: /* 'r' */
185
0
                    result = ULOC_LAYOUT_RTL;
186
0
                    break;
187
0
                case 0x0074: /* 't' */
188
0
                    result = ULOC_LAYOUT_TTB;
189
0
                    break;
190
0
                default:
191
0
                    *status = U_INTERNAL_PROGRAM_ERROR;
192
0
                    break;
193
0
                }
194
0
            }
195
0
        }
196
0
    }
197
198
0
    return result;
199
0
}
200
201
U_CAPI ULayoutType U_EXPORT2
202
uloc_getCharacterOrientation(const char* localeId,
203
                             UErrorCode *status)
204
0
{
205
0
    return _uloc_getOrientationHelper(localeId, "characters", status);
206
0
}
207
208
/**
209
 * Get the layout line orientation for the specified locale.
210
 * 
211
 * @param localeID locale name
212
 * @param status Error status
213
 * @return an enum indicating the layout orientation for lines.
214
 */
215
U_CAPI ULayoutType U_EXPORT2
216
uloc_getLineOrientation(const char* localeId,
217
                        UErrorCode *status)
218
0
{
219
0
    return _uloc_getOrientationHelper(localeId, "lines", status);
220
0
}