Changeset 24639 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Nov 13, 2009 4:09:36 PM (15 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/strtonum.cpp
r21337 r24639 33 33 * Header Files * 34 34 *******************************************************************************/ 35 #include <iprt/mem.h> 35 36 #include <iprt/string.h> 36 37 #include "internal/iprt.h" 37 38 39 #include <iprt/ctype.h> /* needed for RT_C_IS_DIGIT */ 38 40 #include <iprt/err.h> 39 41 … … 90 92 */ 91 93 94 95 /** 96 * Converts a string representation of a version number to an unsigned number. 97 * 98 * @returns iprt status code. 99 * Warnings are used to indicate convertion problems. 100 * @retval VWRN_NUMBER_TOO_BIG 101 * @retval VWRN_TRAILING_CHARS 102 * @retval VWRN_TRAILING_SPACES 103 * @retval VINF_SUCCESS 104 * @retval VERR_NO_MEMORY 105 * @retval VERR_NO_DIGITS 106 * 107 * @param pszValue Pointer to the string value. 108 * @param pu32 Where to store the converted number. 109 */ 110 int RTStrVersionToUInt32(const char *pszVer, uint32_t *pu32) 111 { 112 const char *str = pszVer; 113 AssertPtr(pu32); 114 AssertPtr(str); 115 116 char *strNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char)); 117 if (strNew == NULL) 118 return VERR_NO_MEMORY; 119 120 int rc = VERR_NO_DIGITS; 121 uint16_t c = 0; 122 bool fLastInvalid = false; 123 while ( str 124 && *str != '\0') 125 { 126 if (fLastInvalid) 127 { 128 if ( *str == '-' 129 || *str == '_') 130 { 131 fLastInvalid = false; 132 } 133 } 134 else 135 { 136 if (RT_C_IS_DIGIT(*str)) 137 { 138 strNew[c++] = *str; 139 } 140 else if ( *str != '.' 141 && c == 0) 142 { 143 fLastInvalid = true; 144 } 145 } 146 str++; 147 } 148 strNew[c] = '\0'; 149 150 /* Convert final number string to number */ 151 if (fLastInvalid) 152 { 153 *pu32 = 0; 154 rc = VERR_NO_DIGITS; 155 } 156 else 157 { 158 rc = RTStrToUInt32Ex(strNew, 159 NULL, /* Next pointer, not used */ 160 10 /* Number base */, 161 pu32); 162 if (rc != VINF_SUCCESS) 163 *pu32 = 0; 164 } 165 RTStrFree(strNew); 166 return rc; 167 } 92 168 93 169 -
trunk/src/VBox/Runtime/testcase/tstStrToVer.cpp
r24634 r24639 32 32 #include <iprt/stream.h> 33 33 #include <iprt/err.h> 34 35 36 #include <iprt/assert.h>37 38 int RTStrVersionToUInt32(const char *pszVer, uint32_t *pu32)39 {40 char *str = RTStrDup(pszVer);41 AssertPtr(pu32);42 AssertPtr(str);43 44 char *c = RTStrStrip(str);45 char *next;46 char n = '.';47 int rc;48 uint32_t t;49 50 *pu32 = 0;51 52 char szDelim[] = { '-', '_' };53 54 /* Get trailing content separated by dash (-) or underscore (_)*/55 //c = RTStrStr(c, &n)56 c = strchr(c, '-');57 if (c)58 c++; /* Skip dash */59 else60 c = str;61 62 /* Get last digit */63 rc = RTStrToUInt32Ex(c,64 &next,65 10 /* number base */,66 &t);67 *pu32 += t;68 RTStrFree(str);69 return rc;70 }71 34 72 35 … … 110 73 { 111 74 int cErrors = 0; 112 int rc;113 114 uint32_t l;115 rc = RTStrVersionToUInt32("asdf--234", &l);116 RTPrintf("num: %ld\n", l);117 118 75 119 76 static const struct TstU32 aTstU32[] = 120 77 { 121 { "asdf", VERR_NO_DIGITS, 0 }, 122 { "123", VINF_SUCCESS, 123 }, 123 { "45.63", VINF_SUCCESS, 4563 }, 124 { "68.54.123", VINF_SUCCESS, 6854123 }, 125 { "aasdf-1", VINF_SUCCESS, 1 }, 126 { "qwer-123.34", VINF_SUCCESS, 12334 }, 127 { "aasdf45-r4545-5", VINF_SUCCESS, 5 }, 128 { "foo41-r2431-6.9.8", VINF_SUCCESS, 698 }, 129 { "bar43-r3517-7.1.2-beta", VINF_SUCCESS, 712 }, 130 { "bar43-r3517-7.1.2.534-beta", VWRN_NUMBER_TOO_BIG, 0 }, 78 { "asdf", VERR_NO_DIGITS, 0 }, 79 { "asdf234", VERR_NO_DIGITS, 0 }, 80 { "123", VINF_SUCCESS, 123 }, 81 { "45.63", VINF_SUCCESS, 4563 }, 82 { "68.54.123", VINF_SUCCESS, 6854123 }, 83 { "1.0.3-asdf", VINF_SUCCESS, 103 }, 84 { "aasdf-1", VINF_SUCCESS, 1 }, 85 { "qwer-123.34", VINF_SUCCESS, 12334 }, 86 { "aasdf45-r4545-5", VINF_SUCCESS, 5 }, 87 { "foo41-r2431-6.9.8", VINF_SUCCESS, 698 }, 88 { "bar43-r3517-7.1.2-beta", VINF_SUCCESS, 712 }, 89 { "bar43-r3517-7.1.2.53412344556-beta", VWRN_NUMBER_TOO_BIG, 0 }, 131 90 }; 132 91 RUN_TESTS(aTstU32, uint32_t, "%#ld", RTStrVersionToUInt32); … … 139 98 else 140 99 RTPrintf("tstStrToVer: FAILURE - %d errors\n", cErrors); 141 //return !!cErrors; 142 return 0; /* Don't report any failure yet, makes test boxes unhappy */ 100 return !!cErrors; 143 101 }
Note:
See TracChangeset
for help on using the changeset viewer.