Changeset 24658 in vbox for trunk/src/VBox/Runtime/common
- Timestamp:
- Nov 14, 2009 11:31:36 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/strtonum.cpp
r24645 r24658 37 37 #include "internal/iprt.h" 38 38 39 #include <iprt/assert.h> 39 40 #include <iprt/ctype.h> /* needed for RT_C_IS_DIGIT */ 40 41 #include <iprt/err.h> … … 107 108 * @param pszValue Pointer to the string value. 108 109 * @param pu32 Where to store the converted number. 110 * 111 * @remarks The returned value isn't really suitable for comparing two version 112 * strings. Try see which result you get when converting "3.0.14" and 113 * "3.1.0" and comparing the values. The way to fix this deficiency 114 * would be to convert the individual parts and dividing the return 115 * value into sections: bits 31:24 FirstNumber; 23:16 Second; 15:8 116 * Third; 7:0 Forth. It would probably be a good idea to use a 64-bit 117 * return value instead of a 32-bit one, so there is room for revision 118 * number when found. 119 * 120 * Actually, because of the above, the kind of API I had in mind was 121 * int RTStrVersionCompare(const char *pszVer1, const char *pszVer2). 122 * It wouldn't try convert input to numbers, just do a parallel parse. 123 * This would allow easy handling beta/alpha/++ indicators and any 124 * number of dots and dashes. 109 125 */ 110 126 RTDECL(int) RTStrVersionToUInt32(const char *pszVer, uint32_t *pu32) 111 127 { 112 const char * str= pszVer;128 const char *psz = pszVer; 113 129 AssertPtr(pu32); 114 AssertPtr( str);115 116 char * strNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char));117 if ( strNew == NULL)130 AssertPtr(psz); 131 132 char *pszNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char)); 133 if (pszNew == NULL) 118 134 return VERR_NO_MEMORY; 119 135 120 int rc = VERR_NO_DIGITS; 121 uint16_t c = 0; 122 bool fLastInvalid = false; 123 while ( str 124 && *str != '\0') 136 unsigned i = 0; 137 bool fLastInvalid = false; 138 while ( psz 139 && *psz != '\0') 125 140 { 126 141 if (fLastInvalid) 127 142 { 128 if ( *str == '-' 129 || *str == '_') 130 { 143 if ( *psz == '-' 144 || *psz == '_') 131 145 fLastInvalid = false; 132 }133 146 } 134 147 else 135 148 { 136 if (RT_C_IS_DIGIT(*str)) 137 { 138 strNew[c++] = *str; 139 } 140 else if ( *str != '.' 141 && c == 0) 142 { 149 if (RT_C_IS_DIGIT(*psz)) 150 pszNew[i++] = *psz; 151 else if ( *psz != '.' 152 && i == 0) 143 153 fLastInvalid = true; 144 }145 154 } 146 str++;147 } 148 strNew[c] = '\0';155 psz++; 156 } 157 pszNew[i] = '\0'; 149 158 150 159 /* Convert final number string to number */ 160 int rc; 151 161 if (fLastInvalid) 152 162 {
Note:
See TracChangeset
for help on using the changeset viewer.