📄 qlocale.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qplatformdefs.h"#include "qdatastream.h"#include "qstring.h"#include "qlocale.h"#include "qlocale_p.h"#include "qnamespace.h"#include "qdatetime.h"#include "qstringlist.h"#include "qvariant.h"#if defined(Q_WS_WIN)# include "qt_windows.h"# include <time.h>#endif#if !defined(QWS) && defined(Q_OS_MAC)# include "private/qcore_mac_p.h"#endif#include "private/qnumeric_p.h"#include <ctype.h>#include <float.h>#include <limits.h>#include <math.h>#include <stdlib.h>#include <qdebug.h>#include <time.h>#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)# include <fenv.h>#endif#if !defined(QT_QLOCALE_NEEDS_VOLATILE)# if defined(Q_CC_GNU)# if __GNUC__ == 4# define QT_QLOCALE_NEEDS_VOLATILE# elif defined(Q_OS_WIN)# define QT_QLOCALE_NEEDS_VOLATILE# endif# endif#endif#if defined(QT_QLOCALE_NEEDS_VOLATILE)# define NEEDS_VOLATILE volatile#else# define NEEDS_VOLATILE#endif// Sizes as defined by the ISO C99 standard - fallback#ifndef LLONG_MAX# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)#endif#ifndef LLONG_MIN# define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))#endif#ifndef ULLONG_MAX# define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff)#endif#define CONVERSION_BUFF_SIZE 255#ifndef QT_QLOCALE_USES_FCVTstatic char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);#endifQ_CORE_EXPORT char *qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);Q_CORE_EXPORT double qstrtod(const char *s00, char const **se, bool *ok);Q_CORE_EXPORT qlonglong qstrtoll(const char *nptr, const char **endptr, register int base, bool *ok);static qulonglong qstrtoull(const char *nptr, const char **endptr, register int base, bool *ok);/******************************************************************************** Helpers for accessing Qt locale database*/#include "qlocale_data_p.h"// Assumes that code is a// QChar code[3];// If the code is two-digit the third digit must be 0static QLocale::Language codeToLanguage(const QChar *code){ ushort uc1 = code[0].unicode(); ushort uc2 = code[1].unicode(); ushort uc3 = code[2].unicode(); if (uc1 == 'n' && uc2 == 'o' && uc3 == 0) uc2 = 'b'; const unsigned char *c = language_code_list; for (; *c != 0; c += 3) { if (uc1 == c[0] && uc2 == c[1] && uc3 == c[2]) return QLocale::Language((c - language_code_list)/3); } return QLocale::C;}// Assumes that code is a// QChar code[2];static QLocale::Country codeToCountry(const QChar *code){ ushort uc1 = code[0].unicode(); ushort uc2 = code[1].unicode(); const unsigned char *c = country_code_list; for (; *c != 0; c += 2) { if (uc1 == c[0] && uc2 == c[1]) return QLocale::Country((c - country_code_list)/2); } return QLocale::AnyCountry;}static QString languageToCode(QLocale::Language language){ if (language == QLocale::C) return QLatin1String("C"); const unsigned char *c = language_code_list + 3*(uint(language)); QString code; code.resize(c[2] == 0 ? 2 : 3); code[0] = ushort(c[0]); code[1] = ushort(c[1]); if (c[2] != 0) code[2] = ushort(c[2]); return code;}static QString countryToCode(QLocale::Country country){ if (country == QLocale::AnyCountry) return QString(); QString code; code.resize(2); const unsigned char *c = country_code_list + 2*(uint(country)); code[0] = ushort(c[0]); code[1] = ushort(c[1]); return code;}static const QLocalePrivate *findLocale(QLocale::Language language, QLocale::Country country){ unsigned language_id = language; unsigned country_id = country; uint idx = locale_index[language_id]; const QLocalePrivate *d = locale_data + idx; if (idx == 0) // default language has no associated country return d; if (country == QLocale::AnyCountry) return d; Q_ASSERT(d->languageId() == language_id); while (d->languageId() == language_id && d->countryId() != country_id) ++d; if (d->countryId() == country_id && d->languageId() == language_id) return d; return locale_data + idx;}static bool splitLocaleName(const QString &name, QChar *lang_begin, QChar *cntry_begin){ for (int i = 0; i < 3; ++i) lang_begin[i] = 0; for (int i = 0; i < 2; ++i) cntry_begin[i] = 0; int l = name.length(); QChar *lang = lang_begin; QChar *cntry = cntry_begin; int state = 0; const QChar *uc = name.unicode(); for (int i = 0; i < l; ++i) { if (uc->unicode() == '.' || uc->unicode() == '@') break; switch (state) { case 0: // parsing language if (uc->unicode() == '_') { state = 1; break; } if (lang - lang_begin == 3) return false; if (uc->unicode() < 'a' || uc->unicode() > 'z') return false; *lang = *uc; ++lang; break; case 1: // parsing country if (cntry - cntry_begin == 2) { cntry_begin[0] = 0; break; } *cntry = *uc; ++cntry; break; } ++uc; } int lang_len = lang - lang_begin; return lang_len == 2 || lang_len == 3;}static void getLangAndCountry(const QString &name, QLocale::Language &lang, QLocale::Country &cntry){ lang = QLocale::C; cntry = QLocale::AnyCountry; QChar lang_code[3]; QChar cntry_code[2]; if (!splitLocaleName(name, lang_code, cntry_code)) return; lang = codeToLanguage(lang_code); if (lang == QLocale::C) return; if (cntry_code[0].unicode() != 0) cntry = codeToCountry(cntry_code);}static const QLocalePrivate *findLocale(const QString &name){ QLocale::Language lang; QLocale::Country cntry; getLangAndCountry(name, lang, cntry); return findLocale(lang, cntry);}static QString readEscapedFormatString(const QString &format, int *idx){ int &i = *idx; Q_ASSERT(format.at(i).unicode() == '\''); ++i; if (i == format.size()) return QString(); if (format.at(i).unicode() == '\'') { // "''" outside of a quoted stirng ++i; return QLatin1String("'"); } QString result; while (i < format.size()) { if (format.at(i).unicode() == '\'') { if (i + 1 < format.size() && format.at(i + 1).unicode() == '\'') { // "''" inside of a quoted string result.append(QLatin1Char('\'')); i += 2; } else { break; } } else { result.append(format.at(i++)); } } if (i < format.size()) ++i; return result;}static int repeatCount(const QString &s, int i){ QChar c = s.at(i); int j = i + 1; while (j < s.size() && s.at(j) == c) ++j; return j - i;}static const QLocalePrivate *default_lp = 0;static uint default_number_options = 0;#ifndef QT_NO_SYSTEMLOCALEstatic QByteArray envVarLocale(){ static QByteArray lang = 0;#ifdef Q_OS_UNIX lang = qgetenv("LC_ALL"); if (lang.isNull()) lang = qgetenv("LC_NUMERIC"); if (lang.isNull())#endif lang = qgetenv("LANG"); return lang;}#if defined(Q_OS_WIN)/******************************************************************************** Wrappers for Windows locale system functions*/static const char *winLangCodeToIsoName(int code);static QString winIso639LangName(LCID id = LOCALE_USER_DEFAULT);static QString winIso3116CtryName(LCID id = LOCALE_USER_DEFAULT);static QString getWinLocaleInfo(LCTYPE type){ int cnt = 0; LCID id = GetThreadLocale(); QT_WA({ cnt = GetLocaleInfoW(id, type, 0, 0)*2; } , { cnt = GetLocaleInfoA(id, type, 0, 0); }); if (cnt == 0) { qWarning("QLocale: empty windows locale info (%d)", (int)type); return QString(); } QByteArray buff(cnt, 0); QT_WA({ cnt = GetLocaleInfoW(id, type, reinterpret_cast<wchar_t*>(buff.data()), buff.size()/2); } , { cnt = GetLocaleInfoA(id, type, buff.data(), buff.size()); }); if (cnt == 0) { qWarning("QLocale: empty windows locale info (%d)", (int)type); return QString(); } QString result; QT_WA({ result = QString::fromUtf16(reinterpret_cast<ushort*>(buff.data())); } , { result = QString::fromLocal8Bit(buff.data()); }); return result;}QByteArray getWinLocaleName(LCID id = LOCALE_USER_DEFAULT){ QByteArray result; if (id == LOCALE_USER_DEFAULT) { result = envVarLocale(); if ( !result.isEmpty() ) { long id = 0; bool ok = false; id = qstrtoll(result.data(), 0, 0, &ok); if ( !ok || id == 0 || id < INT_MIN || id > INT_MAX ) return result; else return winLangCodeToIsoName( (int)id ); } } if (QSysInfo::WindowsVersion == QSysInfo::WV_95) { result = winLangCodeToIsoName(id != LOCALE_USER_DEFAULT ? id : GetThreadLocale()); } else { if (id == LOCALE_USER_DEFAULT) id = GetThreadLocale(); QString resultuage = winIso639LangName(id); QString country = winIso3116CtryName(id); result += resultuage.toLatin1(); if (!country.isEmpty()) { result += '_'; result += country.toLatin1(); } } return result;}Q_CORE_EXPORT QLocale qt_localeFromLCID(LCID id){ return QLocale(QString::fromLatin1(getWinLocaleName(id)));}static QString winToQtFormat(const QString &sys_fmt){ QString result; int i = 0; while (i < sys_fmt.size()) { if (sys_fmt.at(i).unicode() == QLatin1Char('\'')) { QString text = readEscapedFormatString(sys_fmt, &i); if (text == QLatin1String("'")) result += QLatin1String("''"); else result += QLatin1Char('\'') + text + QLatin1Char('\''); continue; } QChar c = sys_fmt.at(i); int repeat = repeatCount(sys_fmt, i); switch (c.unicode()) { // Date case 'y': if (repeat > 5) repeat = 5; else if (repeat == 3) repeat = 2; switch (repeat) { case 1: result += QLatin1String("yy"); // "y" unsupported by Qt, use "yy" break; case 5: result += QLatin1String("yyyy"); // "yyyyy" same as "yyyy" on Windows break; default: result += QString(repeat, QLatin1Char('y')); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -