2 *******************************************************************************
3 * Copyright (C) 2012-2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * created on: 2012dec17
9 * created by: Markus W. Scherer
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_COLLATION
16 #include "unicode/coll.h"
17 #include "unicode/udata.h"
18 #include "collation.h"
19 #include "collationdata.h"
20 #include "collationdatareader.h"
21 #include "collationroot.h"
22 #include "collationsettings.h"
23 #include "collationtailoring.h"
24 #include "normalizer2impl.h"
33 static const CollationCacheEntry
*rootSingleton
= NULL
;
34 static UInitOnce initOnce
= U_INITONCE_INITIALIZER
;
40 static UBool U_CALLCONV
uprv_collation_root_cleanup() {
41 SharedObject::clearPtr(rootSingleton
);
49 CollationRoot::load(UErrorCode
&errorCode
) {
50 if(U_FAILURE(errorCode
)) { return; }
51 LocalPointer
<CollationTailoring
> t(new CollationTailoring(NULL
));
52 if(t
.isNull() || t
->isBogus()) {
53 errorCode
= U_MEMORY_ALLOCATION_ERROR
;
56 t
->memory
= udata_openChoice(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
"coll",
58 CollationDataReader::isAcceptable
, t
->version
, &errorCode
);
59 if(U_FAILURE(errorCode
)) { return; }
60 const uint8_t *inBytes
= static_cast<const uint8_t *>(udata_getMemory(t
->memory
));
61 CollationDataReader::read(NULL
, inBytes
, udata_getLength(t
->memory
), *t
, errorCode
);
62 if(U_FAILURE(errorCode
)) { return; }
63 ucln_i18n_registerCleanup(UCLN_I18N_COLLATION_ROOT
, uprv_collation_root_cleanup
);
64 CollationCacheEntry
*entry
= new CollationCacheEntry(Locale::getRoot(), t
.getAlias());
66 t
.orphan(); // The rootSingleton took ownership of the tailoring.
68 rootSingleton
= entry
;
72 const CollationCacheEntry
*
73 CollationRoot::getRootCacheEntry(UErrorCode
&errorCode
) {
74 umtx_initOnce(initOnce
, CollationRoot::load
, errorCode
);
75 if(U_FAILURE(errorCode
)) { return NULL
; }
79 const CollationTailoring
*
80 CollationRoot::getRoot(UErrorCode
&errorCode
) {
81 umtx_initOnce(initOnce
, CollationRoot::load
, errorCode
);
82 if(U_FAILURE(errorCode
)) { return NULL
; }
83 return rootSingleton
->tailoring
;
87 CollationRoot::getData(UErrorCode
&errorCode
) {
88 const CollationTailoring
*root
= getRoot(errorCode
);
89 if(U_FAILURE(errorCode
)) { return NULL
; }
93 const CollationSettings
*
94 CollationRoot::getSettings(UErrorCode
&errorCode
) {
95 const CollationTailoring
*root
= getRoot(errorCode
);
96 if(U_FAILURE(errorCode
)) { return NULL
; }
97 return root
->settings
;
102 #endif // !UCONFIG_NO_COLLATION