blob: dc88c35a680095b46e12391f8a96e3bb1654cb2a [file] [log] [blame]
Jungshik Shin87232d82017-05-13 21:10:13 -07001// © 2016 and later: Unicode, Inc. and others.
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07002// License & terms of use: http://www.unicode.org/copyright.html
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -08003/*
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"
Frank Tang1f164ee2022-11-08 12:31:27 -080030#include "umapfile.h"
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -080031
32U_NAMESPACE_BEGIN
33
34namespace {
35
36static const CollationCacheEntry *rootSingleton = NULL;
Frank Tang1c67b4e2022-05-18 10:13:51 -070037static UInitOnce initOnce {};
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -080038
39} // namespace
40
41U_CDECL_BEGIN
42
43static UBool U_CALLCONV uprv_collation_root_cleanup() {
44 SharedObject::clearPtr(rootSingleton);
45 initOnce.reset();
Frank Tang1f164ee2022-11-08 12:31:27 -080046 return true;
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -080047}
48
49U_CDECL_END
50
Frank Tang1f164ee2022-11-08 12:31:27 -080051UDataMemory*
52CollationRoot::loadFromFile(const char* ucadataPath, UErrorCode &errorCode) {
53 UDataMemory dataMemory;
54 UDataMemory *rDataMem = NULL;
55 if (U_FAILURE(errorCode)) {
56 return NULL;
57 }
58 if (uprv_mapFile(&dataMemory, ucadataPath, &errorCode)) {
59 if (dataMemory.pHeader->dataHeader.magic1 == 0xda &&
60 dataMemory.pHeader->dataHeader.magic2 == 0x27 &&
61 CollationDataReader::isAcceptable(NULL, "icu", "ucadata", &dataMemory.pHeader->info)) {
62 rDataMem = UDataMemory_createNewInstance(&errorCode);
63 if (U_FAILURE(errorCode)) {
64 return NULL;
65 }
66 rDataMem->pHeader = dataMemory.pHeader;
67 rDataMem->mapAddr = dataMemory.mapAddr;
68 rDataMem->map = dataMemory.map;
69 return rDataMem;
70 }
71 errorCode = U_INVALID_FORMAT_ERROR;
72 return NULL;
73 }
74 errorCode = U_MISSING_RESOURCE_ERROR;
75 return NULL;
76}
77
Jungshik Shin5feb9ad2016-10-21 12:52:48 -070078void U_CALLCONV
Frank Tang1f164ee2022-11-08 12:31:27 -080079CollationRoot::load(const char* ucadataPath, UErrorCode &errorCode) {
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -080080 if(U_FAILURE(errorCode)) { return; }
81 LocalPointer<CollationTailoring> t(new CollationTailoring(NULL));
82 if(t.isNull() || t->isBogus()) {
83 errorCode = U_MEMORY_ALLOCATION_ERROR;
84 return;
85 }
Frank Tang1f164ee2022-11-08 12:31:27 -080086 t->memory = ucadataPath ? CollationRoot::loadFromFile(ucadataPath, errorCode) :
87 udata_openChoice(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "coll",
88 "icu", "ucadata",
89 CollationDataReader::isAcceptable,
90 t->version, &errorCode);
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -080091 if(U_FAILURE(errorCode)) { return; }
92 const uint8_t *inBytes = static_cast<const uint8_t *>(udata_getMemory(t->memory));
93 CollationDataReader::read(NULL, inBytes, udata_getLength(t->memory), *t, errorCode);
94 if(U_FAILURE(errorCode)) { return; }
95 ucln_i18n_registerCleanup(UCLN_I18N_COLLATION_ROOT, uprv_collation_root_cleanup);
96 CollationCacheEntry *entry = new CollationCacheEntry(Locale::getRoot(), t.getAlias());
97 if(entry != NULL) {
98 t.orphan(); // The rootSingleton took ownership of the tailoring.
99 entry->addRef();
100 rootSingleton = entry;
101 }
102}
103
104const CollationCacheEntry *
105CollationRoot::getRootCacheEntry(UErrorCode &errorCode) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800106 umtx_initOnce(initOnce, CollationRoot::load, static_cast<const char*>(NULL), errorCode);
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800107 if(U_FAILURE(errorCode)) { return NULL; }
108 return rootSingleton;
109}
110
111const CollationTailoring *
112CollationRoot::getRoot(UErrorCode &errorCode) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800113 umtx_initOnce(initOnce, CollationRoot::load, static_cast<const char*>(NULL), errorCode);
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800114 if(U_FAILURE(errorCode)) { return NULL; }
115 return rootSingleton->tailoring;
116}
117
118const CollationData *
119CollationRoot::getData(UErrorCode &errorCode) {
120 const CollationTailoring *root = getRoot(errorCode);
121 if(U_FAILURE(errorCode)) { return NULL; }
122 return root->data;
123}
124
125const CollationSettings *
126CollationRoot::getSettings(UErrorCode &errorCode) {
127 const CollationTailoring *root = getRoot(errorCode);
128 if(U_FAILURE(errorCode)) { return NULL; }
129 return root->settings;
130}
131
Frank Tang1f164ee2022-11-08 12:31:27 -0800132void
133CollationRoot::forceLoadFromFile(const char* ucadataPath, UErrorCode &errorCode) {
134 umtx_initOnce(initOnce, CollationRoot::load, ucadataPath, errorCode);
135}
136
137
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800138U_NAMESPACE_END
139
140#endif // !UCONFIG_NO_COLLATION