1 | /* $Id: strversion.cpp 24662 2009-11-14 23:41:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Version String Parsing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/ctype.h> /* needed for RT_C_IS_DIGIT */
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Converts a string representation of a version number to an unsigned number.
|
---|
47 | *
|
---|
48 | * @returns iprt status code.
|
---|
49 | * Warnings are used to indicate convertion problems.
|
---|
50 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
51 | * @retval VWRN_TRAILING_CHARS
|
---|
52 | * @retval VWRN_TRAILING_SPACES
|
---|
53 | * @retval VINF_SUCCESS
|
---|
54 | * @retval VERR_NO_MEMORY
|
---|
55 | * @retval VERR_NO_DIGITS
|
---|
56 | *
|
---|
57 | * @param pszValue Pointer to the string value.
|
---|
58 | * @param pu32 Where to store the converted number.
|
---|
59 | *
|
---|
60 | * @todo r=bird: The returned value isn't really suitable for comparing two
|
---|
61 | * version strings. Try see which result you get when converting
|
---|
62 | * "3.0.14" and "3.1.0" and comparing the values. The way to fix this
|
---|
63 | * deficiency would be to convert the individual parts and dividing the
|
---|
64 | * return value into sections: bits 31:24 FirstNumber; 23:16 Second;
|
---|
65 | * 15:8 Third; 7:0 Forth. It would probably be a good idea to use a
|
---|
66 | * 64-bit return value instead of a 32-bit one, so there is room for
|
---|
67 | * revision number when found.
|
---|
68 | *
|
---|
69 | * Actually, because of the above, the kind of API I had in mind was
|
---|
70 | * int RTStrVersionCompare(const char *pszVer1, const char *pszVer2).
|
---|
71 | * It wouldn't try convert input to numbers, just do a parallel parse.
|
---|
72 | * This would allow easy handling beta/alpha/++ indicators and any
|
---|
73 | * number of dots and dashes.
|
---|
74 | */
|
---|
75 | RTDECL(int) RTStrVersionToUInt32(const char *pszVer, uint32_t *pu32)
|
---|
76 | {
|
---|
77 | const char *psz = pszVer;
|
---|
78 | AssertPtr(pu32);
|
---|
79 | AssertPtr(psz);
|
---|
80 |
|
---|
81 | char *pszNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char));
|
---|
82 | if (pszNew == NULL)
|
---|
83 | return VERR_NO_MEMORY;
|
---|
84 |
|
---|
85 | unsigned i = 0;
|
---|
86 | bool fLastInvalid = false;
|
---|
87 | while ( psz
|
---|
88 | && *psz != '\0')
|
---|
89 | {
|
---|
90 | if (fLastInvalid)
|
---|
91 | {
|
---|
92 | if ( *psz == '-'
|
---|
93 | || *psz == '_')
|
---|
94 | fLastInvalid = false;
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | if (RT_C_IS_DIGIT(*psz))
|
---|
99 | pszNew[i++] = *psz;
|
---|
100 | else if ( *psz != '.'
|
---|
101 | && i == 0)
|
---|
102 | fLastInvalid = true;
|
---|
103 | }
|
---|
104 | psz++;
|
---|
105 | }
|
---|
106 | pszNew[i] = '\0';
|
---|
107 |
|
---|
108 | /* Convert final number string to number */
|
---|
109 | int rc;
|
---|
110 | if (fLastInvalid)
|
---|
111 | {
|
---|
112 | *pu32 = 0;
|
---|
113 | rc = VERR_NO_DIGITS;
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | rc = RTStrToUInt32Ex(pszNew, NULL /*pszNext*/, 10 /*uBase*/, pu32);
|
---|
118 | if (rc != VINF_SUCCESS)
|
---|
119 | *pu32 = 0;
|
---|
120 | }
|
---|
121 | RTStrFree(pszNew);
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|