Changeset 36598 in vbox
- Timestamp:
- Apr 6, 2011 8:03:53 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/generic/RTDirCreateUniqueNumbered-generic.cpp
r36582 r36598 37 37 #include <iprt/string.h> 38 38 39 39 40 RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, signed int cchDigits, char chSep) 40 41 { … … 43 44 */ 44 45 AssertPtrReturn(pszPath, VERR_INVALID_POINTER); 45 AssertReturn(cbSize, VERR_INVALID_PARAMETER); 46 AssertReturn(cchDigits, VERR_INVALID_PARAMETER); 47 /* Check for enough space. */ 48 char *pszEnd = strchr(pszPath, '\0'); 46 AssertReturn(cbSize, VERR_BUFFER_OVERFLOW); 47 AssertReturn(cchDigits > 0, VERR_INVALID_PARAMETER); 48 49 /* Check that there is sufficient space. */ 50 char *pszEnd = RTStrEnd(pszPath, cbSize); 51 AssertReturn(pszEnd, VERR_BUFFER_OVERFLOW); 49 52 AssertReturn(cbSize - 1 - (pszEnd - pszPath) >= (size_t)cchDigits + (chSep ? 1 : 0), VERR_BUFFER_OVERFLOW); 53 size_t cbLeft = cbSize - (pszEnd - pszPath); 50 54 51 55 /* First try is to create the path without any numbers. */ … … 58 62 if (chSep != '\0') 59 63 { 64 cbLeft--; 60 65 *pszEnd++ = chSep; 61 66 *pszEnd = '\0'; 62 67 } 63 68 64 /* How many tries? */ 65 size_t cMaxTries = 10; 66 for (size_t a = 0; a < (size_t)cchDigits - 1; ++a) 67 cMaxTries *= 10; 69 /* How many tries? Stay within somewhat sane limits. */ 70 uint32_t cMaxTries; 71 if (cchDigits >= 8) 72 cMaxTries = 100 * _1M; 73 else 74 { 75 cMaxTries = 10; 76 for (int a = 0; a < cchDigits - 1; ++a) 77 cMaxTries *= 10; 78 } 68 79 69 /* Try cMaxTries - 1 counts to create a directory with the appended number. */70 size_t i = 1;80 /* Try cMaxTries - 1 times to create a directory with appended numbers. */ 81 uint32_t i = 1; 71 82 while (i < cMaxTries) 72 83 { 73 84 /* Format the number with leading zero's. */ 74 rc = RTStrFormat Number(pszEnd, i, 10, cchDigits, 0, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD);85 rc = RTStrFormatU32(pszEnd, cbLeft, i, 10, cchDigits, 0, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD); 75 86 if (RT_FAILURE(rc)) 76 87 {
Note:
See TracChangeset
for help on using the changeset viewer.