Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qlocale_p.h
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 #ifndef QLOCALE_P_H
43 #define QLOCALE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists for the convenience
50 // of internal files.  This header file may change from version to version
51 // without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtCore/qstring.h"
57 #include "QtCore/qvarlengtharray.h"
58 #include "QtCore/qvariant.h"
59
60 #include "qlocale.h"
61
62 QT_BEGIN_NAMESPACE
63
64 #ifndef QT_NO_SYSTEMLOCALE
65 class QSystemLocale
66 {
67 public:
68     QSystemLocale();
69     virtual ~QSystemLocale();
70
71     struct CurrencyToStringArgument
72     {
73         CurrencyToStringArgument() { }
74         CurrencyToStringArgument(const QVariant &v, const QString &s)
75             : value(v), symbol(s) { }
76         QVariant value;
77         QString symbol;
78     };
79
80     enum QueryType {
81         LanguageId, // uint
82         CountryId, // uint
83         DecimalPoint, // QString
84         GroupSeparator, // QString
85         ZeroDigit, // QString
86         NegativeSign, // QString
87         DateFormatLong, // QString
88         DateFormatShort, // QString
89         TimeFormatLong, // QString
90         TimeFormatShort, // QString
91         DayNameLong, // QString, in: int
92         DayNameShort, // QString, in: int
93         MonthNameLong, // QString, in: int
94         MonthNameShort, // QString, in: int
95         DateToStringLong, // QString, in: QDate
96         DateToStringShort, // QString in: QDate
97         TimeToStringLong, // QString in: QTime
98         TimeToStringShort, // QString in: QTime
99         DateTimeFormatLong, // QString
100         DateTimeFormatShort, // QString
101         DateTimeToStringLong, // QString in: QDateTime
102         DateTimeToStringShort, // QString in: QDateTime
103         MeasurementSystem, // uint
104         PositiveSign, // QString
105         AMText, // QString
106         PMText, // QString
107         FirstDayOfWeek, // Qt::DayOfWeek
108         Weekdays, // QList<Qt::DayOfWeek>
109         CurrencySymbol, // QString in: CurrencyToStringArgument
110         CurrencyToString, // QString in: qlonglong, qulonglong or double
111         UILanguages, // QStringList
112         StringToStandardQuotation, // QString in: QStringRef to quote
113         StringToAlternateQuotation, // QString in: QStringRef to quote
114         ScriptId, // uint
115         ListToSeparatedString, // QString
116         LocaleChanged, // system locale changed
117         NativeLanguageName, // QString
118         NativeCountryName // QString
119     };
120     virtual QVariant query(QueryType type, QVariant in) const;
121     virtual QLocale fallbackLocale() const;
122
123 private:
124     QSystemLocale(bool);
125     friend class QSystemLocaleSingleton;
126 };
127 #endif
128
129 struct Q_CORE_EXPORT QLocalePrivate
130 {
131 public:
132     QChar decimal() const { return QChar(m_decimal); }
133     QChar group() const { return QChar(m_group); }
134     QChar list() const { return QChar(m_list); }
135     QChar percent() const { return QChar(m_percent); }
136     QChar zero() const { return QChar(m_zero); }
137     QChar plus() const { return QChar(m_plus); }
138     QChar minus() const { return QChar(m_minus); }
139     QChar exponential() const { return QChar(m_exponential); }
140
141     quint16 languageId() const { return m_language_id; }
142     quint16 countryId() const { return m_country_id; }
143
144     QString bcp47Name() const;
145
146     QString languageCode() const; // ### QByteArray::fromRawData would be more optimal
147     QString scriptCode() const;
148     QString countryCode() const;
149
150     static QLocale::Language codeToLanguage(const QString &code);
151     static QLocale::Script codeToScript(const QString &code);
152     static QLocale::Country codeToCountry(const QString &code);
153     static void getLangAndCountry(const QString &name, QLocale::Language &lang,
154                                   QLocale::Script &script, QLocale::Country &cntry);
155     static const QLocalePrivate *findLocale(QLocale::Language language,
156                                             QLocale::Script script,
157                                             QLocale::Country country);
158
159
160     QLocale::MeasurementSystem measurementSystem() const;
161
162     enum DoubleForm {
163         DFExponent = 0,
164         DFDecimal,
165         DFSignificantDigits,
166         _DFMax = DFSignificantDigits
167     };
168
169     enum Flags {
170         NoFlags             = 0,
171         Alternate           = 0x01,
172         ZeroPadded          = 0x02,
173         LeftAdjusted        = 0x04,
174         BlankBeforePositive = 0x08,
175         AlwaysShowSign      = 0x10,
176         ThousandsGroup      = 0x20,
177         CapitalEorX         = 0x40,
178
179         ShowBase            = 0x80,
180         UppercaseBase       = 0x100,
181         ForcePoint          = Alternate
182     };
183
184     enum GroupSeparatorMode {
185         FailOnGroupSeparators,
186         ParseGroupSeparators
187     };
188
189     static QString doubleToString(const QChar zero, const QChar plus,
190                                   const QChar minus, const QChar exponent,
191                                   const QChar group, const QChar decimal,
192                                   double d, int precision,
193                                   DoubleForm form,
194                                   int width, unsigned flags);
195     static QString longLongToString(const QChar zero, const QChar group,
196                                     const QChar plus, const QChar minus,
197                                     qint64 l, int precision, int base,
198                                     int width, unsigned flags);
199     static QString unsLongLongToString(const QChar zero, const QChar group,
200                                        const QChar plus,
201                                        quint64 l, int precision,
202                                        int base, int width,
203                                        unsigned flags);
204
205     QString doubleToString(double d,
206                            int precision = -1,
207                            DoubleForm form = DFSignificantDigits,
208                            int width = -1,
209                            unsigned flags = NoFlags) const;
210     QString longLongToString(qint64 l, int precision = -1,
211                              int base = 10,
212                              int width = -1,
213                              unsigned flags = NoFlags) const;
214     QString unsLongLongToString(quint64 l, int precision = -1,
215                                 int base = 10,
216                                 int width = -1,
217                                 unsigned flags = NoFlags) const;
218     double stringToDouble(const QString &num, bool *ok, GroupSeparatorMode group_sep_mode) const;
219     qint64 stringToLongLong(const QString &num, int base, bool *ok, GroupSeparatorMode group_sep_mode) const;
220     quint64 stringToUnsLongLong(const QString &num, int base, bool *ok, GroupSeparatorMode group_sep_mode) const;
221
222
223     static double bytearrayToDouble(const char *num, bool *ok, bool *overflow = 0);
224     static qint64 bytearrayToLongLong(const char *num, int base, bool *ok, bool *overflow = 0);
225     static quint64 bytearrayToUnsLongLong(const char *num, int base, bool *ok);
226
227     typedef QVarLengthArray<char, 256> CharBuff;
228     bool numberToCLocale(const QString &num,
229                           GroupSeparatorMode group_sep_mode,
230                           CharBuff *result) const;
231     inline char digitToCLocale(QChar c) const;
232
233     static void updateSystemPrivate();
234
235     enum NumberMode { IntegerMode, DoubleStandardMode, DoubleScientificMode };
236     bool validateChars(const QString &str, NumberMode numMode, QByteArray *buff, int decDigits = -1) const;
237
238     QString dateTimeToString(const QString &format, const QDate *date, const QTime *time,
239                              const QLocale *q) const;
240
241     quint16 m_language_id, m_script_id, m_country_id;
242
243     quint16 m_decimal, m_group, m_list, m_percent,
244         m_zero, m_minus, m_plus, m_exponential;
245     quint16 m_quotation_start, m_quotation_end;
246     quint16 m_alternate_quotation_start, m_alternate_quotation_end;
247
248     quint16 m_list_pattern_part_start_idx, m_list_pattern_part_start_size;
249     quint16 m_list_pattern_part_mid_idx, m_list_pattern_part_mid_size;
250     quint16 m_list_pattern_part_end_idx, m_list_pattern_part_end_size;
251     quint16 m_list_pattern_part_two_idx, m_list_pattern_part_two_size;
252     quint16 m_short_date_format_idx, m_short_date_format_size;
253     quint16 m_long_date_format_idx, m_long_date_format_size;
254     quint16 m_short_time_format_idx, m_short_time_format_size;
255     quint16 m_long_time_format_idx, m_long_time_format_size;
256     quint16 m_standalone_short_month_names_idx, m_standalone_short_month_names_size;
257     quint16 m_standalone_long_month_names_idx, m_standalone_long_month_names_size;
258     quint16 m_standalone_narrow_month_names_idx, m_standalone_narrow_month_names_size;
259     quint16 m_short_month_names_idx, m_short_month_names_size;
260     quint16 m_long_month_names_idx, m_long_month_names_size;
261     quint16 m_narrow_month_names_idx, m_narrow_month_names_size;
262     quint16 m_standalone_short_day_names_idx, m_standalone_short_day_names_size;
263     quint16 m_standalone_long_day_names_idx, m_standalone_long_day_names_size;
264     quint16 m_standalone_narrow_day_names_idx, m_standalone_narrow_day_names_size;
265     quint16 m_short_day_names_idx, m_short_day_names_size;
266     quint16 m_long_day_names_idx, m_long_day_names_size;
267     quint16 m_narrow_day_names_idx, m_narrow_day_names_size;
268     quint16 m_am_idx, m_am_size;
269     quint16 m_pm_idx, m_pm_size;
270     char m_currency_iso_code[3];
271     quint16 m_currency_symbol_idx, m_currency_symbol_size;
272     quint16 m_currency_display_name_idx, m_currency_display_name_size;
273     quint8 m_currency_format_idx, m_currency_format_size;
274     quint8 m_currency_negative_format_idx, m_currency_negative_format_size;
275     quint16 m_language_endonym_idx, m_language_endonym_size;
276     quint16 m_country_endonym_idx, m_country_endonym_size;
277     quint16 m_currency_digits : 2;
278     quint16 m_currency_rounding : 3;
279     quint16 m_first_day_of_week : 3;
280     quint16 m_weekend_start : 3;
281     quint16 m_weekend_end : 3;
282
283 };
284
285 inline char QLocalePrivate::digitToCLocale(QChar in) const
286 {
287     const QChar _zero = zero();
288     const QChar _group = group();
289     const ushort zeroUnicode = _zero.unicode();
290     const ushort tenUnicode = zeroUnicode + 10;
291
292     if (in.unicode() >= zeroUnicode && in.unicode() < tenUnicode)
293         return '0' + in.unicode() - zeroUnicode;
294
295     if (in.unicode() >= '0' && in.unicode() <= '9')
296         return in.toLatin1();
297
298     if (in == plus())
299         return '+';
300
301     if (in == minus())
302         return '-';
303
304     if (in == decimal())
305         return '.';
306
307     if (in == group())
308         return ',';
309
310     if (in == exponential() || in == exponential().toUpper())
311         return 'e';
312
313     // In several languages group() is the char 0xA0, which looks like a space.
314     // People use a regular space instead of it and complain it doesn't work.
315     if (_group.unicode() == 0xA0 && in.unicode() == ' ')
316         return ',';
317
318     return 0;
319 }
320
321 QString qt_readEscapedFormatString(const QString &format, int *idx);
322 bool qt_splitLocaleName(const QString &name, QString &lang, QString &script, QString &cntry);
323 int qt_repeatCount(const QString &s, int i);
324
325 QT_END_NAMESPACE
326
327 Q_DECLARE_METATYPE(QStringRef)
328 Q_DECLARE_METATYPE(QList<Qt::DayOfWeek>)
329 #ifndef QT_NO_SYSTEMLOCALE
330 Q_DECLARE_METATYPE(QSystemLocale::CurrencyToStringArgument)
331 #endif
332
333 #endif // QLOCALE_P_H