1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <QStringList> |
41 | |
42 | #include "qbuiltintypes_p.h" |
43 | #include "qpatternistlocale_p.h" |
44 | #include "qvalidationerror_p.h" |
45 | |
46 | #include "qabstractduration_p.h" |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | using namespace QPatternist; |
51 | |
52 | AbstractDuration::AbstractDuration(const bool isPos) : m_isPositive(isPos) |
53 | { |
54 | } |
55 | |
56 | #define error(msg) return ValidationError::createError(msg); |
57 | #define getCapt(sym) ((captTable.sym == -1) ? QString() : capts.at(captTable.sym)) |
58 | |
59 | AtomicValue::Ptr AbstractDuration::create(const CaptureTable &captTable, |
60 | const QString &lexical, |
61 | bool *isPositive, |
62 | YearProperty *years, |
63 | MonthProperty *months, |
64 | DayCountProperty *days, |
65 | HourProperty *hours, |
66 | MinuteProperty *minutes, |
67 | SecondProperty *seconds, |
68 | MSecondProperty *mseconds) |
69 | { |
70 | /* We don't directly write into the arguments(eg @p years) but uses these |
71 | * because the arguments are intended for normalized values, and therefore |
72 | * can cause overflows. */ |
73 | MonthCountProperty monthCount = 0; |
74 | MinuteCountProperty minCount = 0; |
75 | HourCountProperty hourCount = 0; |
76 | SecondCountProperty secCount = 0; |
77 | |
78 | Q_ASSERT(isPositive); |
79 | QRegExp myExp(captTable.regExp); /* Copy, in order to stay thread safe. */ |
80 | |
81 | if(!myExp.exactMatch(str: lexical)) |
82 | { |
83 | error(QString()); |
84 | } |
85 | |
86 | const QStringList capts(myExp.capturedTexts()); |
87 | |
88 | |
89 | if(days) |
90 | { |
91 | if(getCapt(tDelimiter).isEmpty()) |
92 | { |
93 | if((years && getCapt(year).isEmpty() && getCapt(month).isEmpty() && getCapt(day).isEmpty()) |
94 | || |
95 | (!years && getCapt(day).isEmpty())) |
96 | { |
97 | error(QtXmlPatterns::tr("At least one component must be present." )); |
98 | } |
99 | } |
100 | else if(getCapt(hour).isEmpty() && |
101 | getCapt(minutes).isEmpty() && |
102 | getCapt(seconds).isEmpty() && |
103 | getCapt(mseconds).isEmpty()) |
104 | { |
105 | error(QtXmlPatterns::tr("At least one time component must appear " |
106 | "after the %1-delimiter." ) |
107 | .arg(formatKeyword("T" ))); |
108 | } |
109 | } |
110 | else if(getCapt(year).isEmpty() && getCapt(month).isEmpty()) /* This checks xs:yearMonthDuration. */ |
111 | { |
112 | error(QtXmlPatterns::tr("At least one component must be present." )); |
113 | } |
114 | |
115 | /* If we got no '-', we are positive. */ |
116 | *isPositive = capts.at(i: 1).isEmpty(); |
117 | |
118 | if(days) |
119 | { |
120 | Q_ASSERT(hours); |
121 | Q_ASSERT(minutes); |
122 | Q_ASSERT(seconds); |
123 | Q_ASSERT(mseconds); |
124 | |
125 | *days = getCapt(day).toInt(); |
126 | hourCount = getCapt(hour).toInt(); |
127 | minCount = getCapt(minutes).toInt(); |
128 | secCount = getCapt(seconds).toInt(); |
129 | |
130 | const QString msecondsStr(getCapt(mseconds)); |
131 | if(!msecondsStr.isEmpty()) |
132 | *mseconds = msecondsStr.leftJustified(width: 3, fill: QLatin1Char('0')).toInt(); |
133 | else |
134 | *mseconds = msecondsStr.toInt(); |
135 | |
136 | if(secCount > 59) |
137 | { |
138 | minCount += secCount / 60; |
139 | *seconds = secCount % 60; |
140 | } |
141 | else |
142 | *seconds = secCount; |
143 | |
144 | if(minCount > 59) |
145 | { |
146 | hourCount += minCount / 60; |
147 | *minutes = minCount % 60; |
148 | } |
149 | else |
150 | *minutes = minCount; |
151 | |
152 | if(hourCount > 23) |
153 | { |
154 | *days += hourCount / 24; |
155 | *hours = hourCount % 24; |
156 | } |
157 | else |
158 | *hours = hourCount; |
159 | } |
160 | |
161 | if(!years) |
162 | return AtomicValue::Ptr(); |
163 | |
164 | /* We're supposed to handle years/months. */ |
165 | Q_ASSERT(months); |
166 | |
167 | *years = getCapt(year).toInt(); |
168 | monthCount = getCapt(month).toInt(); |
169 | |
170 | if(monthCount > 11) |
171 | { |
172 | *years += monthCount / 12; |
173 | *months = monthCount % 12; |
174 | } |
175 | else |
176 | *months = monthCount; |
177 | |
178 | return AtomicValue::Ptr(); |
179 | } |
180 | #undef error |
181 | #undef getCapt |
182 | |
183 | bool AbstractDuration::operator==(const AbstractDuration &other) const |
184 | { |
185 | if(years() == other.years() |
186 | && months() == other.months() |
187 | && days() == other.days() |
188 | && hours() == other.hours() |
189 | && minutes() == other.minutes() |
190 | && seconds() == other.seconds() |
191 | && mseconds() == other.mseconds()) |
192 | { |
193 | if(isPositive() == other.isPositive()) |
194 | return true; |
195 | else if(years() == 0 |
196 | && months() == 0 |
197 | && days() == 0 |
198 | && hours() == 0 |
199 | && minutes() == 0 |
200 | && seconds () == 0 |
201 | && mseconds() == 0) |
202 | { |
203 | return true; /* Signedness doesn't matter if all are zero. */ |
204 | } |
205 | } |
206 | |
207 | return false; |
208 | } |
209 | |
210 | QString AbstractDuration::serializeMSeconds(const MSecondProperty mseconds) |
211 | { |
212 | QString retval; |
213 | retval.append(c: QLatin1Char('.')); |
214 | int div = 100; |
215 | MSecondProperty msecs = mseconds; |
216 | |
217 | while(msecs > 0) |
218 | { |
219 | int d = msecs / div; |
220 | retval.append(c: QLatin1Char(d + '0')); |
221 | msecs = msecs % div; |
222 | div = div / 10; |
223 | } |
224 | |
225 | return retval; |
226 | } |
227 | |
228 | bool AbstractDuration::isPositive() const |
229 | { |
230 | return m_isPositive; |
231 | } |
232 | |
233 | QT_END_NAMESPACE |
234 | |