/src/icu/source/i18n/collationroot.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 | | * Copyright (C) 2012-2014, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ******************************************************************************* |
8 | | * collationroot.cpp |
9 | | * |
10 | | * created on: 2012dec17 |
11 | | * created by: Markus W. Scherer |
12 | | */ |
13 | | |
14 | | #include "unicode/utypes.h" |
15 | | |
16 | | #if !UCONFIG_NO_COLLATION |
17 | | |
18 | | #include "unicode/coll.h" |
19 | | #include "unicode/udata.h" |
20 | | #include "collation.h" |
21 | | #include "collationdata.h" |
22 | | #include "collationdatareader.h" |
23 | | #include "collationroot.h" |
24 | | #include "collationsettings.h" |
25 | | #include "collationtailoring.h" |
26 | | #include "normalizer2impl.h" |
27 | | #include "ucln_in.h" |
28 | | #include "udatamem.h" |
29 | | #include "umutex.h" |
30 | | |
31 | | U_NAMESPACE_BEGIN |
32 | | |
33 | | namespace { |
34 | | |
35 | | static const CollationCacheEntry *rootSingleton = NULL; |
36 | | static UInitOnce initOnce = U_INITONCE_INITIALIZER; |
37 | | |
38 | | } // namespace |
39 | | |
40 | | U_CDECL_BEGIN |
41 | | |
42 | 0 | static UBool U_CALLCONV uprv_collation_root_cleanup() { |
43 | 0 | SharedObject::clearPtr(rootSingleton); |
44 | 0 | initOnce.reset(); |
45 | 0 | return TRUE; |
46 | 0 | } |
47 | | |
48 | | U_CDECL_END |
49 | | |
50 | | void U_CALLCONV |
51 | 0 | CollationRoot::load(UErrorCode &errorCode) { |
52 | 0 | if(U_FAILURE(errorCode)) { return; } |
53 | 0 | LocalPointer<CollationTailoring> t(new CollationTailoring(NULL)); |
54 | 0 | if(t.isNull() || t->isBogus()) { |
55 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
56 | 0 | return; |
57 | 0 | } |
58 | 0 | t->memory = udata_openChoice(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "coll", |
59 | 0 | "icu", "ucadata", |
60 | 0 | CollationDataReader::isAcceptable, t->version, &errorCode); |
61 | 0 | if(U_FAILURE(errorCode)) { return; } |
62 | 0 | const uint8_t *inBytes = static_cast<const uint8_t *>(udata_getMemory(t->memory)); |
63 | 0 | CollationDataReader::read(NULL, inBytes, udata_getLength(t->memory), *t, errorCode); |
64 | 0 | if(U_FAILURE(errorCode)) { return; } |
65 | 0 | ucln_i18n_registerCleanup(UCLN_I18N_COLLATION_ROOT, uprv_collation_root_cleanup); |
66 | 0 | CollationCacheEntry *entry = new CollationCacheEntry(Locale::getRoot(), t.getAlias()); |
67 | 0 | if(entry != NULL) { |
68 | 0 | t.orphan(); // The rootSingleton took ownership of the tailoring. |
69 | 0 | entry->addRef(); |
70 | 0 | rootSingleton = entry; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | const CollationCacheEntry * |
75 | 0 | CollationRoot::getRootCacheEntry(UErrorCode &errorCode) { |
76 | 0 | umtx_initOnce(initOnce, CollationRoot::load, errorCode); |
77 | 0 | if(U_FAILURE(errorCode)) { return NULL; } |
78 | 0 | return rootSingleton; |
79 | 0 | } |
80 | | |
81 | | const CollationTailoring * |
82 | 0 | CollationRoot::getRoot(UErrorCode &errorCode) { |
83 | 0 | umtx_initOnce(initOnce, CollationRoot::load, errorCode); |
84 | 0 | if(U_FAILURE(errorCode)) { return NULL; } |
85 | 0 | return rootSingleton->tailoring; |
86 | 0 | } |
87 | | |
88 | | const CollationData * |
89 | 0 | CollationRoot::getData(UErrorCode &errorCode) { |
90 | 0 | const CollationTailoring *root = getRoot(errorCode); |
91 | 0 | if(U_FAILURE(errorCode)) { return NULL; } |
92 | 0 | return root->data; |
93 | 0 | } |
94 | | |
95 | | const CollationSettings * |
96 | 0 | CollationRoot::getSettings(UErrorCode &errorCode) { |
97 | 0 | const CollationTailoring *root = getRoot(errorCode); |
98 | 0 | if(U_FAILURE(errorCode)) { return NULL; } |
99 | 0 | return root->settings; |
100 | 0 | } |
101 | | |
102 | | U_NAMESPACE_END |
103 | | |
104 | | #endif // !UCONFIG_NO_COLLATION |