Qt 4.8
Macros | Typedefs | Enumerations | Functions | Variables
qlocale_icu.cpp File Reference
#include "qglobal.h"
#include "qlibrary.h"
#include "qdebug.h"
#include "unicode/uversion.h"
#include "unicode/ucol.h"

Go to the source code of this file.

Macros

#define STRINGIFY(x)   STRINGIFY2(x)
 
#define STRINGIFY2(x)   #x
 

Typedefs

typedef int32_t(* Ptr_u_strToCase) (UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode)
 
typedef void(* Ptr_ucol_close) (UCollator *coll)
 
typedef UCollator *(* Ptr_ucol_open) (const char *loc, UErrorCode *status)
 
typedef UCollationResult(* Ptr_ucol_strcoll) (const UCollator *coll, const UChar *source, int32_t sourceLength, const UChar *target, int32_t targetLength)
 

Enumerations

enum  LibLoadStatus { ErrorLoading = -1, NotLoaded = 0, Loaded = 1 }
 

Functions

bool qt_initIcu (const QString &localeString)
 
static bool qt_u_strToCase (const QString &str, QString *out, const QLocale &locale, Ptr_u_strToCase caseFunc)
 
bool qt_u_strToLower (const QString &str, QString *out, const QLocale &locale)
 
bool qt_u_strToUpper (const QString &str, QString *out, const QLocale &locale)
 
bool qt_ucol_strcoll (const QChar *source, int sourceLength, const QChar *target, int targetLength, int *result)
 

Variables

static UCollator * icuCollator = 0
 
static Ptr_u_strToCase ptr_u_strToLower = 0
 
static Ptr_u_strToCase ptr_u_strToUpper = 0
 
static Ptr_ucol_close ptr_ucol_close = 0
 
static Ptr_ucol_open ptr_ucol_open = 0
 
static Ptr_ucol_strcoll ptr_ucol_strcoll = 0
 
static LibLoadStatus status = NotLoaded
 

Macro Definition Documentation

◆ STRINGIFY

#define STRINGIFY (   x)    STRINGIFY2(x)

Definition at line 74 of file qlocale_icu.cpp.

Referenced by qt_initIcu().

◆ STRINGIFY2

#define STRINGIFY2 (   x)    #x

Definition at line 73 of file qlocale_icu.cpp.

Typedef Documentation

◆ Ptr_u_strToCase

typedef int32_t(* Ptr_u_strToCase) (UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode)

Definition at line 54 of file qlocale_icu.cpp.

◆ Ptr_ucol_close

typedef void(* Ptr_ucol_close) (UCollator *coll)

Definition at line 52 of file qlocale_icu.cpp.

◆ Ptr_ucol_open

typedef UCollator*(* Ptr_ucol_open) (const char *loc, UErrorCode *status)

Definition at line 51 of file qlocale_icu.cpp.

◆ Ptr_ucol_strcoll

typedef UCollationResult(* Ptr_ucol_strcoll) (const UCollator *coll, const UChar *source, int32_t sourceLength, const UChar *target, int32_t targetLength)

Definition at line 53 of file qlocale_icu.cpp.

Enumeration Type Documentation

◆ LibLoadStatus

Enumerator
ErrorLoading 
NotLoaded 
Loaded 

Definition at line 62 of file qlocale_icu.cpp.

63 {
64  ErrorLoading = -1,
65  NotLoaded = 0,
66  Loaded = 1
67 };

Function Documentation

◆ qt_initIcu()

bool qt_initIcu ( const QString localeString)

Definition at line 76 of file qlocale_icu.cpp.

Referenced by QLocale::setDefault(), and QLocalePrivate::updateSystemPrivate().

