VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strversion.cpp@ 81106

Last change on this file since 81106 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.9 KB
Line 
1/* $Id: strversion.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Version String Parsing.
4 */
5
6/*
7 * Copyright (C) 2009-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/string.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/errcore.h>
37
38
39/*********************************************************************************************************************************
40* Defined Constants And Macros *
41*********************************************************************************************************************************/
42#define RTSTRVER_IS_PUNCTUACTION(ch) \
43 ( (ch) == '_' || (ch) == '-' || (ch) == '+' || RT_C_IS_PUNCT(ch) )
44
45
46/**
47 * Parses a out the next block from a version string.
48 *
49 * @returns true if numeric, false if not.
50 * @param ppszVer The string cursor, IN/OUT.
51 * @param pi32Value Where to return the value if numeric.
52 * @param pcchBlock Where to return the block length.
53 */
54static bool rtStrVersionParseBlock(const char **ppszVer, int32_t *pi32Value, size_t *pcchBlock)
55{
56 const char *psz = *ppszVer;
57
58 /*
59 * Check for end-of-string.
60 */
61 if (!*psz)
62 {
63 *pi32Value = 0;
64 *pcchBlock = 0;
65 return false;
66 }
67
68 /*
69 * Try convert the block to a number the simple way.
70 */
71 char ch;
72 bool fNumeric = RT_C_IS_DIGIT(*psz);
73 if (fNumeric)
74 {
75 do
76 ch = *++psz;
77 while (ch && RT_C_IS_DIGIT(ch));
78
79 int rc = RTStrToInt32Ex(*ppszVer, NULL, 10, pi32Value);
80 if (RT_FAILURE(rc) || rc == VWRN_NUMBER_TOO_BIG)
81 {
82 AssertRC(rc);
83 fNumeric = false;
84 *pi32Value = 0;
85 }
86 }
87 else
88 {
89 /*
90 * Find the end of the current string. Make a special case for SVN
91 * revision numbers that immediately follows a release tag string.
92 */
93 do
94 ch = *++psz;
95 while ( ch
96 && !RT_C_IS_DIGIT(ch)
97 && !RTSTRVER_IS_PUNCTUACTION(ch));
98
99 size_t cchBlock = psz - *ppszVer;
100 if ( cchBlock > 1
101 && psz[-1] == 'r'
102 && RT_C_IS_DIGIT(*psz))
103 {
104 psz--;
105 cchBlock--;
106 }
107
108
109 /*
110 * Translate standard pre release terms to negative values.
111 */
112 static const struct
113 {
114 size_t cch;
115 const char *psz;
116 int32_t iValue;
117 } s_aTerms[] =
118 {
119 { 2, "RC", -100000 },
120 { 3, "PRE", -200000 },
121 { 5, "GAMMA", -300000 },
122 { 4, "BETA", -400000 },
123 { 5, "ALPHA", -500000 }
124 };
125
126 int32_t iVal1 = 0;
127 for (unsigned i = 0; i < RT_ELEMENTS(s_aTerms); i++)
128 if ( cchBlock == s_aTerms[i].cch
129 && !RTStrNCmp(s_aTerms[i].psz, *ppszVer, cchBlock))
130 {
131 iVal1 = s_aTerms[i].iValue;
132 break;
133 }
134 if (iVal1 != 0)
135 {
136 /*
137 * Does the prelease term have a trailing number?
138 * Add it assuming BETA == BETA1.
139 */
140 if (RT_C_IS_DIGIT(*psz))
141 {
142 const char *psz2 = psz;
143 do
144 ch = *++psz;
145 while ( ch
146 && RT_C_IS_DIGIT(ch)
147 && !RTSTRVER_IS_PUNCTUACTION(ch));
148
149 int rc = RTStrToInt32Ex(psz2, NULL, 10, pi32Value);
150 if (RT_SUCCESS(rc) && rc != VWRN_NUMBER_TOO_BIG && *pi32Value)
151 iVal1 += *pi32Value - 1;
152 else
153 {
154 AssertRC(rc);
155 psz = psz2;
156 }
157 }
158 fNumeric = true;
159 }
160 *pi32Value = iVal1;
161 }
162 *pcchBlock = psz - *ppszVer;
163
164 /*
165 * Skip trailing punctuation.
166 */
167 if (RTSTRVER_IS_PUNCTUACTION(*psz))
168 psz++;
169 *ppszVer = psz;
170
171 return fNumeric;
172}
173
174
175RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2)
176{
177 AssertPtr(pszVer1);
178 AssertPtr(pszVer2);
179
180 /*
181 * Do a parallel parse of the strings.
182 */
183 while (*pszVer1 || *pszVer2)
184 {
185 const char *pszBlock1 = pszVer1;
186 size_t cchBlock1;
187 int32_t iVal1;
188 bool fNumeric1 = rtStrVersionParseBlock(&pszVer1, &iVal1, &cchBlock1);
189
190 const char *pszBlock2 = pszVer2;
191 size_t cchBlock2;
192 int32_t iVal2;
193 bool fNumeric2 = rtStrVersionParseBlock(&pszVer2, &iVal2, &cchBlock2);
194
195 if (fNumeric1 && fNumeric2)
196 {
197 if (iVal1 != iVal2)
198 return iVal1 < iVal2 ? -1 : 1;
199 }
200 else if ( fNumeric1 != fNumeric2
201 && ( fNumeric1
202 ? iVal1 == 0 && cchBlock2 == 0
203 : iVal2 == 0 && cchBlock1 == 0)
204 )
205 {
206 /*else: 1.0 == 1.0.0.0.0. */;
207 }
208 else if ( fNumeric1 != fNumeric2
209 && (fNumeric1 ? iVal1 : iVal2) < 0)
210 {
211 /* Pre-release indicators are smaller than all other strings. */
212 return fNumeric1 ? -1 : 1;
213 }
214 else
215 {
216 int iDiff = RTStrNICmp(pszBlock1, pszBlock2, RT_MIN(cchBlock1, cchBlock2));
217 if (!iDiff && cchBlock1 != cchBlock2)
218 iDiff = cchBlock1 < cchBlock2 ? -1 : 1;
219 if (iDiff)
220 return iDiff < 0 ? -1 : 1;
221 }
222 }
223 return 0;
224}
225RT_EXPORT_SYMBOL(RTStrVersionCompare);
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette