Changeset 7183 in vbox
- Timestamp:
- Feb 27, 2008 5:41:30 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 28483
- Location:
- trunk
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/string.h
r7170 r7183 537 537 RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags); 538 538 539 540 /** 541 * Callback for formatting a type. 542 * 543 * This is registered using the RTStrFormatTypeRegister function and will 544 * be called during string formatting to handle the specified %R[type]. 545 * The argument for this format type is assumed to be a pointer and it's 546 * passed in the @a pvValue argument. 547 * 548 * @returns Length of the formatted output. 549 * @param pfnOutput Output worker. 550 * @param pvArgOutput Argument to the output worker. 551 * @param pszType The type name. 552 * @param pvValue The argument value. 553 * @param cchWidth Width. 554 * @param cchPrecision Precision. 555 * @param fFlags Flags (NTFS_*). 556 * @param pvUser The user argument. 557 */ 558 typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, 559 const char *pszType, void const *pvValue, 560 int cchWidth, int cchPrecision, unsigned fFlags, 561 void *pvUser); 562 /** Pointer to a FNRTSTRFORMATTYPE. */ 563 typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE; 564 565 566 /** 567 * Register a format handler for a type. 568 * 569 * The format handler is used to handle '%R[type]' format types, where the argument 570 * in the vector is a pointer value (a bit restrictive, but keeps it simple). 571 * 572 * The caller must ensure that no other thread will be making use of any of 573 * the dynamic formatting type facilities simultaneously with this call. 574 * 575 * @returns IPRT status code. 576 * @retval VINF_SUCCESS on success. 577 * @retval VERR_ALREADY_EXISTS if the type has already been registered. 578 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already. 579 * 580 * @param pszType The type name. 581 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details. 582 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser 583 * for how to update this later. 584 */ 585 RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser); 586 587 /** 588 * Deregisters a format type. 589 * 590 * The caller must ensure that no other thread will be making use of any of 591 * the dynamic formatting type facilities simultaneously with this call. 592 * 593 * @returns IPRT status code. 594 * @retval VINF_SUCCESS on success. 595 * @retval VERR_FILE_NOT_FOUND if not found. 596 * 597 * @param pszType The type to deregister. 598 */ 599 RTDECL(int) RTStrFormatTypeDeregister(const char *pszType); 600 601 /** 602 * Sets the user argument for a type. 603 * 604 * This can be used if a user argument needs relocating in GC. 605 * 606 * @returns IPRT status code. 607 * @retval VINF_SUCCESS on success. 608 * @retval VERR_FILE_NOT_FOUND if not found. 609 * 610 * @param pszType The type to update. 611 * @param pvUser The new user argument value. 612 */ 613 RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser); 614 615 539 616 /** 540 617 * String printf. -
trunk/src/VBox/Additions/linux/module/Makefile.module
r6857 r7183 42 42 strformat.o \ 43 43 strformatrt.o \ 44 strformattype.o \ 44 45 strformat-vbox.o 45 46 -
trunk/src/VBox/Additions/linux/module/files_vboxadd
r5999 r7183 60 60 ${PATH_ROOT}/src/VBox/Runtime/common/string/strformat.cpp=>strformat.c \ 61 61 ${PATH_ROOT}/src/VBox/Runtime/common/string/strformatrt.cpp=>strformatrt.c \ 62 ${PATH_ROOT}/src/VBox/Runtime/common/string/strformattype.cpp=>strformattype.c \ 62 63 ${PATH_ROOT}/src/VBox/Runtime/r0drv/alloc-r0drv.cpp=>r0drv/alloc-r0drv.c \ 63 64 ${PATH_ROOT}/src/VBox/Runtime/r0drv/alloc-r0drv.h=>r0drv/alloc-r0drv.h \ -
trunk/src/VBox/HostDrivers/Support/linux/Makefile
r7130 r7183 94 94 common/string/strformat.o \ 95 95 common/string/strformatrt.o \ 96 common/string/strformattype.o \ 96 97 common/string/strprintf.o \ 97 98 common/string/strtonum.o \ -
trunk/src/VBox/HostDrivers/Support/linux/files_vboxdrv
r7004 r7183 69 69 ${PATH_ROOT}/src/VBox/Runtime/common/string/strformat.cpp=>common/string/strformat.c \ 70 70 ${PATH_ROOT}/src/VBox/Runtime/common/string/strformatrt.cpp=>common/string/strformatrt.c \ 71 ${PATH_ROOT}/src/VBox/Runtime/common/string/strformattype.cpp=>common/string/strformattype.c \ 71 72 ${PATH_ROOT}/src/VBox/Runtime/common/string/strprintf.cpp=>common/string/strprintf.c \ 72 73 ${PATH_ROOT}/src/VBox/Runtime/common/string/strtonum.cpp=>common/string/strtonum.c \ -
trunk/src/VBox/Runtime/Makefile.kmk
r7169 r7183 188 188 common/string/strformat.cpp \ 189 189 common/string/strformatrt.cpp \ 190 common/string/strformattype.cpp \ 190 191 common/string/string.cpp \ 191 192 common/string/strprintf.cpp \ … … 566 567 common/string/strformat.cpp \ 567 568 common/string/strformatrt.cpp \ 569 common/string/strformattype.cpp \ 568 570 common/string/string.cpp \ 569 571 common/string/strprintf.cpp \ … … 766 768 common/string/strformat.cpp \ 767 769 common/string/strformatrt.cpp \ 770 common/string/strformattype.cpp \ 768 771 common/string/strlen.cpp \ 769 772 common/string/strncmp.cpp \ … … 861 864 common/string/strformat.cpp \ 862 865 common/string/strformatrt.cpp \ 866 common/string/strformattype.cpp \ 863 867 common/string/strprintf.cpp \ 864 868 common/string/strtonum.cpp \ … … 1110 1114 common/string/strformat.cpp \ 1111 1115 common/string/strformatrt.cpp \ 1116 common/string/strformattype.cpp \ 1112 1117 common/string/strlen.cpp \ 1113 1118 common/string/strncmp.cpp \ -
trunk/src/VBox/Runtime/common/string/strformat.cpp
r6296 r7183 770 770 case 'R': 771 771 { 772 pszFormat--; 773 cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchPrecision, cchWidth, fFlags, chArgSize); 772 if (*pszFormat != '[') 773 { 774 pszFormat--; 775 cch += rtstrFormatRt(pfnOutput, pvArgOutput, &pszFormat, &args, cchPrecision, cchWidth, fFlags, chArgSize); 776 } 777 else 778 { 779 pszFormat--; 780 cch += rtstrFormatType(pfnOutput, pvArgOutput, &pszFormat, &args, cchPrecision, cchWidth, fFlags, chArgSize); 781 } 774 782 break; 775 783 } -
trunk/src/VBox/Runtime/include/internal/string.h
r5999 r7183 52 52 #endif 53 53 size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize); 54 size_t rtstrFormatType(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize); 54 55 55 56 __END_DECLS -
trunk/src/VBox/Runtime/testcase/tstStrFormat.cpp
r5999 r7183 34 34 #include <iprt/stream.h> 35 35 36 /******************************************************************************* 37 * Global Variables * 38 *******************************************************************************/ 39 static int g_cErrors = 0; 40 41 42 /** See FNRTSTRFORMATTYPE. */ 43 static DECLCALLBACK(size_t) TstType(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, 44 const char *pszType, void const *pvValue, 45 int cchWidth, int cchPrecision, unsigned fFlags, 46 void *pvUser) 47 { 48 /* validate */ 49 if (strncmp(pszType, "type", 4)) 50 { 51 RTPrintf("tstStrFormat: pszType=%s expected 'typeN'\n", pszType); 52 g_cErrors++; 53 } 54 55 int iType = pszType[4] - '0'; 56 if ((uintptr_t)pvUser != (uintptr_t)TstType + iType) 57 { 58 RTPrintf("tstStrFormat: pvValue=%p expected %p\n", pvUser, (void *)((uintptr_t)TstType + iType)); 59 g_cErrors++; 60 } 61 62 /* format */ 63 size_t cch = pfnOutput(pvArgOutput, pszType, 5); 64 cch += pfnOutput(pvArgOutput, "=", 1); 65 char szNum[64]; 66 size_t cchNum = RTStrFormatNumber(szNum, (uintptr_t)pvValue, 0, cchWidth, cchPrecision, fFlags); 67 cch += pfnOutput(pvArgOutput, szNum, cchNum); 68 return cch; 69 } 70 71 36 72 int main() 37 73 { 38 74 RTR3Init(); 39 75 40 int cErrors = 0;41 76 uint32_t u32 = 0x010; 42 77 uint64_t u64 = 0x100; … … 49 84 RTPrintf("error: '%s'\n" 50 85 "wanted 'u32=16 u64=256 u64=0x100'\n", szStr); 51 cErrors++;86 g_cErrors++; 52 87 } 53 88 … … 60 95 "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", szStr); 61 96 RTPrintf("%d\n", (int)(u64 % 10)); 62 cErrors++;97 g_cErrors++; 63 98 } 64 99 … … 72 107 "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", szStr); 73 108 RTPrintf("%d\n", (int)(u64 % 10)); 74 cErrors++;109 g_cErrors++; 75 110 } 76 111 … … 82 117 RTPrintf("error: '%s'\n" 83 118 "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", szStr); 84 cErrors++;119 g_cErrors++; 85 120 } 86 121 … … 96 131 "expected: '%s'\n", 97 132 szStr, szCorrect); 98 cErrors++;133 g_cErrors++; 99 134 } 100 135 … … 105 140 { 106 141 RTPrintf("error: RTStrAPrintf failed, cch2=%d\n", cch2); 107 cErrors++;142 g_cErrors++; 108 143 } 109 144 else if (strcmp(psz, "Hey there! This is a test!")) … … 113 148 "wanted: 'Hey there! This is a test!'\n", 114 149 psz); 115 cErrors++;150 g_cErrors++; 116 151 } 117 152 else if ((int)strlen(psz) != cch2) 118 153 { 119 154 RTPrintf("error: RTStrAPrintf failed, cch2 == %d expected %u\n", cch2, strlen(psz)); 120 cErrors++;155 g_cErrors++; 121 156 } 122 157 RTStrFree(psz); … … 131 166 " wanted: '%s'\n", \ 132 167 __LINE__, fmt, szStr, out " 42=42 " out " 42=42"); \ 133 cErrors++; \168 g_cErrors++; \ 134 169 } \ 135 170 else if (cch != sizeof(out " 42=42 " out " 42=42") - 1) \ … … 137 172 RTPrintf("error(%d): Invalid length %d returned, expected %u!\n", \ 138 173 __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \ 139 cErrors++; \174 g_cErrors++; \ 140 175 } \ 141 176 } while (0) … … 314 349 "expected: '%s'\n", 315 350 szStr, szCorrect); 316 cErrors++;351 g_cErrors++; 317 352 } 318 353 … … 351 386 RTPrintf("error: '%s'\n" \ 352 387 "expected: '%s'\n", szStr, Correct); \ 353 cErrors++; \388 g_cErrors++; \ 354 389 } 355 390 … … 394 429 #endif 395 430 431 /* 432 * Custom types. 433 */ 434 #define CHECK(expr) do { if (!(expr)) { RTPrintf("tstEnv: error line %d: %s\n", __LINE__, #expr); g_cErrors++; } } while (0) 435 #define CHECK_RC(expr, rc) do { int rc2 = expr; if (rc2 != (rc)) { RTPrintf("tstEnv: error line %d: %s -> %Rrc expected %Rrc\n", __LINE__, #expr, rc2, rc); g_cErrors++; } } while (0) 436 437 CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); 438 CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS); 439 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3]", (void *)1); 440 CHECKSTR("type3=1"); 441 442 CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); 443 CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS); 444 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1]", (void *)1, (void *)2); 445 CHECKSTR("type3=1 type1=2"); 446 447 CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); 448 CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS); 449 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3); 450 CHECKSTR("type3=1 type1=2 type4=3"); 451 452 CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); 453 CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS); 454 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4); 455 CHECKSTR("type3=1 type1=2 type4=3 type2=4"); 456 457 CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS); 458 CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS); 459 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)1, (void *)2, (void *)3, (void *)4, (void *)5); 460 CHECKSTR("type3=1 type1=2 type4=3 type2=4 type5=5"); 461 462 CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS); 463 CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS); 464 CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS); 465 CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS); 466 CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS); 467 468 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40, (void *)50); 469 CHECKSTR("type3=10 type1=20 type4=30 type2=40 type5=50"); 470 471 CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS); 472 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40); 473 CHECKSTR("type3=10 type1=20 type4=30 type5=40"); 474 475 CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS); 476 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30); 477 CHECKSTR("type3=10 type1=20 type4=30"); 478 479 CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS); 480 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1]", (void *)10, (void *)20); 481 CHECKSTR("type3=10 type1=20"); 482 483 CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS); 484 cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3]", (void *)10); 485 CHECKSTR("type3=10"); 486 487 CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS); 396 488 397 489 /* 398 490 * Summarize and exit. 399 491 */ 400 if (! cErrors)492 if (!g_cErrors) 401 493 RTPrintf("tstStrFormat: SUCCESS\n"); 402 494 else 403 RTPrintf("tstStrFormat: FAILED - %d errors\n", cErrors);404 return !! cErrors;495 RTPrintf("tstStrFormat: FAILED - %d errors\n", g_cErrors); 496 return !!g_cErrors; 405 497 } 406 498
Note:
See TracChangeset
for help on using the changeset viewer.