77 {
78  if (status == ErrorLoading)
79  return false;
80 
81  if (status == NotLoaded) {
82 
83  // resolve libicui18n
84  QLibrary lib(QLatin1String("icui18n"), QLatin1String(U_ICU_VERSION_SHORT));
85  lib.setLoadHints(QLibrary::ImprovedSearchHeuristics);
86  if (!lib.load()) {
87  qWarning() << "Unable to load library icui18n" << lib.errorString();
89  return false;
90  }
91 
92  ptr_ucol_open = (Ptr_ucol_open)lib.resolve("ucol_open");
93  ptr_ucol_close = (Ptr_ucol_close)lib.resolve("ucol_close");
94  ptr_ucol_strcoll = (Ptr_ucol_strcoll)lib.resolve("ucol_strcoll");
95 
97  // try again with decorated symbol names
98  ptr_ucol_open = (Ptr_ucol_open)lib.resolve("ucol_open" STRINGIFY(U_ICU_VERSION_SUFFIX));
99  ptr_ucol_close = (Ptr_ucol_close)lib.resolve("ucol_close" STRINGIFY(U_ICU_VERSION_SUFFIX));
100  ptr_ucol_strcoll = (Ptr_ucol_strcoll)lib.resolve("ucol_strcoll" STRINGIFY(U_ICU_VERSION_SUFFIX));
101  }
102 
104  ptr_ucol_open = 0;
105  ptr_ucol_close = 0;
106  ptr_ucol_strcoll = 0;
107 
108  qWarning("Unable to find symbols in icui18n");
110  return false;
111  }
112 
113  // resolve libicuuc
114  QLibrary ucLib(QLatin1String("icuuc"), QLatin1String(U_ICU_VERSION_SHORT));
115  ucLib.setLoadHints(QLibrary::ImprovedSearchHeuristics);
116  if (!ucLib.load()) {
117  qWarning() << "Unable to load library icuuc" << ucLib.errorString();
119  return false;
120  }
121 
122  ptr_u_strToUpper = (Ptr_u_strToCase)ucLib.resolve("u_strToUpper");
123  ptr_u_strToLower = (Ptr_u_strToCase)ucLib.resolve("u_strToLower");
124 
126  ptr_u_strToUpper = (Ptr_u_strToCase)ucLib.resolve("u_strToUpper" STRINGIFY(U_ICU_VERSION_SUFFIX));
127  ptr_u_strToLower = (Ptr_u_strToCase)ucLib.resolve("u_strToLower" STRINGIFY(U_ICU_VERSION_SUFFIX));
128  }
129 
131  ptr_u_strToUpper = 0;
132  ptr_u_strToLower = 0;
133 
134  qWarning("Unable to find symbols in icuuc");
136  return false;
137  }
138 
139  // success :)
140  status = Loaded;
141  }
142 
143  if (icuCollator) {
145  icuCollator = 0;
146  }
147 
148  UErrorCode icuStatus = U_ZERO_ERROR;
149  icuCollator = ptr_ucol_open(localeString.toLatin1().constData(), &icuStatus);
150 
151  if (!icuCollator) {
152  qWarning("Unable to open locale %s in ICU, error code %d", qPrintable(localeString), icuStatus);
153  return false;
154  }
155 
156  return true;
157 }
static Ptr_ucol_open ptr_ucol_open
Definition: qlocale_icu.cpp:56
static Ptr_u_strToCase ptr_u_strToLower
Definition: qlocale_icu.cpp:60
static UCollator * icuCollator
Definition: qlocale_icu.cpp:71
static Ptr_ucol_close ptr_ucol_close
Definition: qlocale_icu.cpp:58
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
#define STRINGIFY(x)
Definition: qlocale_icu.cpp:74
UCollator *(* Ptr_ucol_open)(const char *loc, UErrorCode *status)
Definition: qlocale_icu.cpp:51
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void(* Ptr_ucol_close)(UCollator *coll)
Definition: qlocale_icu.cpp:52
UCollationResult(* Ptr_ucol_strcoll)(const UCollator *coll, const UChar *source, int32_t sourceLength, const UChar *target, int32_t targetLength)
Definition: qlocale_icu.cpp:53
static Ptr_ucol_strcoll ptr_ucol_strcoll
Definition: qlocale_icu.cpp:57
int32_t(* Ptr_u_strToCase)(UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode)
Definition: qlocale_icu.cpp:54
#define qPrintable(string)
Definition: qglobal.h:1750
static Ptr_u_strToCase ptr_u_strToUpper
Definition: qlocale_icu.cpp:59
The QLibrary class loads shared libraries at runtime.
Definition: qlibrary.h:62

◆ qt_u_strToCase()

static bool qt_u_strToCase ( const QString str,
QString out,
const QLocale locale,
Ptr_u_strToCase  caseFunc 
)
static

