xsversion.c
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 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 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #include "xsversion.h"
66 #include "xsstring.h"
67 #include <stdio.h>
68 
78 int XsVersion_empty(const XsVersion* thisPtr)
79 {
80  return thisPtr->m_major == 0 && thisPtr->m_minor == 0 && thisPtr->m_revision == 0;
81 }
82 
84 void XsVersion_toString(const XsVersion* thisPtr, XsString* version)
85 {
86  char buffer[256];
87  int chars;
88 
89  if (thisPtr->m_build != 0 && thisPtr->m_reposVersion != 0)
90  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);
91  else
92  chars = sprintf(buffer, "%d.%d.%d", thisPtr->m_major, thisPtr->m_minor, thisPtr->m_revision);
93 
94  XsString_assign(version, (XsSize)(ptrdiff_t)chars, buffer);
95  if (thisPtr->m_extra.m_size != 0)
96  {
97  const char space = ' ';
98  XsArray_insert(version, version->m_size - 1, 1, &space);
99  XsString_append(version, &thisPtr->m_extra);
100  }
101 }
102 
107 void XsVersion_toSimpleString(const XsVersion* thisPtr, XsString* version)
108 {
109  char buffer[256];
110  int chars = sprintf(buffer, "%d.%d.%d", thisPtr->m_major, thisPtr->m_minor, thisPtr->m_revision);
111  XsString_assign(version, (XsSize)(ptrdiff_t)chars, buffer);
112 }
113 
118 void XsVersion_fromString(XsVersion* thisPtr, const XsString* version)
119 {
120  int major = 0;
121  int minor = 0;
122  int revision = 0;
123  int build = 0;
124  int reposVersion = 0;
125  int result;
126  size_t count = 0;
127 
128  assert(thisPtr);
129  thisPtr->m_major = 0;
130  thisPtr->m_minor = 0;
131  thisPtr->m_revision = 0;
132  thisPtr->m_build = 0;
133  thisPtr->m_reposVersion = 0;
134  XsString_resize(&thisPtr->m_extra, 0);
135  if (!version || XsString_empty(version))
136  return;
137 
138  result = sscanf(version->m_data, "%d.%d.%d build %d rev %d%zn", &major, &minor, &revision, &build, &reposVersion, &count);
139 
140  if (result > 0)
141  {
142  thisPtr->m_major = major;
143  thisPtr->m_minor = minor;
144  thisPtr->m_revision = revision;
145  }
146 
147  if (result > 3)
148  {
149  thisPtr->m_build = build;
150  thisPtr->m_reposVersion = reposVersion;
151  }
152 
153  if ((result == 5) && ((count + 1) < version->m_size))
154  XsString_assignCharArray(&thisPtr->m_extra, &version->m_data[count + 1]);
155 }
156 
160 void XsVersion_fromSimpleVersion(XsVersion* thisPtr, const XsSimpleVersion* simpleVersion)
161 {
162  thisPtr->m_major = simpleVersion->m_major;
163  thisPtr->m_minor = simpleVersion->m_minor;
164  thisPtr->m_revision = simpleVersion->m_revision;
165  thisPtr->m_build = 0;
166  thisPtr->m_reposVersion = 0;
167  XsString_resize(&thisPtr->m_extra, 0);
168 }
169 
173 void XsVersion_toSimpleVersion(const XsVersion* thisPtr, XsSimpleVersion* simpleVersion)
174 {
175  simpleVersion->m_major = (uint8_t)(int8_t)thisPtr->m_major;
176  simpleVersion->m_minor = (uint8_t)(int8_t)thisPtr->m_minor;
177  simpleVersion->m_revision = (uint8_t)(int8_t)thisPtr->m_revision;
178 }
179 
XsVersion::m_revision
int m_revision
The revision number of the version.
Definition: xsversion.h:317
xsstring.h
XsVersion::m_extra
XsString m_extra
Storage for some extra information about the version.
Definition: xsversion.h:320
XsVersion::XsVersion_toString
void XsVersion_toString(const XsVersion *thisPtr, XsString *version)
Get a string with the version expressed in a readable format.
Definition: xsversion.c:84
XsVersion::m_build
int m_build
The build number for this build.
Definition: xsversion.h:318
XsString_empty
int XsString_empty(XsString const *thisPtr)
Returns true when the supplied string is empty.
Definition: xsstring.c:493
XsVersion::XsVersion_toSimpleVersion
void XsVersion_toSimpleVersion(const XsVersion *thisPtr, XsSimpleVersion *simpleVersion)
Create a XsSimpleVersion (version) from a XsVersion.
Definition: xsversion.c:173
XsSimpleVersion::m_major
uint8_t m_major
The major part of the version number.
Definition: xssimpleversion.h:219
XsString_append
void XsString_append(XsString *thisPtr, XsString const *other)
This function concatenates the other to this.
Definition: xsstring.c:257
XsSimpleVersion
A class to store version information.
Definition: xssimpleversion.h:91
XsSimpleVersion::m_minor
uint8_t m_minor
The minor part of the version number.
Definition: xssimpleversion.h:220
XsVersion::XsVersion_empty
int XsVersion_empty(const XsVersion *thisPtr)
Test if this is a null-version.
Definition: xsversion.c:78
XsVersion::XsVersion_toSimpleString
void XsVersion_toSimpleString(const XsVersion *thisPtr, XsString *version)
Get a string with the version expressed in a readable format.
Definition: xsversion.c:107
XsSimpleVersion::m_revision
uint8_t m_revision
The revision number of the version.
Definition: xssimpleversion.h:221
XsVersion::XsVersion_fromSimpleVersion
void XsVersion_fromSimpleVersion(XsVersion *thisPtr, const XsSimpleVersion *simpleVersion)
Create a XsVersion a XsSimpleVersion, simpleVersion.
Definition: xsversion.c:160
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
XsVersion::m_minor
int m_minor
The minor part of the version number.
Definition: xsversion.h:316
XsVersion
A class to store version information.
Definition: xsversion.h:95
xsversion.h
XsVersion::XsVersion_fromString
void XsVersion_fromString(XsVersion *thisPtr, const XsString *version)
Set the version to the values in the string.
Definition: xsversion.c:118
XsArray::XsArray_insert
void XsArray_insert(void *thisPtr, XsSize index, XsSize count, void const *src)
Insert count items from src at index in the array.
Definition: xsarray.c:372
XsString_resize
void XsString_resize(XsString *thisPtr, XsSize count)
This function resizes the contained string to the desired size, while retaining its contents.
Definition: xsstring.c:243
XsString_assignCharArray
void XsString_assignCharArray(XsString *thisPtr, const char *src)
This function determines the size of src and copies the contents to the object.
Definition: xsstring.c:172
XsVersion::m_major
int m_major
The major part of the version number.
Definition: xsversion.h:315
XsString_assign
void XsString_assign(XsString *thisPtr, XsSize count, const char *src)
Reinitializes the XsArray with space for count items and copies them from src.
Definition: xsstring.c:142
XsString
A 0-terminated managed string of characters.
XsVersion::m_reposVersion
int m_reposVersion
The source revision used for this build.
Definition: xsversion.h:319


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20