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
23 #include <dpl/utils/widget_version.h>
24 #include <dpl/assert.h>
25 #include <dpl/log/log.h>
29 namespace // anonymous
31 size_t WAC_CERTIFY_MANDATORY_PART_LOW_COUNT = 2;
32 size_t WAC_CERTIFY_MANDATORY_PART_HIGH_COUNT = 3;
33 size_t WAC_CERTIFY_MANDATORY_PART_MAJOR_INDEX = 0;
34 size_t WAC_CERTIFY_MANDATORY_PART_MINOR_INDEX = 1;
35 size_t WAC_CERTIFY_MANDATORY_PART_MICRO_INDEX = 2;
36 DPL::String::value_type WAC_CERTIFY_MANDATORY_VS_OPTIONAL_SPLIT_CHAR = L' ';
37 DPL::String::value_type WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR = L'.';
38 DPL::String::value_type LEADING_ZERO_CHAR = L'0';
42 // Augmented BNF for Syntax Specifications: ABNF. RFC5234. D. Crocker and P.
43 // Overell. January 2008.
45 // ALPHA = %x41-5A / %x61-7A
46 inline bool IsAlpha(int c)
48 return (c >= 0x41 && c <= 0x5A) ||
49 (c >= 0x61 && c <= 0x7A);
53 inline bool IsDigit(int c)
55 return c >= 0x30 && c <= 0x39;
59 inline bool IsSp(int c)
64 DPL::String RemoveLeadingZeroes(const DPL::String &str)
68 if (str[0] != LEADING_ZERO_CHAR) {
74 while (pos + 1 < str.size() && str[pos + 1] == LEADING_ZERO_CHAR) {
78 return str.substr(pos);
82 bool NumberLessOperator(const DPL::String &left,
83 const DPL::String &right)
85 // Assume: No leading zeroes
86 if (left.size() < right.size()) {
90 if (left.size() > right.size()) {
94 // Now: left.size() == right.size()
95 for (ssize_t i = static_cast<ssize_t>(left.size()) - 1; i >= 0; --i) {
96 if (left[i] < right[i]) {
100 if (left[i] > right[i]) {
109 bool WacCertifyNumberString(const DPL::String &str)
111 for (DPL::String::const_iterator i = str.begin(); i != str.end(); ++i) {
120 bool WacCertifyAlphaNumberStringSpace(const DPL::String &str)
122 for (DPL::String::const_iterator i = str.begin(); i != str.end(); ++i) {
123 if (!IsDigit(*i) && !IsAlpha(*i) && !IsSp(*i)) {
132 WidgetVersion::WidgetVersion(const DPL::String &str) :
136 LogDebug("Parsing version string: " << str);
138 // Split optional an mandatory parts
139 size_t optionalPartPosition = str.find(
140 WAC_CERTIFY_MANDATORY_VS_OPTIONAL_SPLIT_CHAR);
142 DPL::String mandatoryPart;
143 DPL::Optional<DPL::String> optionalPart;
145 if (optionalPartPosition == DPL::String::npos) {
148 mandatoryPart = str.substr(0, optionalPartPosition);
149 optionalPart = str.substr(optionalPartPosition + 1, DPL::String::npos);
152 LogDebug("Mandatory part is: " << mandatoryPart);
153 LogDebug("Optional part is: " << optionalPart);
155 // Split string and construct version
156 std::vector<DPL::String> parts;
157 DPL::Tokenize(mandatoryPart,
158 WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR,
159 std::back_inserter(parts),
162 LogDebug("Tokenized mandatory parts: " << parts.size());
164 if (parts.size() != WAC_CERTIFY_MANDATORY_PART_LOW_COUNT &&
165 parts.size() != WAC_CERTIFY_MANDATORY_PART_HIGH_COUNT)
172 DPL::Optional<DPL::String> micro;
175 major = parts[WAC_CERTIFY_MANDATORY_PART_MAJOR_INDEX];
176 minor = parts[WAC_CERTIFY_MANDATORY_PART_MINOR_INDEX];
178 if (parts.size() == WAC_CERTIFY_MANDATORY_PART_HIGH_COUNT) {
179 micro = parts[WAC_CERTIFY_MANDATORY_PART_MICRO_INDEX];
182 WacCertify(major, minor, micro, optionalPart);
185 WidgetVersion::WidgetVersion(const DPL::String &major,
186 const DPL::String &minor,
187 const DPL::Optional<DPL::String> µ,
188 const DPL::Optional<DPL::String> &optional) :
191 // Create Raw version
193 m_raw += WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR;
195 m_raw += WAC_CERTIFY_MANDATORY_NUMBER_PART_SPLIT_CHAR;
202 m_raw += WAC_CERTIFY_MANDATORY_VS_OPTIONAL_SPLIT_CHAR;
207 WacCertify(major, minor, micro, optional);
210 void WidgetVersion::WacCertify(const DPL::String &major,
211 const DPL::String &minor,
212 const DPL::Optional<DPL::String> µ,
213 const DPL::Optional<DPL::String> &optional)
215 LogDebug("Certyfing...");
217 LogDebug("Major candidate: " << major);
218 LogDebug("Minor candidate: " << minor);
219 LogDebug("Micro candidate: " << micro);
220 LogDebug("Optional candidate: " << optional);
223 if (major.empty() || !WacCertifyNumberString(major)) {
224 LogDebug("Major version not certified!");
228 if (minor.empty() || !WacCertifyNumberString(minor)) {
229 LogDebug("Minor version not certified!");
233 if (!!micro && (micro->empty() || !WacCertifyNumberString(*micro))) {
234 LogDebug("Microversion not certified!");
239 (optional->empty() || !WacCertifyAlphaNumberStringSpace(*optional)))
241 LogDebug("Optional version not certified!");
249 m_optional = optional;
253 LogDebug("Certified.");
256 bool WidgetVersion::IsWac() const
261 const DPL::String &WidgetVersion::Raw() const
266 const DPL::String &WidgetVersion::Major() const
271 const DPL::String &WidgetVersion::Minor() const
276 const DPL::Optional<DPL::String> &WidgetVersion::Micro() const
281 const DPL::Optional<DPL::String> &WidgetVersion::Optional() const
286 bool operator<(const WidgetVersion &left,
287 const WidgetVersion &right)
290 left.IsWac() && right.IsWac() &&
291 "Only WAC version strings are comparable!");
293 if (NumberLessOperator(RemoveLeadingZeroes(left.Major()),
294 RemoveLeadingZeroes(right.Major())))
298 if (NumberLessOperator(RemoveLeadingZeroes(right.Major()),
299 RemoveLeadingZeroes(left.Major())))
304 if (NumberLessOperator(RemoveLeadingZeroes(left.Minor()),
305 RemoveLeadingZeroes(right.Minor())))
309 if (NumberLessOperator(RemoveLeadingZeroes(right.Minor()),
310 RemoveLeadingZeroes(left.Minor())))
315 if (!!left.Micro() && !!right.Micro() &&
316 NumberLessOperator(RemoveLeadingZeroes(*left.Micro()),
317 RemoveLeadingZeroes(*right.Micro())))
321 if (!left.Micro() && !!right.Micro()) {
328 bool operator<=(const WidgetVersion &left,
329 const WidgetVersion &right)
332 left.IsWac() && right.IsWac() &&
333 "Only WAC version strings are comparable!");
335 return (left == right) || (left < right);
338 bool operator>(const WidgetVersion &left,
339 const WidgetVersion &right)
342 left.IsWac() && right.IsWac() &&
343 "Only WAC version strings are comparable!");
345 return !(left <= right);
348 bool operator>=(const WidgetVersion &left,
349 const WidgetVersion &right)
352 left.IsWac() && right.IsWac() &&
353 "Only WAC version strings are comparable!");
355 return (left == right) || (left > right);
358 bool operator==(const WidgetVersion &left,
359 const WidgetVersion &right)
362 left.IsWac() && right.IsWac() &&
363 "Only WAC version strings are comparable!");
369 //Both Micro exist and are equal
370 //or both Micro do not exist
371 return RemoveLeadingZeroes(left.Major()) ==
372 RemoveLeadingZeroes(right.Major()) &&
373 RemoveLeadingZeroes(left.Minor()) ==
374 RemoveLeadingZeroes(right.Minor()) &&
376 (!!left.Micro() && !!right.Micro() &&
377 RemoveLeadingZeroes(*left.Micro()) ==
378 RemoveLeadingZeroes(*right.Micro())) ||
379 (!left.Micro() && !right.Micro())
383 bool operator!=(const WidgetVersion &left,
384 const WidgetVersion &right)
387 left.IsWac() && right.IsWac() &&
388 "Only WAC version strings are comparable!");
390 return !(left == right);
393 std::ostream & operator<<(std::ostream& stream,
394 const WidgetVersion& version)
396 stream << version.Raw();