| 1 |
|
| 2 |
// Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without modification, |
| 6 |
// are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions, and the following disclaimer. |
| 10 |
// |
| 11 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
// this list of conditions, and the following disclaimer in the documentation |
| 13 |
// and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 |
// may be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
// THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 |
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 |
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 |
// SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 |
// OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 |
// ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 |
// |
| 32 |
|
| 33 |
#include "xsversion.h" |
| 34 |
#include "xsstring.h" |
| 35 |
#include <stdio.h> |
| 36 |
|
| 37 |
/*! \class XsVersion |
| 38 |
\brief A class to store version information |
| 39 |
*/ |
| 40 |
|
| 41 |
/*! \addtogroup cinterface C Interface |
| 42 |
@{ |
| 43 |
*/ |
| 44 |
|
| 45 |
/*! \relates XsVersion \brief Test if this is a null-version. */ |
| 46 |
int XsVersion_empty(const XsVersion* thisPtr) |
| 47 |
{ |
| 48 |
return thisPtr->m_major == 0 && thisPtr->m_minor == 0 && thisPtr->m_revision == 0; |
| 49 |
} |
| 50 |
|
| 51 |
/*! \relates XsVersion \brief Get a string with the version expressed in a readable format. */ |
| 52 |
void XsVersion_toString(const XsVersion* thisPtr, XsString* version) |
| 53 |
{ |
| 54 |
char buffer[256]; |
| 55 |
int chars; |
| 56 |
|
| 57 |
if (thisPtr->m_build != 0 && thisPtr->m_reposVersion != 0) |
| 58 |
chars = sprintf(buffer, "%d.%d.%d build %d rev %d", thisPtr->m_major, thisPtr->m_minor, thisPtr->m_revision, thisPtr->m_build, thisPtr->m_reposVersion); |
| 59 |
else |
| 60 |
chars = sprintf(buffer, "%d.%d.%d", thisPtr->m_major, thisPtr->m_minor, thisPtr->m_revision); |
| 61 |
|
| 62 |
XsString_assign(version, (XsSize)(ptrdiff_t)chars, buffer); |
| 63 |
if (thisPtr->m_extra.m_size != 0) |
| 64 |
{ |
| 65 |
const char space = ' '; |
| 66 |
XsArray_insert(version, version->m_size-1, 1, &space); |
| 67 |
XsString_append(version, &thisPtr->m_extra); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/*! |
| 72 |
* \relates XsVersion |
| 73 |
* \brief Get a string with the version expressed in a readable format. |
| 74 |
*/ |
| 75 |
void XsVersion_toSimpleString(const XsVersion* thisPtr, XsString* version) |
| 76 |
{ |
| 77 |
char buffer[256]; |
| 78 |
int chars = sprintf(buffer, "%d.%d.%d", thisPtr->m_major, thisPtr->m_minor, thisPtr->m_revision); |
| 79 |
XsString_assign(version, (XsSize)(ptrdiff_t)chars, buffer); |
| 80 |
} |
| 81 |
|
| 82 |
/*! |
| 83 |
* \relates XsVersion |
| 84 |
* \brief Set the version to the values in the string |
| 85 |
*/ |
| 86 |
void XsVersion_fromString(XsVersion* thisPtr, const XsString* version) |
| 87 |
{ |
| 88 |
int major = 0; |
| 89 |
int minor = 0; |
| 90 |
int revision = 0; |
| 91 |
int build = 0; |
| 92 |
int reposVersion = 0; |
| 93 |
int result; |
| 94 |
size_t count = 0; |
| 95 |
|
| 96 |
assert(thisPtr); |
| 97 |
thisPtr->m_major = 0; |
| 98 |
thisPtr->m_minor = 0; |
| 99 |
thisPtr->m_revision = 0; |
| 100 |
thisPtr->m_build = 0; |
| 101 |
thisPtr->m_reposVersion = 0; |
| 102 |
XsString_resize(&thisPtr->m_extra, 0); |
| 103 |
if (!version || XsString_empty(version)) |
| 104 |
return; |
| 105 |
|
| 106 |
result = sscanf(version->m_data, "%d.%d.%d build %d rev %d%zn", &major, &minor, &revision, &build, &reposVersion, &count); |
| 107 |
|
| 108 |
if (result > 0) |
| 109 |
{ |
| 110 |
thisPtr->m_major = major; |
| 111 |
thisPtr->m_minor = minor; |
| 112 |
thisPtr->m_revision = revision; |
| 113 |
} |
| 114 |
|
| 115 |
if (result > 3) |
| 116 |
{ |
| 117 |
thisPtr->m_build = build; |
| 118 |
thisPtr->m_reposVersion = reposVersion; |
| 119 |
} |
| 120 |
|
| 121 |
if ((result == 5) && ((count + 1) < version->m_size)) |
| 122 |
XsString_assignCharArray(&thisPtr->m_extra, &version->m_data[count + 1]); |
| 123 |
} |
| 124 |
|
| 125 |
/*! \relates XsVersion |
| 126 |
* \brief Create a XsVersion a XsSimpleVersion, \a simpleVersion. |
| 127 |
*/ |
| 128 |
void XsVersion_fromSimpleVersion(XsVersion* thisPtr, const XsSimpleVersion* simpleVersion) |
| 129 |
{ |
| 130 |
thisPtr->m_major = simpleVersion->m_major; |
| 131 |
thisPtr->m_minor = simpleVersion->m_minor; |
| 132 |
thisPtr->m_revision = simpleVersion->m_revision; |
| 133 |
thisPtr->m_build = 0; |
| 134 |
thisPtr->m_reposVersion = 0; |
| 135 |
XsString_resize(&thisPtr->m_extra, 0); |
| 136 |
} |
| 137 |
|
| 138 |
/*! \relates XsVersion |
| 139 |
* \brief Create a XsSimpleVersion (\a version) from a XsVersion. |
| 140 |
*/ |
| 141 |
void XsVersion_toSimpleVersion(const XsVersion* thisPtr, XsSimpleVersion* simpleVersion) |
| 142 |
{ |
| 143 |
simpleVersion->m_major = (uint8_t)(int8_t)thisPtr->m_major; |
| 144 |
simpleVersion->m_minor = (uint8_t)(int8_t)thisPtr->m_minor; |
| 145 |
simpleVersion->m_revision = (uint8_t)(int8_t)thisPtr->m_revision; |
| 146 |
} |
| 147 |
|
| 148 |
/*! @} */ |