1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
46 #include "unicode/uversion.h"
47 #include "unicode/ucol.h"
51 typedef UCollator *(*Ptr_ucol_open)(const char *loc, UErrorCode *status);
52 typedef void (*Ptr_ucol_close)(UCollator *coll);
53 typedef UCollationResult (*Ptr_ucol_strcoll)(const UCollator *coll, const UChar *source, int32_t sourceLength, const UChar *target, int32_t targetLength);
54 typedef int32_t (*Ptr_u_strToCase)(UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode);
56 static Ptr_ucol_open ptr_ucol_open = 0;
57 static Ptr_ucol_strcoll ptr_ucol_strcoll = 0;
58 static Ptr_ucol_close ptr_ucol_close = 0;
59 static Ptr_u_strToCase ptr_u_strToUpper = 0;
60 static Ptr_u_strToCase ptr_u_strToLower = 0;
69 static LibLoadStatus status = NotLoaded;
71 static UCollator *icuCollator = 0;
73 #define STRINGIFY2(x) #x
74 #define STRINGIFY(x) STRINGIFY2(x)
76 bool qt_initIcu(const QString &localeString)
78 if (status == ErrorLoading)
81 if (status == NotLoaded) {
84 const QString version = QString::fromLatin1(U_ICU_VERSION_SHORT);
86 // QLibrary on Windows does not use the version number, the libraries
87 // are named "icuin<version>.dll", though.
88 QString libName = QStringLiteral("icuin") + version;
90 QString libName = QStringLiteral("icui18n");
92 QLibrary lib(libName, version);
94 qWarning("Unable to load library '%s' version %s: %s",
95 qPrintable(libName), qPrintable(version),
96 qPrintable(lib.errorString()));
97 status = ErrorLoading;
101 ptr_ucol_open = (Ptr_ucol_open)lib.resolve("ucol_open");
102 ptr_ucol_close = (Ptr_ucol_close)lib.resolve("ucol_close");
103 ptr_ucol_strcoll = (Ptr_ucol_strcoll)lib.resolve("ucol_strcoll");
105 if (!ptr_ucol_open || !ptr_ucol_close || !ptr_ucol_strcoll) {
106 // try again with decorated symbol names
107 ptr_ucol_open = (Ptr_ucol_open)lib.resolve("ucol_open" STRINGIFY(U_ICU_VERSION_SUFFIX));
108 ptr_ucol_close = (Ptr_ucol_close)lib.resolve("ucol_close" STRINGIFY(U_ICU_VERSION_SUFFIX));
109 ptr_ucol_strcoll = (Ptr_ucol_strcoll)lib.resolve("ucol_strcoll" STRINGIFY(U_ICU_VERSION_SUFFIX));
112 if (!ptr_ucol_open || !ptr_ucol_close || !ptr_ucol_strcoll) {
115 ptr_ucol_strcoll = 0;
117 qWarning("Unable to find symbols in '%s'.", qPrintable(libName));
118 status = ErrorLoading;
124 libName = QStringLiteral("icuuc") + version;
126 libName = QStringLiteral("icuuc");
128 QLibrary ucLib(libName, version);
130 qWarning("Unable to load library '%s' version %s: %s",
131 qPrintable(libName), qPrintable(version),
132 qPrintable(ucLib.errorString()));
133 status = ErrorLoading;
137 ptr_u_strToUpper = (Ptr_u_strToCase)ucLib.resolve("u_strToUpper");
138 ptr_u_strToLower = (Ptr_u_strToCase)ucLib.resolve("u_strToLower");
140 if (!ptr_u_strToUpper || !ptr_u_strToLower) {
141 ptr_u_strToUpper = (Ptr_u_strToCase)ucLib.resolve("u_strToUpper" STRINGIFY(U_ICU_VERSION_SUFFIX));
142 ptr_u_strToLower = (Ptr_u_strToCase)ucLib.resolve("u_strToLower" STRINGIFY(U_ICU_VERSION_SUFFIX));
145 if (!ptr_u_strToUpper || !ptr_u_strToLower) {
146 ptr_u_strToUpper = 0;
147 ptr_u_strToLower = 0;
149 qWarning("Unable to find symbols in '%s'", qPrintable(libName));
150 status = ErrorLoading;
159 ptr_ucol_close(icuCollator);
163 UErrorCode icuStatus = U_ZERO_ERROR;
164 icuCollator = ptr_ucol_open(localeString.toLatin1().constData(), &icuStatus);
167 qWarning("Unable to open locale %s in ICU, error code %d", qPrintable(localeString), icuStatus);
174 bool qt_ucol_strcoll(const QChar *source, int sourceLength, const QChar *target, int targetLength, int *result)
183 *result = ptr_ucol_strcoll(icuCollator, reinterpret_cast<const UChar *>(source), int32_t(sourceLength),
184 reinterpret_cast<const UChar *>(target), int32_t(targetLength));
189 // caseFunc can either be u_strToUpper or u_strToLower
190 static bool qt_u_strToCase(const QString &str, QString *out, const QLocale &locale, Ptr_u_strToCase caseFunc)
197 QString result(str.size(), Qt::Uninitialized);
199 UErrorCode status = U_ZERO_ERROR;
201 int32_t size = caseFunc(reinterpret_cast<UChar *>(result.data()), result.size(),
202 reinterpret_cast<const UChar *>(str.constData()), str.size(),
203 locale.bcp47Name().toLatin1().constData(), &status);
205 if (U_FAILURE(status))
208 if (size < result.size()) {
210 } else if (size > result.size()) {
211 // the resulting string is larger than our source string
214 status = U_ZERO_ERROR;
215 size = caseFunc(reinterpret_cast<UChar *>(result.data()), result.size(),
216 reinterpret_cast<const UChar *>(str.constData()), str.size(),
217 locale.bcp47Name().toLatin1().constData(), &status);
219 if (U_FAILURE(status))
222 // if the sizes don't match now, we give up.
223 if (size != result.size())
231 bool qt_u_strToUpper(const QString &str, QString *out, const QLocale &locale)
233 return qt_u_strToCase(str, out, locale, ptr_u_strToUpper);
236 bool qt_u_strToLower(const QString &str, QString *out, const QLocale &locale)
238 return qt_u_strToCase(str, out, locale, ptr_u_strToLower);