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