Qt 4.8
Macros | Functions | Variables
qlocale.cpp File Reference
#include "qglobal.h"
#include "private/qcore_mac_p.h"
#include <CoreFoundation/CoreFoundation.h>
#include "qplatformdefs.h"
#include "qdatastream.h"
#include "qstring.h"
#include "qlocale.h"
#include "qlocale_p.h"
#include "qlocale_tools_p.h"
#include "qdatetime_p.h"
#include "qnamespace.h"
#include "qdatetime.h"
#include "qstringlist.h"
#include "qvariant.h"
#include "qstringbuilder.h"
#include "qt_windows.h"
#include <time.h>
#include "private/qnumeric_p.h"
#include "private/qsystemlibrary_p.h"
#include "qlocale_data_p.h"

Go to the source code of this file.

Macros

#define QT_MAX_FLOAT   3.4028234663852886e+38
 Returns the float represented by the localized string s, or 0.0 if the conversion failed. More...
 

Functions

static const QLocalePrivatedataPointerHelper (quint16 index)
 
static const QLocalePrivatedefaultPrivate ()
 
static const QLocalePrivatefindLocale (const QString &name)
 
static QString getLocaleData (const ushort *data, int size)
 
static QString getLocaleListData (const ushort *data, int size, int index)
 
static quint16 localePrivateIndex (const QLocalePrivate *p)
 
QDataStreamoperator<< (QDataStream &ds, const QLocale &l)
 
QDataStreamoperator>> (QDataStream &ds, QLocale &l)
 
static bool parse_locale_tag (const QString &input, int &i, QString *result, const QString &separators)
 
static bool qIsUpper (char c)
 
static QSystemLocaleQSystemLocale_globalSystemLocale ()
 
QString qt_readEscapedFormatString (const QString &format, int *idx)
 
int qt_repeatCount (const QString &s, int i)
 
bool qt_splitLocaleName (const QString &name, QString &lang, QString &script, QString &cntry)
 
static char qToLower (char c)
 
static const QSystemLocalesystemLocale ()
 
static const QLocalePrivatesystemPrivate ()
 
static bool timeFormatContainsAP (const QString &format)
 
static QString timeZone ()
 

Variables

static QSystemLocale_systemLocale = 0
 
static const QLocalePrivatedefault_lp = 0
 
static uint default_number_options = 0
 
static const int locale_data_size = sizeof(locale_data)/sizeof(QLocalePrivate) - 1
 
static QLocalePrivatesystem_lp = 0
 

Macro Definition Documentation

◆ QT_MAX_FLOAT

#define QT_MAX_FLOAT   3.4028234663852886e+38

Returns the float represented by the localized string s, or 0.0 if the conversion failed.

If ok is not 0, reports failure by setting ok to false and success by setting *ok to true.

This function ignores leading and trailing whitespace.

See also
toDouble(), toInt(), toString()

Definition at line 1242 of file qlocale.cpp.

Referenced by QLocale::toFloat().

Function Documentation

◆ dataPointerHelper()

static const QLocalePrivate* dataPointerHelper ( quint16  index)
static

Definition at line 598 of file qlocale.cpp.

Referenced by QLocale::d().

599 {
600 #ifndef QT_NO_SYSTEMLOCALE
602  if (index == locale_data_size)
603  return system_lp;
604 #else
606 #endif
607 
608  return &locale_data[index];
609 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static QLocalePrivate * system_lp
Definition: qlocale.cpp:85
quint16 index
static const QLocalePrivate locale_data[]
static const int locale_data_size
Definition: qlocale.cpp:596

◆ defaultPrivate()

static const QLocalePrivate* defaultPrivate ( )
static

Definition at line 548 of file qlocale.cpp.

Referenced by QLocale::QLocale().

549 {
550  if (!default_lp)
552  return default_lp;
553 }
static const QLocalePrivate * systemPrivate()
Definition: qlocale.cpp:535
static const QLocalePrivate * default_lp
Definition: qlocale.cpp:430

◆ findLocale()

static const QLocalePrivate* findLocale ( const QString name)
static

Definition at line 377 of file qlocale.cpp.

Referenced by QLocalePrivate::countryId(), and QLocale::QLocale().

378 {
379  QLocale::Language lang;
381  QLocale::Country cntry;
382  QLocalePrivate::getLangAndCountry(name, lang, script, cntry);
383 
384  return QLocalePrivate::findLocale(lang, script, cntry);
385 }
static void getLangAndCountry(const QString &name, QLocale::Language &lang, QLocale::Script &script, QLocale::Country &cntry)
Definition: qlocale.cpp:357
static const QLocalePrivate * findLocale(QLocale::Language language, QLocale::Script script, QLocale::Country country)
Definition: qlocale.cpp:241
Language
Definition: qlocale.h:148
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)

