sbgVersion.c
Go to the documentation of this file.
1 #include "sbgVersion.h"
2 #include <sbgCommon.h>
3 
4 //----------------------------------------------------------------------//
5 //- Version encoding / decoding methods -//
6 //----------------------------------------------------------------------//
7 
13 void sbgVersionDecode(uint32 encodedVersion, SbgVersion *pVersionInfo)
14 {
15  //
16  // Check input parameters
17  //
18  SBG_ASSERT(pVersionInfo);
19 
20  //
21  // Test if we have a software version scheme
22  //
23  if (encodedVersion&SBG_VERSION_SOFT_SCHEME)
24  {
25  //
26  // We have a software scheme, decode it
27  //
28  pVersionInfo->softwareScheme = true;
29 
30  //
31  // Decode the software scheme fields
32  //
37 
38  //
39  // Set the revision to zero as it's not used
40  //
41  pVersionInfo->rev = 0;
42  }
43  else
44  {
45  //
46  // We have a basic scheme, decode it
47  //
48  pVersionInfo->softwareScheme = false;
49 
50  //
51  // Decode the software scheme fields
52  //
53  pVersionInfo->major = (encodedVersion >> 24) & 0xFF;
54  pVersionInfo->minor = (encodedVersion >> 16) & 0xFF;
55  pVersionInfo->rev = (encodedVersion >> 8) & 0xFF;
56  pVersionInfo->build = (encodedVersion >> 0) & 0xFF;
57 
58  //
59  // Set the qualifier to zero
60  //
61  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_DEV;
62  }
63 }
64 
70 uint32 sbgVersionEncode(const SbgVersion *pVersionInfo)
71 {
72  uint32 encodedVersion = 0;
73 
74  //
75  // Check input parameter
76  //
77  SBG_ASSERT(pVersionInfo);
78 
79  //
80  // Test if we have a software scheme or a basic one
81  //
82  if (pVersionInfo->softwareScheme)
83  {
84  //
85  // We have a software, scheme, so test that the version is valid
86  //
87  SBG_ASSERT((pVersionInfo->major <= 63) && (pVersionInfo->minor <= 63) && (pVersionInfo->rev == 0));
88 
89  //
90  // Indicate that we have a software version scheme
91  //
92  encodedVersion = SBG_VERSION_SOFTWARE(pVersionInfo->major, pVersionInfo->minor, pVersionInfo->build, pVersionInfo->qualifier);
93  }
94  else
95  {
96  //
97  // We have a basic version scheme so check parameter validty
98  //
99  SBG_ASSERT(pVersionInfo->major <= 127);
100 
101  //
102  // Encode the basic version information
103  //
104  encodedVersion = SBG_VERSION_BASIC(pVersionInfo->major, pVersionInfo->minor, pVersionInfo->rev, pVersionInfo->build);
105  }
106 
107  return encodedVersion;
108 }
109 
110 //----------------------------------------------------------------------//
111 //- Version comparaison methods -//
112 //----------------------------------------------------------------------//
113 
124 int32 sbgVersionCompare(const SbgVersion *pVersionA, const SbgVersion *pVersionB, SbgVersionCmpThresold thresold)
125 {
126  int32 result;
127 
128  //
129  // Check input parameter
130  //
131  SBG_ASSERT((pVersionA) && (pVersionB));
132 
133  //
134  // Check that the two versions are using the same scheme
135  //
136  SBG_ASSERT(pVersionA->softwareScheme == pVersionB->softwareScheme);
137 
138  //
139  // Do the comparaison according to the selected thresold
140  // Start by compairing the major field
141  //
142  result = pVersionA->major - pVersionB->major;
143 
144  //
145  // Test if we have to also compare the minor field
146  //
147  if ( (result == 0) && ((thresold == SBG_VERSION_CMP_THRESOLD_MINOR) || (thresold == SBG_VERSION_CMP_THRESOLD_REVISION) || (thresold == SBG_VERSION_CMP_THRESOLD_BUILD) || (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER)) )
148  {
149  //
150  // Update the result using the minor indication
151  //
152  result = pVersionA->minor - pVersionB->minor;
153 
154  //
155  // Test if we have to also compare the revision field (for basic version only)
156  //
157  if ( (result == 0) && ((thresold == SBG_VERSION_CMP_THRESOLD_REVISION) || (thresold == SBG_VERSION_CMP_THRESOLD_BUILD) || (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER)) )
158  {
159  //
160  // Test if we have a software scheme or a basic scheme version
161  //
162  if (pVersionA->softwareScheme)
163  {
164  //
165  // We have a software scheme so set the result to 0
166  //
167  result = 0;
168  }
169  else
170  {
171  //
172  // We have a basic scheme so we can compare the revision field
173  //
174  result = pVersionA->rev - pVersionB->rev;
175  }
176 
177  //
178  // Test if we have to also compare the build field
179  //
180  if ( (result == 0) && ((thresold == SBG_VERSION_CMP_THRESOLD_BUILD) || (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER)) )
181  {
182  //
183  // Compare the build field
184  //
185  result = pVersionA->build - pVersionB->build;
186 
187  //
188  // Test if we have to also compare the qualifier field
189  //
190  if ( (result == 0) && (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER) )
191  {
192  //
193  // Test if we have a software scheme
194  //
195  if (pVersionA->softwareScheme)
196  {
197  //
198  // We have a software scheme so set the result to 0
199  //
200  result = pVersionA->qualifier - pVersionB->qualifier;
201  }
202  else
203  {
204  //
205  // We have a basic scheme so set the result to 0
206  //
207  result = 0;
208  }
209  }
210  }
211  }
212  }
213 
214  return result;
215 }
216 
217 //----------------------------------------------------------------------//
218 //- Version to string methods -//
219 //----------------------------------------------------------------------//
220 
229 SbgErrorCode sbgVersionToString(const SbgVersion *pVersionInfo, char *pBuffer, uint32 sizeOfBuffer)
230 {
231  SbgErrorCode errorCode = SBG_NO_ERROR;
232 
233  //
234  // Check input parameters
235  //
236  SBG_ASSERT((pVersionInfo) && (pBuffer) && (sizeOfBuffer>0));
237 
238  //
239  // Test if we have a basic or software version scheme
240  //
241  if (pVersionInfo->softwareScheme)
242  {
243  //
244  // We have a software version scheme
245  // Test that the buffer is big enough to store the generated string (31.31.65535-hotfix)
246  //
247  if (sizeOfBuffer >= 19)
248  {
249  //
250  // Generate the string version
251  //
252  sprintf(pBuffer, "%u.%u.%u-", pVersionInfo->major, pVersionInfo->minor, pVersionInfo->build);
253 
254  //
255  // Append the qualifier
256  //
257  switch (pVersionInfo->qualifier)
258  {
260  strcat(pBuffer, "dev");
261  break;
263  strcat(pBuffer, "alpha");
264  break;
266  strcat(pBuffer, "beta");
267  break;
269  strcat(pBuffer, "rc");
270  break;
272  strcat(pBuffer, "stable");
273  break;
275  strcat(pBuffer, "hotfix");
276  break;
277  default:
278  break;
279  }
280  }
281  else
282  {
283  //
284  // Output buffer is to small
285  //
286  errorCode = SBG_BUFFER_OVERFLOW;
287  }
288 
289  }
290  else
291  {
292  //
293  // We have a basic version scheme, generate the string
294  // Test that the buffer is big enough to store the generated string
295  //
296  if (sizeOfBuffer >= 16)
297  {
298  //
299  // Generate the string version
300  //
301  sprintf(pBuffer, "%u.%u.%u.%u", pVersionInfo->major, pVersionInfo->minor, pVersionInfo->rev, pVersionInfo->build);
302  }
303  else
304  {
305  //
306  // Output buffer is to small
307  //
308  errorCode = SBG_BUFFER_OVERFLOW;
309  }
310  }
311 
312  //
313  // Return status of operation
314  //
315  return errorCode;
316 }
317 
326 SbgErrorCode sbgVersionToStringEncoded(uint32 version, char *pBuffer, uint32 sizeOfBuffer)
327 {
328  SbgVersion versionInfo;
329 
330  //
331  // Decode the versions
332  //
333  sbgVersionDecode(version, &versionInfo);
334 
335  //
336  // Return the version as a string
337  //
338  return sbgVersionToString(&versionInfo, pBuffer, sizeOfBuffer);
339 }
340 
341 //----------------------------------------------------------------------//
342 //- String to version methods -//
343 //----------------------------------------------------------------------//
344 
351 SbgErrorCode sbgVersionFromString(const char *pVersionStr, SbgVersion *pVersionInfo)
352 {
353  SbgErrorCode errorCode = SBG_NO_ERROR;
354  char qualifierStr[32];
355  uint32 major;
356  uint32 minor;
357  uint32 rev;
358  uint32 build;
359 
360  //
361  // Check input parameters
362  //
363  SBG_ASSERT((pVersionStr) && (pVersionInfo));
364 
365  //
366  // Zero init the returned version struct
367  //
368  memset(pVersionInfo, 0x00, sizeof(SbgVersion));
369 
370  //
371  // Try to parse a basic version
372  //
373  if (sscanf(pVersionStr, "%u.%u.%u.%u", &major, &minor, &rev, &build) == 4)
374  {
375  //
376  // We have read successfully a basic version
377  //
378  pVersionInfo->softwareScheme = false;
379 
380  //
381  // Fill the version numbers
382  //
383  pVersionInfo->major = (uint8)major;
384  pVersionInfo->minor = (uint8)minor;
385  pVersionInfo->rev = (uint8)rev;
386  pVersionInfo->build = (uint8)build;
387  }
388  else if (sscanf(pVersionStr, "%u.%u.%u-%s", &major, &minor, &build, qualifierStr) == 4)
389  {
390  //
391  // We have read successfully a software scheme version
392  //
393  pVersionInfo->softwareScheme = true;
394 
395  //
396  // Fill the version numbers
397  //
398  pVersionInfo->major = (uint8)major;
399  pVersionInfo->minor = (uint8)minor;
400  pVersionInfo->build = (uint16)build;
401 
402  //
403  // Also convert the qualifier
404  //
405  if (!strcmp(qualifierStr, "dev"))
406  {
407  //
408  // Dev qualifier
409  //
410  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_DEV;
411  }
412  else if (!strcmp(qualifierStr, "alpha"))
413  {
414  //
415  // Alpha qualifier
416  //
417  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_ALPHA;
418  }
419  else if (!strcmp(qualifierStr, "beta"))
420  {
421  //
422  // Beta qualifier
423  //
424  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_BETA;
425  }
426  else if (!strcmp(qualifierStr, "rc"))
427  {
428  //
429  // Release Candidate qualifier
430  //
431  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_RC;
432  }
433  else if (!strcmp(qualifierStr, "stable"))
434  {
435  //
436  // Stable qualifier
437  //
438  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_STABLE;
439  }
440  else if (!strcmp(qualifierStr, "hotfix"))
441  {
442  //
443  // Hot Fix qualifier
444  //
446  }
447  else
448  {
449  //
450  // Unknown qualifier
451  //
452  errorCode = SBG_INVALID_PARAMETER;
453  }
454  }
455  else
456  {
457  //
458  // Invalid format
459  //
460  errorCode = SBG_INVALID_PARAMETER;
461  }
462 
463  return errorCode;
464 }
465 
472 SbgErrorCode sbgVersionFromStringEncoded(const char *pVersionStr, uint32 *pVersion)
473 {
474  SbgErrorCode errorCode = SBG_NO_ERROR;
475  SbgVersion versionInfo;
476 
477  //
478  // Check input parameters
479  //
480  SBG_ASSERT((pVersionStr) && (pVersion));
481 
482  //
483  // Parse the version from a string
484  //
485  errorCode = sbgVersionFromString(pVersionStr, &versionInfo);
486 
487  //
488  // Test that no error has occurred
489  //
490  if (errorCode == SBG_NO_ERROR)
491  {
492  //
493  // Encode the version and return it
494  //
495  *pVersion = sbgVersionEncode(&versionInfo);
496  }
497  else
498  {
499  //
500  // An error has occurred so return a zero version
501  //
502  *pVersion = 0;
503  }
504 
505  //
506  // Return error
507  //
508  return errorCode;
509 }
uint16 build
Definition: sbgVersion.h:106
Helper methods and definitions used to handle version.
void sbgVersionDecode(uint32 encodedVersion, SbgVersion *pVersionInfo)
Definition: sbgVersion.c:13
unsigned int uint32
Definition: sbgTypes.h:58
#define SBG_VERSION_SOFT_SCHEME_QUALIFIER_MASK
Definition: sbgVersion.h:54
#define SBG_VERSION_SOFT_SCHEME_BUILD_SHIFT
Definition: sbgVersion.h:64
#define SBG_VERSION_SOFT_SCHEME_MINOR_SHIFT
Definition: sbgVersion.h:61
SbgErrorCode sbgVersionFromString(const char *pVersionStr, SbgVersion *pVersionInfo)
Definition: sbgVersion.c:351
enum _SbgVersionCmpThresold SbgVersionCmpThresold
#define SBG_VERSION_SOFT_SCHEME_MINOR_MASK
Definition: sbgVersion.h:60
SbgErrorCode sbgVersionToString(const SbgVersion *pVersionInfo, char *pBuffer, uint32 sizeOfBuffer)
Definition: sbgVersion.c:229
uint32 sbgVersionEncode(const SbgVersion *pVersionInfo)
Definition: sbgVersion.c:70
SbgErrorCode sbgVersionToStringEncoded(uint32 version, char *pBuffer, uint32 sizeOfBuffer)
Definition: sbgVersion.c:326
bool softwareScheme
Definition: sbgVersion.h:101
SbgErrorCode sbgVersionFromStringEncoded(const char *pVersionStr, uint32 *pVersion)
Definition: sbgVersion.c:472
uint8 minor
Definition: sbgVersion.h:104
#define SBG_VERSION_SOFT_SCHEME_MAJOR_SHIFT
Definition: sbgVersion.h:58
enum _SbgVersionQualifier SbgVersionQualifier
Main header file for SBG Systems common C library.
#define SBG_VERSION_SOFT_SCHEME
Definition: sbgVersion.h:52
int32 sbgVersionCompare(const SbgVersion *pVersionA, const SbgVersion *pVersionB, SbgVersionCmpThresold thresold)
Definition: sbgVersion.c:124
uint8 major
Definition: sbgVersion.h:103
unsigned char uint8
Definition: sbgTypes.h:56
#define SBG_ASSERT(expression)
Definition: sbgDebug.h:52
#define SBG_VERSION_SOFTWARE(major, minor, build, qualifier)
Definition: sbgVersion.h:134
signed int int32
Definition: sbgTypes.h:64
unsigned short uint16
Definition: sbgTypes.h:57
#define SBG_VERSION_SOFT_SCHEME_BUILD_MASK
Definition: sbgVersion.h:63
enum _SbgErrorCode SbgErrorCode
#define SBG_VERSION_BASIC(major, minor, rev, build)
Definition: sbgVersion.h:121
#define SBG_VERSION_SOFT_SCHEME_MAJOR_MASK
Definition: sbgVersion.h:57
SbgVersionQualifier qualifier
Definition: sbgVersion.h:102
#define SBG_VERSION_SOFT_SCHEME_QUALIFIER_SHIFT
Definition: sbgVersion.h:55


sbg_driver
Author(s):
autogenerated on Sun Jan 27 2019 03:42:20