Changeset 5712 in vbox for trunk/src/VBox/Runtime/common/string/strtonum.cpp
- Timestamp:
- Nov 12, 2007 3:40:19 PM (18 years ago)
- svn:sync-xref-src-repo-rev:
- 26066
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/strtonum.cpp
r5413 r5712 83 83 * @returns iprt status code. 84 84 * Warnings are used to indicate convertion problems. 85 * @retval VWRN_NUMBER_TOO_BIG 86 * @retval VWRN_NEGATIVE_UNSIGNED 87 * @retval VWRN_TRAILING_CHARS 88 * @retval VWRN_TRAILING_SPACES 89 * @retval VINF_SUCCESS 90 * @retval VERR_NO_DIGITS 91 * 85 92 * @param pszValue Pointer to the string value. 86 93 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 173 180 *ppszNext = (char *)psz; 174 181 182 /* 183 * Warn about trailing chars/spaces. 184 */ 185 if ( rc == VINF_SUCCESS 186 && *psz) 187 { 188 while (*psz == ' ' || *psz == '\t') 189 psz++; 190 rc = *psz ? VWRN_TRAILING_CHARS : VWRN_TRAILING_SPACES; 191 } 192 193 return rc; 194 } 195 196 197 /** 198 * Converts a string representation of a number to a 64-bit unsigned number, 199 * making sure the full string is converted. 200 * 201 * @returns iprt status code. 202 * Warnings are used to indicate convertion problems. 203 * @retval VWRN_NUMBER_TOO_BIG 204 * @retval VWRN_NEGATIVE_UNSIGNED 205 * @retval VINF_SUCCESS 206 * @retval VERR_NO_DIGITS 207 * @retval VERR_TRAILING_SPACES 208 * @retval VERR_TRAILING_CHARS 209 * 210 * @param pszValue Pointer to the string value. 211 * @param uBase The base of the representation used. 212 * If the function will look for known prefixes before defaulting to 10. 213 * @param pu64 Where to store the converted number. (optional) 214 */ 215 RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64) 216 { 217 char *psz; 218 int rc = RTStrToUInt64Ex(pszValue, &psz, uBase, pu64); 219 if (RT_SUCCESS(rc) && *psz) 220 { 221 if (rc == VWRN_TRAILING_CHARS || rc == VWRN_TRAILING_SPACES) 222 rc = -rc; 223 else 224 { 225 while (*psz == ' ' || *psz == '\t') 226 psz++; 227 rc = *psz ? VERR_TRAILING_CHARS : VERR_TRAILING_SPACES; 228 } 229 } 175 230 return rc; 176 231 } … … 200 255 * @returns iprt status code. 201 256 * Warnings are used to indicate convertion problems. 257 * @retval VWRN_NUMBER_TOO_BIG 258 * @retval VWRN_NEGATIVE_UNSIGNED 259 * @retval VWRN_TRAILING_CHARS 260 * @retval VWRN_TRAILING_SPACES 261 * @retval VINF_SUCCESS 262 * @retval VERR_NO_DIGITS 263 * 202 264 * @param pszValue Pointer to the string value. 203 265 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 210 272 uint64_t u64; 211 273 int rc = RTStrToUInt64Ex(pszValue, ppszNext, uBase, &u64); 274 if (rc == VINF_SUCCESS) 275 { 276 if (u64 & ~0xffffffffULL) 277 rc = VWRN_NUMBER_TOO_BIG; 278 } 279 if (pu32) 280 *pu32 = (uint32_t)u64; 281 return rc; 282 } 283 284 285 /** 286 * Converts a string representation of a number to a 32-bit unsigned number, 287 * making sure the full string is converted. 288 * 289 * @returns iprt status code. 290 * Warnings are used to indicate convertion problems. 291 * @retval VWRN_NUMBER_TOO_BIG 292 * @retval VWRN_NEGATIVE_UNSIGNED 293 * @retval VINF_SUCCESS 294 * @retval VERR_NO_DIGITS 295 * @retval VERR_TRAILING_SPACES 296 * @retval VERR_TRAILING_CHARS 297 * 298 * @param pszValue Pointer to the string value. 299 * @param uBase The base of the representation used. 300 * If the function will look for known prefixes before defaulting to 10. 301 * @param pu32 Where to store the converted number. (optional) 302 */ 303 RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32) 304 { 305 uint64_t u64; 306 int rc = RTStrToUInt64Full(pszValue, uBase, &u64); 212 307 if (rc == VINF_SUCCESS) 213 308 { … … 244 339 * @returns iprt status code. 245 340 * Warnings are used to indicate convertion problems. 341 * @retval VWRN_NUMBER_TOO_BIG 342 * @retval VWRN_NEGATIVE_UNSIGNED 343 * @retval VWRN_TRAILING_CHARS 344 * @retval VWRN_TRAILING_SPACES 345 * @retval VINF_SUCCESS 346 * @retval VERR_NO_DIGITS 347 * 246 348 * @param pszValue Pointer to the string value. 247 349 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 254 356 uint64_t u64; 255 357 int rc = RTStrToUInt64Ex(pszValue, ppszNext, uBase, &u64); 358 if (rc == VINF_SUCCESS) 359 { 360 if (u64 & ~0xffffULL) 361 rc = VWRN_NUMBER_TOO_BIG; 362 } 363 if (pu16) 364 *pu16 = (uint16_t)u64; 365 return rc; 366 } 367 368 369 /** 370 * Converts a string representation of a number to a 16-bit unsigned number, 371 * making sure the full string is converted. 372 * 373 * @returns iprt status code. 374 * Warnings are used to indicate convertion problems. 375 * @retval VWRN_NUMBER_TOO_BIG 376 * @retval VWRN_NEGATIVE_UNSIGNED 377 * @retval VINF_SUCCESS 378 * @retval VERR_NO_DIGITS 379 * @retval VERR_TRAILING_SPACES 380 * @retval VERR_TRAILING_CHARS 381 * 382 * @param pszValue Pointer to the string value. 383 * @param uBase The base of the representation used. 384 * If the function will look for known prefixes before defaulting to 10. 385 * @param pu16 Where to store the converted number. (optional) 386 */ 387 RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16) 388 { 389 uint64_t u64; 390 int rc = RTStrToUInt64Full(pszValue, uBase, &u64); 256 391 if (rc == VINF_SUCCESS) 257 392 { … … 288 423 * @returns iprt status code. 289 424 * Warnings are used to indicate convertion problems. 425 * @retval VWRN_NUMBER_TOO_BIG 426 * @retval VWRN_NEGATIVE_UNSIGNED 427 * @retval VWRN_TRAILING_CHARS 428 * @retval VWRN_TRAILING_SPACES 429 * @retval VINF_SUCCESS 430 * @retval VERR_NO_DIGITS 431 * 290 432 * @param pszValue Pointer to the string value. 291 433 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 298 440 uint64_t u64; 299 441 int rc = RTStrToUInt64Ex(pszValue, ppszNext, uBase, &u64); 442 if (rc == VINF_SUCCESS) 443 { 444 if (u64 & ~0xffULL) 445 rc = VWRN_NUMBER_TOO_BIG; 446 } 447 if (pu8) 448 *pu8 = (uint8_t)u64; 449 return rc; 450 } 451 452 453 /** 454 * Converts a string representation of a number to a 8-bit unsigned number, 455 * making sure the full string is converted. 456 * 457 * @returns iprt status code. 458 * Warnings are used to indicate convertion problems. 459 * @retval VWRN_NUMBER_TOO_BIG 460 * @retval VWRN_NEGATIVE_UNSIGNED 461 * @retval VINF_SUCCESS 462 * @retval VERR_NO_DIGITS 463 * @retval VERR_TRAILING_SPACES 464 * @retval VERR_TRAILING_CHARS 465 * 466 * @param pszValue Pointer to the string value. 467 * @param uBase The base of the representation used. 468 * If the function will look for known prefixes before defaulting to 10. 469 * @param pu8 Where to store the converted number. (optional) 470 */ 471 RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8) 472 { 473 uint64_t u64; 474 int rc = RTStrToUInt64Full(pszValue, uBase, &u64); 300 475 if (rc == VINF_SUCCESS) 301 476 { … … 337 512 * @returns iprt status code. 338 513 * Warnings are used to indicate convertion problems. 514 * @retval VWRN_NUMBER_TOO_BIG 515 * @retval VWRN_TRAILING_CHARS 516 * @retval VWRN_TRAILING_SPACES 517 * @retval VINF_SUCCESS 518 * @retval VERR_NO_DIGITS 519 * 339 520 * @param pszValue Pointer to the string value. 340 521 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 423 604 *ppszNext = (char *)psz; 424 605 606 /* 607 * Warn about trailing chars/spaces. 608 */ 609 if ( rc == VINF_SUCCESS 610 && *psz) 611 { 612 while (*psz == ' ' || *psz == '\t') 613 psz++; 614 rc = *psz ? VWRN_TRAILING_CHARS : VWRN_TRAILING_SPACES; 615 } 616 617 return rc; 618 } 619 620 621 /** 622 * Converts a string representation of a number to a 64-bit signed number, 623 * making sure the full string is converted. 624 * 625 * @returns iprt status code. 626 * Warnings are used to indicate convertion problems. 627 * @retval VWRN_NUMBER_TOO_BIG 628 * @retval VINF_SUCCESS 629 * @retval VERR_TRAILING_CHARS 630 * @retval VERR_TRAILING_SPACES 631 * @retval VERR_NO_DIGITS 632 * 633 * @param pszValue Pointer to the string value. 634 * @param uBase The base of the representation used. 635 * If the function will look for known prefixes before defaulting to 10. 636 * @param pi64 Where to store the converted number. (optional) 637 */ 638 RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64) 639 { 640 char *psz; 641 int rc = RTStrToInt64Ex(pszValue, &psz, uBase, pi64); 642 if (RT_SUCCESS(rc) && *psz) 643 { 644 if (rc == VWRN_TRAILING_CHARS || rc == VWRN_TRAILING_SPACES) 645 rc = -rc; 646 else 647 { 648 while (*psz == ' ' || *psz == '\t') 649 psz++; 650 rc = *psz ? VERR_TRAILING_CHARS : VERR_TRAILING_SPACES; 651 } 652 } 425 653 return rc; 426 654 } … … 450 678 * @returns iprt status code. 451 679 * Warnings are used to indicate convertion problems. 680 * @retval VWRN_NUMBER_TOO_BIG 681 * @retval VWRN_TRAILING_CHARS 682 * @retval VWRN_TRAILING_SPACES 683 * @retval VINF_SUCCESS 684 * @retval VERR_NO_DIGITS 685 * 452 686 * @param pszValue Pointer to the string value. 453 687 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 460 694 int64_t i64; 461 695 int rc = RTStrToInt64Ex(pszValue, ppszNext, uBase, &i64); 696 if (rc == VINF_SUCCESS) 697 { 698 int32_t i32 = (int32_t)i64; 699 if (i64 != (int64_t)i32) 700 rc = VWRN_NUMBER_TOO_BIG; 701 } 702 if (pi32) 703 *pi32 = (int32_t)i64; 704 return rc; 705 } 706 707 708 /** 709 * Converts a string representation of a number to a 32-bit signed number, 710 * making sure the full string is converted. 711 * 712 * @returns iprt status code. 713 * Warnings are used to indicate convertion problems. 714 * @retval VWRN_NUMBER_TOO_BIG 715 * @retval VINF_SUCCESS 716 * @retval VERR_TRAILING_CHARS 717 * @retval VERR_TRAILING_SPACES 718 * @retval VERR_NO_DIGITS 719 * 720 * @param pszValue Pointer to the string value. 721 * @param uBase The base of the representation used. 722 * If the function will look for known prefixes before defaulting to 10. 723 * @param pi32 Where to store the converted number. (optional) 724 */ 725 RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32) 726 { 727 int64_t i64; 728 int rc = RTStrToInt64Full(pszValue, uBase, &i64); 462 729 if (rc == VINF_SUCCESS) 463 730 { … … 495 762 * @returns iprt status code. 496 763 * Warnings are used to indicate convertion problems. 764 * @retval VWRN_NUMBER_TOO_BIG 765 * @retval VWRN_TRAILING_CHARS 766 * @retval VWRN_TRAILING_SPACES 767 * @retval VINF_SUCCESS 768 * @retval VERR_NO_DIGITS 769 * 497 770 * @param pszValue Pointer to the string value. 498 771 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 505 778 int64_t i64; 506 779 int rc = RTStrToInt64Ex(pszValue, ppszNext, uBase, &i64); 780 if (rc == VINF_SUCCESS) 781 { 782 int16_t i16 = (int16_t)i64; 783 if (i64 != (int64_t)i16) 784 rc = VWRN_NUMBER_TOO_BIG; 785 } 786 if (pi16) 787 *pi16 = (int16_t)i64; 788 return rc; 789 } 790 791 792 /** 793 * Converts a string representation of a number to a 16-bit signed number, 794 * making sure the full string is converted. 795 * 796 * @returns iprt status code. 797 * Warnings are used to indicate convertion problems. 798 * @retval VWRN_NUMBER_TOO_BIG 799 * @retval VINF_SUCCESS 800 * @retval VERR_TRAILING_CHARS 801 * @retval VERR_TRAILING_SPACES 802 * @retval VERR_NO_DIGITS 803 * 804 * @param pszValue Pointer to the string value. 805 * @param uBase The base of the representation used. 806 * If the function will look for known prefixes before defaulting to 10. 807 * @param pi16 Where to store the converted number. (optional) 808 */ 809 RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16) 810 { 811 int64_t i64; 812 int rc = RTStrToInt64Full(pszValue, uBase, &i64); 507 813 if (rc == VINF_SUCCESS) 508 814 { … … 540 846 * @returns iprt status code. 541 847 * Warnings are used to indicate convertion problems. 848 * @retval VWRN_NUMBER_TOO_BIG 849 * @retval VWRN_TRAILING_CHARS 850 * @retval VWRN_TRAILING_SPACES 851 * @retval VINF_SUCCESS 852 * @retval VERR_NO_DIGITS 853 * 542 854 * @param pszValue Pointer to the string value. 543 855 * @param ppszNext Where to store the pointer to the first char following the number. (Optional) … … 550 862 int64_t i64; 551 863 int rc = RTStrToInt64Ex(pszValue, ppszNext, uBase, &i64); 864 if (rc == VINF_SUCCESS) 865 { 866 int8_t i8 = (int8_t)i64; 867 if (i64 != (int64_t)i8) 868 rc = VWRN_NUMBER_TOO_BIG; 869 } 870 if (pi8) 871 *pi8 = (int8_t)i64; 872 return rc; 873 } 874 875 876 /** 877 * Converts a string representation of a number to a 8-bit signed number, 878 * making sure the full string is converted. 879 * 880 * @returns iprt status code. 881 * Warnings are used to indicate convertion problems. 882 * @retval VWRN_NUMBER_TOO_BIG 883 * @retval VINF_SUCCESS 884 * @retval VERR_TRAILING_CHARS 885 * @retval VERR_TRAILING_SPACES 886 * @retval VERR_NO_DIGITS 887 * 888 * @param pszValue Pointer to the string value. 889 * @param uBase The base of the representation used. 890 * If the function will look for known prefixes before defaulting to 10. 891 * @param pi64 Where to store the converted number. (optional) 892 */ 893 RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8) 894 { 895 int64_t i64; 896 int rc = RTStrToInt64Full(pszValue, uBase, &i64); 552 897 if (rc == VINF_SUCCESS) 553 898 {
Note:
See TracChangeset
for help on using the changeset viewer.