◆ getLocaleData()

static QString getLocaleData ( const ushort data,
int  size 
)
inlinestatic

Definition at line 573 of file qlocale.cpp.

Referenced by QLocale::amText(), QLocale::createSeparatedList(), QLocale::currencySymbol(), QLocale::dateFormat(), QLocale::nativeCountryName(), QLocale::nativeLanguageName(), QLocale::pmText(), QLocale::timeFormat(), and QLocale::toCurrencyString().

574 {
575  return size ? QString::fromRawData(reinterpret_cast<const QChar*>(data), size) : QString();
576 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString fromRawData(const QChar *, int size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
Definition: qstring.cpp:7673
static const char * data(const QByteArray &arr)

◆ getLocaleListData()

static QString getLocaleListData ( const ushort data,
int  size,
int  index 
)
static

Definition at line 555 of file qlocale.cpp.

Referenced by QLocale::currencySymbol(), QLocale::dayName(), QLocale::monthName(), QLocale::standaloneDayName(), and QLocale::standaloneMonthName().

556 {
557  static const ushort separator = ';';
558  while (index && size > 0) {
559  while (*data != separator)
560  ++data, --size;
561  --index;
562  ++data;
563  --size;
564  }
565  const ushort *end = data;
566  while (size > 0 && *end != separator)
567  ++end, --size;
568  if (end-data == 0)
569  return QString();
570  return QString::fromRawData(reinterpret_cast<const QChar*>(data), end-data);
571 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString fromRawData(const QChar *, int size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
Definition: qstring.cpp:7673
static const char * data(const QByteArray &arr)
unsigned short ushort
Definition: qglobal.h:995
quint16 index
static const KeyPair *const end

◆ localePrivateIndex()

static quint16 localePrivateIndex ( const QLocalePrivate p)
static

Definition at line 611 of file qlocale.cpp.

Referenced by QLocale::matchingLocales(), QLocale::QLocale(), and QLocale::system().

612 {
613 #ifndef QT_NO_SYSTEMLOCALE
615  || (p != 0 && p == system_lp));
617 #else
618  Q_ASSERT(p >= locale_data && p - locale_data < locale_data_size);
619  quint16 index = p - locale_data;
620 #endif
621 
622  return index;
623 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
unsigned short quint16
Definition: qglobal.h:936
static QLocalePrivate * system_lp
Definition: qlocale.cpp:85
quint16 index
static const QLocalePrivate locale_data[]
static const int locale_data_size
Definition: qlocale.cpp:596

◆ operator<<()

QDataStream& operator<< ( QDataStream ds,
const QLocale l 
)

Definition at line 580 of file qlocale.cpp.

Referenced by QLocale::toCurrencyString().

581 {
582  ds << l.name();
583  return ds;
584 }
QString name() const
Returns the language and country of this locale as a string of the form "language_country", where language is a lowercase, two-letter ISO 639 language code, and country is an uppercase, two- or three-letter ISO 3166 country code.
Definition: qlocale.cpp:963

◆ operator>>()

QDataStream& operator>> ( QDataStream ds,
QLocale l 
)

Definition at line 586 of file qlocale.cpp.

Referenced by QLocale::toCurrencyString().

587 {
588  QString s;
589  ds >> s;
590  l = QLocale(s);
591  return ds;
592 }
The QString class provides a Unicode character string.
Definition: qstring.h:83

◆ parse_locale_tag()

static bool parse_locale_tag ( const QString input,
int &  i,
QString result,
const QString separators 
)
static

Definition at line 282 of file qlocale.cpp.

Referenced by qt_splitLocaleName().

283 {
284  *result = QString(8, Qt::Uninitialized); // worst case according to BCP47
285  QChar *pch = result->data();
286  const QChar *uc = input.data() + i;
287  const int l = input.length();
288  int size = 0;
289  for (; i < l && size < 8; ++i, ++size) {
290  if (separators.contains(*uc))
291  break;
292  if (! ((uc->unicode() >= 'a' && uc->unicode() <= 'z') ||
293  (uc->unicode() >= 'A' && uc->unicode() <= 'Z') ||
294  (uc->unicode() >= '0' && uc->unicode() <= '9')) ) // latin only
295  return false;
296  *pch++ = *uc++;
297  }
298  result->truncate(size);
299  return true;
300 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
QFactoryLoader * l

◆ qIsUpper()

static bool qIsUpper ( char  c)
static

Definition at line 1848 of file qlocale.cpp.

Referenced by QLocale::toString().

1849 {
1850  return c >= 'A' && c <= 'Z';
1851 }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ QSystemLocale_globalSystemLocale()

static QSystemLocale* QSystemLocale_globalSystemLocale ( )
static

Referenced by systemLocale().

◆ qt_readEscapedFormatString()

QString qt_readEscapedFormatString ( const QString format,
int *  idx 
)

Definition at line 387 of file qlocale.cpp.

Referenced by QLocalePrivate::dateTimeToString(), QLocalePrivate::digitToCLocale(), macToQtFormat(), timeFormatContainsAP(), and QSystemLocalePrivate::winToQtFormat().

388 {
389  int &i = *idx;
390 
391  Q_ASSERT(format.at(i) == QLatin1Char('\''));
392  ++i;
393  if (i == format.size())
394  return QString();
395  if (format.at(i).unicode() == '\'') { // "''" outside of a quoted stirng
396  ++i;
397  return QLatin1String("'");
398  }
399 
400  QString result;
401 
402  while (i < format.size()) {
403  if (format.at(i).unicode() == '\'') {
404  if (i + 1 < format.size() && format.at(i + 1).unicode() == '\'') {
405  // "''" inside of a quoted string
406  result.append(QLatin1Char('\''));
407  i += 2;
408  } else {
409  break;
410  }
411  } else {
412  result.append(format.at(i++));
413  }
414  }
415  if (i < format.size())
416  ++i;
417 
418  return result;
419 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
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
QString & append(QChar c)
Definition: qstring.cpp:1777
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qt_repeatCount()

int qt_repeatCount ( const QString s,
int  i 
)

Definition at line 421 of file qlocale.cpp.

Referenced by QLocalePrivate::dateTimeToString(), QLocalePrivate::digitToCLocale(), macToQtFormat(), and QSystemLocalePrivate::winToQtFormat().

422 {
423  QChar c = s.at(i);
424  int j = i + 1;
425  while (j < s.size() && s.at(j) == c)
426  ++j;
427  return j - i;
428 }
unsigned char c[8]
Definition: qnumeric_p.h:62
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102

◆ qt_splitLocaleName()

bool qt_splitLocaleName ( const QString name,
QString lang,
QString script,
QString cntry 
)

Definition at line 302 of file qlocale.cpp.

Referenced by QLocalePrivate::digitToCLocale(), QLocalePrivate::getLangAndCountry(), getMacLocaleName(), and getWinLocaleName().

303 {
304  const int length = name.length();
305 
306  lang = script = cntry = QString();
307 
308  const QString separators = QLatin1String("_-.@");
309  enum ParserState { NoState, LangState, ScriptState, CountryState };
310  ParserState state = LangState;
311  for (int i = 0; i < length && state != NoState; ) {
312  QString value;
313  if (!parse_locale_tag(name, i, &value, separators) ||value.isEmpty())
314  break;
315  QChar sep = i < length ? name.at(i) : QChar();
316  switch (state) {
317  case LangState:
318  if (!sep.isNull() && !separators.contains(sep)) {
319  state = NoState;
320  break;
321  }
322  lang = value;
323  if (i == length) {
324  // just language was specified
325  state = NoState;
326  break;
327  }
328  state = ScriptState;
329  break;
330  case ScriptState: {
331  QString scripts = QString::fromLatin1((const char *)script_code_list, sizeof(script_code_list));
332  if (value.length() == 4 && scripts.indexOf(value) % 4 == 0) {
333  // script name is always 4 characters
334  script = value;
335  state = CountryState;
336  } else {
337  // it wasn't a script, maybe it is a country then?
338  cntry = value;
339  state = NoState;
340  }
341  break;
342  }
343  case CountryState:
344  cntry = value;
345  state = NoState;
346  break;
347  case NoState:
348  // shouldn't happen
349  qWarning("QLocale: This should never happen");
350  break;
351  }
352  ++i;
353  }
354  return lang.length() == 2 || lang.length() == 3;
355 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
static const unsigned char script_code_list[]
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Q_CORE_EXPORT void qWarning(const char *,...)
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static bool parse_locale_tag(const QString &input, int &i, QString *result, const QString &separators)
Definition: qlocale.cpp:282

◆ qToLower()

static char qToLower ( char  c)
static

Definition at line 1853 of file qlocale.cpp.

Referenced by QLocale::toString().

1854 {
1855  if (c >= 'A' && c <= 'Z')
1856  return c - 'A' + 'a';
1857  else
1858  return c;
1859 }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ systemLocale()

static const QSystemLocale* systemLocale ( )
static

◆ systemPrivate()

static const QLocalePrivate* systemPrivate ( )
static

◆ timeFormatContainsAP()

static bool timeFormatContainsAP ( const QString format)
static

Definition at line 1354 of file qlocale.cpp.

Referenced by QLocalePrivate::dateTimeToString().

1355 {
1356  int i = 0;
1357  while (i < format.size()) {
1358  if (format.at(i).unicode() == '\'') {
1359  qt_readEscapedFormatString(format, &i);
1360  continue;
1361  }
1362 
1363  if (format.at(i).toLower().unicode() == 'a')
1364  return true;
1365 
1366  ++i;
1367  }
1368  return false;
1369 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QString qt_readEscapedFormatString(const QString &format, int *idx)
Definition: qlocale.cpp:387
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
QChar toLower() const
Returns the lowercase equivalent if the character is uppercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1239

◆ timeZone()

static QString timeZone ( )
static

Definition at line 1371 of file qlocale.cpp.

Referenced by QLocalePrivate::dateTimeToString().

1372 {
1373 #if defined(Q_OS_WINCE)
1374  TIME_ZONE_INFORMATION info;
1375  DWORD res = GetTimeZoneInformation(&info);
1376  if (res == TIME_ZONE_ID_UNKNOWN)
1377  return QString();
1378  return QString::fromWCharArray(info.StandardName);
1379 #elif defined(Q_OS_WIN)
1380  _tzset();
1381 # if defined(_MSC_VER) && _MSC_VER >= 1400
1382  size_t returnSize = 0;
1383  char timeZoneName[512];
1384  if (_get_tzname(&returnSize, timeZoneName, 512, 1))
1385  return QString();
1386  return QString::fromLocal8Bit(timeZoneName);
1387 # else
1388  return QString::fromLocal8Bit(_tzname[1]);
1389 # endif
1390 #elif defined(Q_OS_VXWORKS)
1391  return QString();
1392 #else
1393  tzset();
1394  return QString::fromLocal8Bit(tzname[1]);
1395 #endif
1396 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
static mach_timebase_info_data_t info
The QString class provides a Unicode character string.
Definition: qstring.h:83

Variable Documentation

◆ _systemLocale

QSystemLocale* _systemLocale = 0
static

Definition at line 83 of file qlocale.cpp.

Referenced by QSystemLocale::QSystemLocale(), and systemLocale().

◆ default_lp

const QLocalePrivate* default_lp = 0
static

Definition at line 430 of file qlocale.cpp.

Referenced by defaultPrivate().

◆ default_number_options

uint default_number_options = 0
static

Definition at line 431 of file qlocale.cpp.

Referenced by QLocale::QLocale().

◆ locale_data_size

const int locale_data_size = sizeof(locale_data)/sizeof(QLocalePrivate) - 1
static

Definition at line 596 of file qlocale.cpp.

Referenced by localePrivateIndex().

◆ system_lp

QLocalePrivate* system_lp = 0
static

Definition at line 85 of file qlocale.cpp.

Referenced by dataPointerHelper(), and systemPrivate().