📄 qlocale.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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"#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>#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 && __GNUC_MINOR__ == 0# 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#if !defined(Q_OS_WIN) && !(defined(Q_OS_MAC) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3))#define QT_USE_DATABASE#endif#define CONVERSION_BUFF_SIZE 255#ifndef QT_QLOCALE_USES_FCVTstatic char *qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);static double qstrtod(const char *s00, char const **se, bool *ok);#endifstatic 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"static QLocale::Language codeToLanguage(const QString &code){ if (code.length() != 2) return QLocale::C; ushort uc1 = code.unicode()[0].unicode(); ushort uc2 = code.unicode()[1].unicode(); const unsigned char *c = language_code_list; for (; *c != 0; c += 2) { if (uc1 == c[0] && uc2 == c[1]) return QLocale::Language((c - language_code_list)/2); } return QLocale::C;}static QLocale::Country codeToCountry(const QString &code){ if (code.length() != 2) return QLocale::AnyCountry; ushort uc1 = code.unicode()[0].unicode(); ushort uc2 = code.unicode()[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"); QString code; code.resize(2); const unsigned char *c = language_code_list + 2*(uint(language)); code[0] = ushort(c[0]); code[1] = ushort(c[1]); 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 const QLocalePrivate *findLocale(const QString &name){ QLocale::Language lang = QLocale::C; QLocale::Country cntry = QLocale::AnyCountry; uint l = name.length(); do { if (l < 2) break; const QChar *uc = name.unicode(); if (l > 2 && uc[2] != QLatin1Char('_') && uc[2] != QLatin1Char('.') && uc[2] != QLatin1Char('@')) break; QString lang_code = name.mid(0, 2); // CLDR has changed the code for Bokmal from "no" to "nb". We want to support // both, but we have no alias mechanism in the database. if (lang_code == QLatin1String("nb")) lang_code = QLatin1String("no"); lang = codeToLanguage(lang_code); if (lang == QLocale::C) break; if (l == 2 || uc[2] == QLatin1Char('.') || uc[2] == QLatin1Char('@')) break; // we have uc[2] == '_' if (l < 5) break; if (l > 5 && uc[5] != QLatin1Char('.') && uc[5] != QLatin1Char('@')) break; cntry = codeToCountry(name.mid(3, 2)); } while (false); 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;}/******************************************************************************** Wrappers for Windows locale system functions*/#ifdef Q_OS_WINstatic QString getWinLocaleInfo(LCTYPE type){ int cnt = 0; QT_WA({ cnt = GetLocaleInfoW(LOCALE_USER_DEFAULT, type, 0, 0)*2; } , { cnt = GetLocaleInfoA(LOCALE_USER_DEFAULT, type, 0, 0); }); if (cnt == 0) { qWarning("QLocale: empty windows locale info (%d)", type); return QString(); } QByteArray buff(cnt, 0); QT_WA({ cnt = GetLocaleInfoW(LOCALE_USER_DEFAULT, type, reinterpret_cast<wchar_t*>(buff.data()), buff.size()/2); } , { cnt = GetLocaleInfoA(LOCALE_USER_DEFAULT, type, buff.data(), buff.size()); }); if (cnt == 0) { qWarning("QLocale: empty windows locale info (%d)", type); return QString(); } QString result; QT_WA({ result = QString::fromUtf16(reinterpret_cast<ushort*>(buff.data())); } , { result = QString::fromLocal8Bit(buff.data()); }); return result;}static const QLocalePrivate *systemLocale(){ static QLocalePrivate *result = 0; if (result == 0) { // Initialize with the default values for the current system locale const QLocalePrivate *plain = findLocale(QLocalePrivate::systemLocaleName()); QLocalePrivate *custom = new QLocalePrivate(*plain); // Modify it according to the custom settings QString s = getWinLocaleInfo(LOCALE_SDECIMAL); if (!s.isEmpty()) custom->m_decimal = s.at(0).unicode(); s = getWinLocaleInfo(LOCALE_STHOUSAND); if (!s.isEmpty()) custom->m_group = s.at(0).unicode(); s = getWinLocaleInfo(LOCALE_SLIST); if (!s.isEmpty()) custom->m_list = s.at(0).unicode(); s = getWinLocaleInfo(LOCALE_SNEGATIVESIGN); if (!s.isEmpty()) custom->m_minus = s.at(0).unicode(); if (!q_atomic_test_and_set_ptr(&result, 0, custom)) delete custom; } return result;}static QString systemDateToString(const QDate &date, QLocale::FormatType format){ SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wYear = date.year(); st.wMonth = date.month(); st.wDay = date.day(); DWORD flags = format == QLocale::ShortFormat ? DATE_SHORTDATE : DATE_LONGDATE; QT_WA({ TCHAR buf[255]; if (GetDateFormatW(LOCALE_USER_DEFAULT, flags, &st, 0, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetDateFormatA(LOCALE_USER_DEFAULT, flags, &st, 0, (char*)&buf, 255)) return QString::fromLocal8Bit(buf); }); return QString();}static QString systemTimeToString(const QTime &time, QLocale::FormatType){ SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wHour = time.hour(); st.wMinute = time.minute(); st.wSecond = time.second(); st.wMilliseconds = 0; DWORD flags = 0; QT_WA({ TCHAR buf[255]; if (GetTimeFormatW(LOCALE_USER_DEFAULT, flags, &st, 0, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetTimeFormatA(LOCALE_USER_DEFAULT, flags, &st, 0, (char*)&buf, 255)) return QString::fromLocal8Bit(buf); }); return QString();}static QString systemDateFormat(QLocale::FormatType format){ LCTYPE type = format == QLocale::LongFormat ? LOCALE_SLONGDATE : LOCALE_SSHORTDATE; return getWinLocaleInfo(type);}static QString systemTimeFormat(QLocale::FormatType){ return getWinLocaleInfo(LOCALE_STIMEFORMAT);}static QString systemDayName(int day, QLocale::FormatType format){ static const LCTYPE short_day_map[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 }; static const LCTYPE long_day_map[] = { LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6 }; if (day < 0 || day > 6) return QString(); LCTYPE type = format == QLocale::ShortFormat ? short_day_map[day] : long_day_map[day]; return getWinLocaleInfo(type);}static QString systemMonthName(int month, QLocale::FormatType format){ static const LCTYPE short_month_map[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6, LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 }; static const LCTYPE long_month_map[] = { LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8, LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12 }; if (month < 0 || month > 11) return QString(); LCTYPE type = format == QLocale::ShortFormat ? short_month_map[month] : long_month_map[month]; return getWinLocaleInfo(type);}static QString toQtFormat(const QString &sys_fmt){ QString result; int i = 0; while (i < sys_fmt.size()) { if (sys_fmt.at(i).unicode() == '\'') { QString text = readEscapedFormatString(sys_fmt, &i); if (text == QLatin1String("'")) result += QLatin1String("''"); else result += QChar('\'') + text + QChar('\''); continue; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -