Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qlocale_icu.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qglobal.h"
43 #include "qlibrary.h"
44 #include "qdebug.h"
45
46 #include "unicode/uversion.h"
47 #include "unicode/ucol.h"
48
49 QT_BEGIN_NAMESPACE
50
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);
55
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;
61
62 enum LibLoadStatus
63 {
64     ErrorLoading = -1,
65     NotLoaded = 0,
66     Loaded = 1
67 };
68
69 static LibLoadStatus status = NotLoaded;
70
71 static UCollator *icuCollator = 0;
72
73 #define STRINGIFY2(x) #x
74 #define STRINGIFY(x) STRINGIFY2(x)
75
76 bool qt_initIcu(const QString &localeString)
77 {
78     if (status == ErrorLoading)
79         return false;
80
81     if (status == NotLoaded) {
82
83         // resolve libicui18n
84         const QString version = QString::fromLatin1(U_ICU_VERSION_SHORT);
85 #ifdef Q_OS_WIN
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;
89 #else
90         QString libName = QStringLiteral("icui18n");
91 #endif
92         QLibrary lib(libName, version);
93         if (!lib.load()) {
94             qWarning("Unable to load library '%s' version %s: %s",
95                      qPrintable(libName), qPrintable(version),
96                      qPrintable(lib.errorString()));
97             status = ErrorLoading;
98             return false;
99         }
100
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");
104
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));
110         }
111
112         if (!ptr_ucol_open || !ptr_ucol_close || !ptr_ucol_strcoll) {
113             ptr_ucol_open = 0;
114             ptr_ucol_close = 0;
115             ptr_ucol_strcoll = 0;
116
117             qWarning("Unable to find symbols in '%s'.", qPrintable(libName));
118             status = ErrorLoading;
119             return false;
120         }
121
122         // resolve libicuuc
123 #ifdef Q_OS_WIN
124         libName = QStringLiteral("icuuc") + version;
125 #else
126         libName = QStringLiteral("icuuc");
127 #endif
128         QLibrary ucLib(libName, version);
129         if (!ucLib.load()) {
130             qWarning("Unable to load library '%s' version %s: %s",
131                      qPrintable(libName), qPrintable(version),
132                      qPrintable(ucLib.errorString()));
133             status = ErrorLoading;
134             return false;
135         }
136
137         ptr_u_strToUpper = (Ptr_u_strToCase)ucLib.resolve("u_strToUpper");
138         ptr_u_strToLower = (Ptr_u_strToCase)ucLib.resolve("u_strToLower");
139
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));
143         }
144
145         if (!ptr_u_strToUpper || !ptr_u_strToLower) {
146             ptr_u_strToUpper = 0;
147             ptr_u_strToLower = 0;
148
149             qWarning("Unable to find symbols in '%s'", qPrintable(libName));
150             status = ErrorLoading;
151             return false;
152         }
153
154         // success :)
155         status = Loaded;
156     }
157
158     if (icuCollator) {
159         ptr_ucol_close(icuCollator);
160         icuCollator = 0;
161     }
162
163     UErrorCode icuStatus = U_ZERO_ERROR;
164     icuCollator = ptr_ucol_open(localeString.toLatin1().constData(), &icuStatus);
165
166     if (!icuCollator) {
167         qWarning("Unable to open locale %s in ICU, error code %d", qPrintable(localeString), icuStatus);
168         return false;
169     }
170
171     return true;
172 }
173
174 bool qt_ucol_strcoll(const QChar *source, int sourceLength, const QChar *target, int targetLength, int *result)
175 {
176     Q_ASSERT(result);
177     Q_ASSERT(source);
178     Q_ASSERT(target);
179
180     if (!icuCollator)
181         return false;
182
183     *result = ptr_ucol_strcoll(icuCollator, reinterpret_cast<const UChar *>(source), int32_t(sourceLength),
184                                reinterpret_cast<const UChar *>(target), int32_t(targetLength));
185
186     return true;
187 }
188
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)
191 {
192     Q_ASSERT(out);
193
194     if (!icuCollator)
195         return false;
196
197     QString result(str.size(), Qt::Uninitialized);
198
199     UErrorCode status = U_ZERO_ERROR;
200
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);
204
205     if (U_FAILURE(status))
206         return false;
207
208     if (size < result.size()) {
209         result.resize(size);
210     } else if (size > result.size()) {
211         // the resulting string is larger than our source string
212         result.resize(size);
213
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);
218
219         if (U_FAILURE(status))
220             return false;
221
222         // if the sizes don't match now, we give up.
223         if (size != result.size())
224             return false;
225     }
226
227     *out = result;
228     return true;
229 }
230
231 bool qt_u_strToUpper(const QString &str, QString *out, const QLocale &locale)
232 {
233     return qt_u_strToCase(str, out, locale, ptr_u_strToUpper);
234 }
235
236 bool qt_u_strToLower(const QString &str, QString *out, const QLocale &locale)
237 {
238     return qt_u_strToCase(str, out, locale, ptr_u_strToLower);
239 }
240
241 QT_END_NAMESPACE