2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file widget_version.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for widget version
22 #include <widget_version.h>
23 #include <dpl/assert.h>
24 #include <dpl/log/log.h>
28 namespace // anonymous
30 size_t WAC_CERTIFY_MANDATORY_PART_LOW_COUNT = 2;
31 size_t WAC_CERTIFY_MANDATORY_PART_HIGH_COUNT = 3;
32 size_t WAC_CERTIFY_MANDATORY_PART_MAJOR_INDEX = 0;
33 size_t WAC_CERTIFY_MANDATORY_PART_MINOR_INDEX = 1;
34 size_t WAC_CERTIFY_MANDATORY_PART_MICRO_INDEX = 2;
35 DPL::String::value_type WAC_CERTIFY_MANDATORY_VS_OPTIONAL_SPLIT_CHAR = L' ';
36 DPL::String::value_type WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR = L'.';
37 DPL::String::value_type LEADING_ZERO_CHAR = L'0';
41 // Augmented BNF for Syntax Specifications: ABNF. RFC5234. D. Crocker and P. Overell. January 2008.
43 // ALPHA = %x41-5A / %x61-7A
44 inline bool IsAlpha(int c)
46 return (c >= 0x41 && c <= 0x5A) ||
47 (c >= 0x61 && c <= 0x7A);
51 inline bool IsDigit(int c)
53 return c >= 0x30 && c <= 0x39;
57 inline bool IsSp(int c)
62 DPL::String RemoveLeadingZeroes(const DPL::String &str)
66 if (str[0] != LEADING_ZERO_CHAR) {
72 while (pos + 1 < str.size() && str[pos + 1] == LEADING_ZERO_CHAR) {
76 return str.substr(pos);
80 bool NumberLessOperator(const DPL::String &left,
81 const DPL::String &right)
83 // Assume: No leading zeroes
84 if (left.size() < right.size()) {
88 if (left.size() > right.size()) {
92 // Now: left.size() == right.size()
93 for (ssize_t i = static_cast<ssize_t>(left.size()) - 1; i >= 0; --i) {
94 if (left[i] < right[i]) {
98 if (left[i] > right[i]) {
107 bool WacCertifyNumberString(const DPL::String &str)
109 for (DPL::String::const_iterator i = str.begin(); i != str.end(); ++i) {
118 bool WacCertifyAlphaNumberStringSpace(const DPL::String &str)
120 for (DPL::String::const_iterator i = str.begin(); i != str.end(); ++i) {
121 if (!IsDigit(*i) && !IsAlpha(*i) && !IsSp(*i)) {
130 WidgetVersion::WidgetVersion(const DPL::String &str) :
134 LogDebug("Parsing version string: " << str);
136 // Split optional an mandatory parts
137 size_t optionalPartPosition = str.find(
138 WAC_CERTIFY_MANDATORY_VS_OPTIONAL_SPLIT_CHAR);
140 DPL::String mandatoryPart;
141 DPL::Optional<DPL::String> optionalPart;
143 if (optionalPartPosition == DPL::String::npos) {
146 mandatoryPart = str.substr(0, optionalPartPosition);
147 optionalPart = str.substr(optionalPartPosition + 1, DPL::String::npos);
150 LogDebug("Mandatory part is: " << mandatoryPart);
151 LogDebug("Optional part is: " << optionalPart);
153 // Split string and construct version
154 std::vector<DPL::String> parts;
155 DPL::Tokenize(mandatoryPart,
156 WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR,
157 std::back_inserter(parts),
160 LogDebug("Tokenized mandatory parts: " << parts.size());
162 if (parts.size() != WAC_CERTIFY_MANDATORY_PART_LOW_COUNT &&
163 parts.size() != WAC_CERTIFY_MANDATORY_PART_HIGH_COUNT) {
169 DPL::Optional<DPL::String> micro;
172 major = parts[WAC_CERTIFY_MANDATORY_PART_MAJOR_INDEX];
173 minor = parts[WAC_CERTIFY_MANDATORY_PART_MINOR_INDEX];
175 if (parts.size() == WAC_CERTIFY_MANDATORY_PART_HIGH_COUNT) {
176 micro = parts[WAC_CERTIFY_MANDATORY_PART_MICRO_INDEX];
179 WacCertify(major, minor, micro, optionalPart);
182 WidgetVersion::WidgetVersion(const DPL::String &major,
183 const DPL::String &minor,
184 const DPL::Optional<DPL::String> µ,
185 const DPL::Optional<DPL::String> &optional) :
188 // Create Raw version
190 m_raw += WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR;
192 m_raw += WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR;
199 m_raw += WAC_CERTIFY_MANDATORY_VS_OPTIONAL_SPLIT_CHAR;
204 WacCertify(major, minor, micro, optional);
207 void WidgetVersion::WacCertify(const DPL::String &major,
208 const DPL::String &minor,
209 const DPL::Optional<DPL::String> µ,
210 const DPL::Optional<DPL::String> &optional)
212 LogDebug("Certyfing...");
214 LogDebug("Major candidate: " << major);
215 LogDebug("Minor candidate: " << minor);
216 LogDebug("Micro candidate: " << micro);
217 LogDebug("Optional candidate: " << optional);
220 if (major.empty() || !WacCertifyNumberString(major)) {
221 LogDebug("Major version not certified!");
225 if (minor.empty() || !WacCertifyNumberString(minor)) {
226 LogDebug("Minor version not certified!");
230 if (!!micro && (micro->empty() || !WacCertifyNumberString(*micro))) {
231 LogDebug("Microversion not certified!");
236 (optional->empty() || !WacCertifyAlphaNumberStringSpace(*optional))) {
237 LogDebug("Optional version not certified!");
245 m_optional = optional;
249 LogDebug("Certified.");
252 bool WidgetVersion::IsWac() const
257 const DPL::String &WidgetVersion::Raw() const
262 const DPL::String &WidgetVersion::Major() const
267 const DPL::String &WidgetVersion::Minor() const
272 const DPL::Optional<DPL::String> &WidgetVersion::Micro() const
277 const DPL::Optional<DPL::String> &WidgetVersion::Optional() const
282 bool operator<(const WidgetVersion &left,
283 const WidgetVersion &right)
286 left.IsWac() && right.IsWac() &&
287 "Only WAC version strings are comparable!");
289 if (NumberLessOperator(RemoveLeadingZeroes(left.Major()),
290 RemoveLeadingZeroes(right.Major()))) { return true; }
291 if (NumberLessOperator(RemoveLeadingZeroes(right.Major()),
292 RemoveLeadingZeroes(left.Major()))) { return false; }
294 if (NumberLessOperator(RemoveLeadingZeroes(left.Minor()),
295 RemoveLeadingZeroes(right.Minor()))) { return true; }
296 if (NumberLessOperator(RemoveLeadingZeroes(right.Minor()),
297 RemoveLeadingZeroes(left.Minor()))) { return false; }
299 if (!!left.Micro() && !!right.Micro() &&
300 NumberLessOperator(RemoveLeadingZeroes(*left.Micro()),
301 RemoveLeadingZeroes(*right.Micro()))) { return true; }
302 if (!left.Micro() && !!right.Micro()) { return true; }
307 bool operator<=(const WidgetVersion &left,
308 const WidgetVersion &right)
311 left.IsWac() && right.IsWac() &&
312 "Only WAC version strings are comparable!");
314 return (left == right) || (left < right);
317 bool operator>(const WidgetVersion &left,
318 const WidgetVersion &right)
321 left.IsWac() && right.IsWac() &&
322 "Only WAC version strings are comparable!");
324 return !(left <= right);
327 bool operator>=(const WidgetVersion &left,
328 const WidgetVersion &right)
331 left.IsWac() && right.IsWac() &&
332 "Only WAC version strings are comparable!");
334 return (left == right) || (left > right);
337 bool operator==(const WidgetVersion &left,
338 const WidgetVersion &right)
341 left.IsWac() && right.IsWac() &&
342 "Only WAC version strings are comparable!");
344 return RemoveLeadingZeroes(left.Major()) ==
345 RemoveLeadingZeroes(right.Major()) && // Major are equal
346 RemoveLeadingZeroes(left.Minor()) ==
347 RemoveLeadingZeroes(right.Minor()) && // and Minor are equal
349 (!!left.Micro() && !!right.Micro() &&
350 RemoveLeadingZeroes(*left.Micro()) ==
351 RemoveLeadingZeroes(*right.Micro())) || // Both Micro exist and are equal
352 (!left.Micro() && !right.Micro()) // or both Micro do not exist
356 bool operator!=(const WidgetVersion &left,
357 const WidgetVersion &right)
360 left.IsWac() && right.IsWac() &&
361 "Only WAC version strings are comparable!");
363 return !(left == right);
366 std::ostream & operator<<(std::ostream& stream,
367 const WidgetVersion& version)
369 stream << version.Raw();