Definition at line 175 of file qlocale_icu.cpp.

Referenced by qt_u_strToLower(), and qt_u_strToUpper().

176 {
177  Q_ASSERT(out);
178 
179  if (!icuCollator)
180  return false;
181 
182  QString result(str.size(), Qt::Uninitialized);
183 
184  UErrorCode status = U_ZERO_ERROR;
185 
186  int32_t size = caseFunc(reinterpret_cast<UChar *>(result.data()), result.size(),
187  reinterpret_cast<const UChar *>(str.constData()), str.size(),
188  locale.bcp47Name().toLatin1().constData(), &status);
189 
190  if (U_FAILURE(status))
191  return false;
192 
193  if (size < result.size()) {
194  result.resize(size);
195  } else if (size > result.size()) {
196  // the resulting string is larger than our source string
197  result.resize(size);
198 
199  status = U_ZERO_ERROR;
200  size = caseFunc(reinterpret_cast<UChar *>(result.data()), result.size(),
201  reinterpret_cast<const UChar *>(str.constData()), str.size(),
202  locale.bcp47Name().toLatin1().constData(), &status);
203 
204  if (U_FAILURE(status))
205  return false;
206 
207  // if the sizes don't match now, we give up.
208  if (size != result.size())
209  return false;
210  }
211 
212  *out = result;
213  return true;
214 }
static UCollator * icuCollator
Definition: qlocale_icu.cpp:71
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
QString bcp47Name() const
Returns the dash-separated language, script and country (and possibly other BCP47 fields) of this loc...
Definition: qlocale.cpp:1012

◆ qt_u_strToLower()

bool qt_u_strToLower ( const QString str,
QString out,
const QLocale locale 
)

Definition at line 221 of file qlocale_icu.cpp.

Referenced by QLocale::toLower().

222 {
223  return qt_u_strToCase(str, out, locale, ptr_u_strToLower);
224 }
static Ptr_u_strToCase ptr_u_strToLower
Definition: qlocale_icu.cpp:60
static bool qt_u_strToCase(const QString &str, QString *out, const QLocale &locale, Ptr_u_strToCase caseFunc)

◆ qt_u_strToUpper()

bool qt_u_strToUpper ( const QString str,
QString out,
const QLocale locale 
)

Definition at line 216 of file qlocale_icu.cpp.

Referenced by QLocale::toUpper().

217 {
218  return qt_u_strToCase(str, out, locale, ptr_u_strToUpper);
219 }
static bool qt_u_strToCase(const QString &str, QString *out, const QLocale &locale, Ptr_u_strToCase caseFunc)
static Ptr_u_strToCase ptr_u_strToUpper
Definition: qlocale_icu.cpp:59

◆ qt_ucol_strcoll()

bool qt_ucol_strcoll ( const QChar source,
int  sourceLength,
const QChar target,
int  targetLength,
int *  result 
)

Definition at line 159 of file qlocale_icu.cpp.

Referenced by QString::localeAwareCompare_helper().

160 {
161  Q_ASSERT(result);
162  Q_ASSERT(source);
163  Q_ASSERT(target);
164 
165  if (!icuCollator)
166  return false;
167 
168  *result = ptr_ucol_strcoll(icuCollator, reinterpret_cast<const UChar *>(source), int32_t(sourceLength),
169  reinterpret_cast<const UChar *>(target), int32_t(targetLength));
170 
171  return true;
172 }
static UCollator * icuCollator
Definition: qlocale_icu.cpp:71
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static Ptr_ucol_strcoll ptr_ucol_strcoll
Definition: qlocale_icu.cpp:57

Variable Documentation

◆ icuCollator

UCollator* icuCollator = 0
static

Definition at line 71 of file qlocale_icu.cpp.

Referenced by qt_initIcu(), qt_u_strToCase(), and qt_ucol_strcoll().

◆ ptr_u_strToLower

Ptr_u_strToCase ptr_u_strToLower = 0
static

Definition at line 60 of file qlocale_icu.cpp.

Referenced by qt_initIcu(), and qt_u_strToLower().

◆ ptr_u_strToUpper

Ptr_u_strToCase ptr_u_strToUpper = 0
static

Definition at line 59 of file qlocale_icu.cpp.

Referenced by qt_initIcu(), and qt_u_strToUpper().

◆ ptr_ucol_close

Ptr_ucol_close ptr_ucol_close = 0
static

Definition at line 58 of file qlocale_icu.cpp.

Referenced by qt_initIcu().

◆ ptr_ucol_open

Ptr_ucol_open ptr_ucol_open = 0
static

Definition at line 56 of file qlocale_icu.cpp.

Referenced by qt_initIcu().

◆ ptr_ucol_strcoll

Ptr_ucol_strcoll ptr_ucol_strcoll = 0
static

Definition at line 57 of file qlocale_icu.cpp.

Referenced by qt_initIcu(), and qt_ucol_strcoll().

◆ status

LibLoadStatus status = NotLoaded
static

Definition at line 69 of file qlocale_icu.cpp.

Referenced by accessibilityEventHandler(), add(), QMacPrintEngine::begin(), bidiItemize(), QAhiScreen::blit(), QGLFramebufferObjectPrivate::checkFramebufferStatus(), clear(), commonEdge(), QAhiScreen::configure(), QAhiScreen::connect(), QCoreWlanEngine::connectToId(), QPSQLDriverPrivate::detectBackslashEscape(), enableHarfBuzz(), QIODevice::errorString(), QWingedEdge::findInsertStatus(), fmtDateTime(), functor(), QPSQLDriverPrivate::getPSQLVersion(), QFontEngineMac::getSfntTable(), QFileIconProviderPrivate::getWinIcon(), QVistaHelper::handleWinEvent(), QmlJSDebugger::QDeclarativeViewInspectorPrivate::highlight(), QAhiScreen::initDevice(), QAhiScreenCursor::move(), QAInterface::navigate(), QMacPrintEnginePrivate::newPage_helper(), QMYSQLResult::nextResult(), QDeclarativeXmlListModelPrivate::notifyQueryStarted(), QDateTimeParser::parseFormat(), QEventDispatcherMac::processEvents(), QPSQLResultPrivate::processResults(), QMacPrintEngine::property(), qDBusPropertySet(), QFontEngineMacMulti::QFontEngineMacMulti(), qt_initIcu(), QDBusAbstractInterfaceBase::qt_metacall(), qt_parseNsswitchPrintersEntry(), qt_u_strToCase(), qTraverseKdPointTree(), QMetaProperty::read(), QOCICols::readPiecewise(), QFontEngineQPF::remapFontData(), QWingedEdge::removeEdge(), QSslSocketPrivate::resetDefaultCiphers(), QDeclarativeVME::run(), QDeclarativeCompiledBindingsPrivate::run(), QNetworkAccessHttpBackend::sendCacheContents(), QAhiScreenCursor::set(), QPSQLDriverPrivate::setDatestyle(), QPSQLDriverPrivate::setEncodingUtf8(), QAhiScreen::setMode(), QAhiScreenPrivate::setMode(), QThread::setPriority(), QMacPrintEngine::setProperty(), QDeclarativeObjectScriptClass::setProperty(), QPictureIO::setStatus(), QWidgetPrivate::setWindowTitle_sys(), QAhiScreenCursor::show(), QSystemTrayIconPrivate::showMessage_sys(), QAhiScreen::solidFill(), QMacPrintEnginePrivate::supportedResolutions(), QSslSocketPrivate::systemCaCertificates(), traverse(), unquote(), QDeclarativeBinding::update(), QPropertyAnimationPrivate::updateProperty(), QSqlField::value(), QDeclarativeInspectorService::views(), QDeclarativePropertyPrivate::write(), QDeclarativePointFValueType::write(), QIODevice::write(), QDeclarativePointValueType::write(), QDeclarativeSizeFValueType::write(), QDeclarativeSizeValueType::write(), QDeclarativeRectFValueType::write(), QMetaProperty::write(), QDeclarativeRectValueType::write(), QDeclarativeVector2DValueType::write(), QDeclarativeVector3DValueType::write(), QDeclarativeVector4DValueType::write(), QDeclarativeQuaternionValueType::write(), QDeclarativeMatrix4x4ValueType::write(), QDeclarativeEasingValueType::write(), QDeclarativeFontValueType::write(), QDeclarativePropertyPrivate::writeEnumProperty(), and QXIMInputContext::x11FilterEvent().