Changeset 23 for trunk/src/helpers/xstring.c
- Timestamp:
- Jan 16, 2001, 8:49:10 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/xstring.c
r21 r23 370 370 * If pxstr contains something, its contents are destroyed. 371 371 * 372 * With ulSourceLength, specify the length of pcszSource. 373 * If you specify 0, this function will run strlen(pcszSource) 374 * itself. If you already know the length of pcszSource (or 375 * only want to copy a substring), you can speed this function 376 * up a bit this way. 377 * 372 378 * Returns the length of the new string (excluding the null 373 379 * terminator), or null upon errors. … … 377 383 + XSTRING str; 378 384 + xstrInit(&str, 0); 379 + xstrcpy(&str, "blah" );385 + xstrcpy(&str, "blah", 0); 380 386 * 381 387 * This sequence can be abbreviated using xstrInitCopy. … … 383 389 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy 384 390 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 391 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength 385 392 */ 386 393 387 394 ULONG xstrcpy(PXSTRING pxstr, // in/out: string 388 const char *pcszSource) // in: source, can be NULL 389 { 390 xstrClear(pxstr); 395 const char *pcszSource, // in: source, can be NULL 396 ULONG ulSourceLength) // in: length of pcszSource or 0 397 { 398 // xstrClear(pxstr); NOOOO! this frees the string, we want to keep the memory 391 399 392 400 if (pxstr) 393 401 { 394 ULONG ulSourceLength = 0;395 402 if (pcszSource) 396 ulSourceLength = strlen(pcszSource);397 398 if (ulSourceLength)399 403 { 400 // we do have a source string: 401 ULONG cbNeeded = ulSourceLength + 1; 402 if (cbNeeded > pxstr->cbAllocated) 404 if (ulSourceLength == 0) 405 ulSourceLength = strlen(pcszSource); 406 407 if (ulSourceLength) 403 408 { 404 // we need more memory than we have previously 405 // allocated: 406 pxstr->cbAllocated = cbNeeded; 407 pxstr->psz = (PSZ)malloc(cbNeeded); 409 // we do have a source string: 410 ULONG cbNeeded = ulSourceLength + 1; 411 if (cbNeeded > pxstr->cbAllocated) 412 { 413 // we need more memory than we have previously 414 // allocated: 415 pxstr->cbAllocated = cbNeeded; 416 pxstr->psz = (PSZ)malloc(cbNeeded); 417 } 418 // else: we have enough memory 419 420 // strcpy(pxstr->psz, pcszSource); 421 memcpy(pxstr->psz, 422 pcszSource, 423 ulSourceLength); 424 *(pxstr->psz + ulSourceLength) = 0; 408 425 } 409 // else: we have enough memory 410 411 strcpy(pxstr->psz, pcszSource); 426 else 427 { 428 // no source specified or source is empty: 429 if (pxstr->cbAllocated) 430 // we did have a string: set to empty, 431 // but leave allocated memory intact 432 *(pxstr->psz) = 0; 433 // else: pxstr->psz is still NULL 434 } 435 436 // in all cases, set new length 437 pxstr->ulLength = ulSourceLength; 412 438 } 413 else414 {415 // no source specified or source is empty:416 if (pxstr->cbAllocated)417 // we did have a string: set to empty,418 // but leave allocated memory intact419 *(pxstr->psz) = 0;420 // else: pxstr->psz is still NULL421 }422 423 // in all cases, set new length424 pxstr->ulLength = ulSourceLength;425 439 } 426 440 … … 435 449 * If pxstr is empty, this behaves just like xstrcpy. 436 450 * 451 * With ulSourceLength, specify the length of pcszSource. 452 * If you specify 0, this function will run strlen(pcszSource) 453 * itself. If you already know the length of pcszSource (or 454 * only want to copy a substring), you can speed this function 455 * up a bit this way. 456 * 437 457 * Returns the length of the new string (excluding the null 438 458 * terminator) if the string was changed, or 0 if nothing … … 446 466 + XSTRING str; 447 467 + xstrInit(&str, 0); 448 + xstrcpy(&str, "blah" );449 + xstrcat(&str, "blup" );468 + xstrcpy(&str, "blah", 0); 469 + xstrcat(&str, "blup", 0); 450 470 * 451 471 * After this, str.psz points to a new string containing … … 458 478 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 459 479 *@@changed V0.9.7 (2000-12-10) [umoeller]: return value was wrong 480 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength 460 481 */ 461 482 462 483 ULONG xstrcat(PXSTRING pxstr, // in/out: string 463 const char *pcszSource) // in: source, can be NULL 484 const char *pcszSource, // in: source, can be NULL 485 ULONG ulSourceLength) // in: length of pcszSource or 0 464 486 { 465 487 ULONG ulrc = 0; … … 467 489 if (pxstr) 468 490 { 469 ULONG ulSourceLength = 0;470 491 if (pcszSource) 471 ulSourceLength = strlen(pcszSource);472 473 if (ulSourceLength)474 492 { 475 // we do have a source string: 476 477 // 1) memory management 478 ULONG cbNeeded = pxstr->ulLength + ulSourceLength + 1; 479 if (cbNeeded > pxstr->cbAllocated) 493 if (ulSourceLength == 0) 494 ulSourceLength = strlen(pcszSource); 495 496 if (ulSourceLength) 480 497 { 481 // we need more memory than we have previously 482 // allocated: 483 if (pxstr->cbAllocated) 484 // appendee already had memory: 485 // reallocate 486 pxstr->psz = (PSZ)realloc(pxstr->psz, 487 cbNeeded); 488 else 489 // appendee has no memory: 490 pxstr->psz = (PSZ)malloc(cbNeeded); 491 492 pxstr->cbAllocated = cbNeeded; 493 // ulLength is unchanged yet 494 } 495 // else: we have enough memory, both if appendee 496 // is empty or not empty 497 498 // now we have: 499 // -- if appendee (pxstr) had enough memory, no problem 500 // -- if appendee (pxstr) needed more memory 501 // -- and was not empty: pxstr->psz now points to a 502 // reallocated copy of the old string 503 // -- and was empty: pxstr->psz now points to a 504 // new (unitialized) buffer 505 506 // 2) append source string: 507 memcpy(pxstr->psz + pxstr->ulLength, 508 pcszSource, 509 ulSourceLength + 1); // null terminator 510 511 // in all cases, set new length 512 pxstr->ulLength += ulSourceLength; 513 ulrc = pxstr->ulLength; // V0.9.7 (2000-12-10) [umoeller] 514 515 } // end if (ulSourceLength) 498 // we do have a source string: 499 500 // 1) memory management 501 ULONG cbNeeded = pxstr->ulLength + ulSourceLength + 1; 502 if (cbNeeded > pxstr->cbAllocated) 503 { 504 // we need more memory than we have previously 505 // allocated: 506 if (pxstr->cbAllocated) 507 // appendee already had memory: 508 // reallocate 509 pxstr->psz = (PSZ)realloc(pxstr->psz, 510 cbNeeded); 511 else 512 // appendee has no memory: 513 pxstr->psz = (PSZ)malloc(cbNeeded); 514 515 pxstr->cbAllocated = cbNeeded; 516 // ulLength is unchanged yet 517 } 518 // else: we have enough memory, both if appendee 519 // is empty or not empty 520 521 // now we have: 522 // -- if appendee (pxstr) had enough memory, no problem 523 // -- if appendee (pxstr) needed more memory 524 // -- and was not empty: pxstr->psz now points to a 525 // reallocated copy of the old string 526 // -- and was empty: pxstr->psz now points to a 527 // new (unitialized) buffer 528 529 // 2) append source string: 530 memcpy(pxstr->psz + pxstr->ulLength, 531 pcszSource, 532 ulSourceLength + 1); // null terminator 533 534 // in all cases, set new length 535 pxstr->ulLength += ulSourceLength; 536 ulrc = pxstr->ulLength; // V0.9.7 (2000-12-10) [umoeller] 537 538 } // end if (ulSourceLength) 539 } 540 516 541 // else no source specified or source is empty: 517 542 // do nothing … … 601 626 602 627 /* 628 *@@ xstrrpl: 629 * replaces cSearchLen characters in pxstr, starting 630 * at the position ulStart, with the string 631 * in pxstrReplaceWith. 632 * 633 * Returns the new length of the string, excluding 634 * the null terminator, or 0 if the replacement failed 635 * (e.g. because the offsets were too large). 636 * 637 * This has been extracted from xstrFindReplace because 638 * if you already know the position of a substring, 639 * you can now call this directly. This properly 640 * reallocates the string if more memory is needed. 641 * 642 * Example: 643 * 644 + XSTRING xstr, xstrReplacement; 645 + xstrInitCopy(&xstr, "This is a test string."); 646 + // positions: 0123456789012345678901 647 + // 1 2 648 + 649 + xstrInitCopy(&xstrReplacement, "stupid"); 650 + 651 + xstrrpl(&xstr, 652 + 10, // position of "test" 653 + 4, // length of "test" 654 + &xstrReplacement); 655 * 656 * This would yield "This is a stupid string." 657 * 658 *@@added V0.9.7 (2001-01-15) [umoeller] 659 */ 660 661 ULONG xstrrpl(PXSTRING pxstr, // in/out: string 662 ULONG ulFirstReplOfs, // in: ofs of first char to replace 663 ULONG cReplLen, // in: no. of chars to replace 664 const XSTRING *pstrReplaceWith) // in: string to replace chars with 665 { 666 ULONG ulrc = 0; 667 668 // security checks... 669 if ( (ulFirstReplOfs + cReplLen <= pxstr->ulLength) 670 && (pstrReplaceWith) 671 // && (pstrReplaceWith->ulLength) no, this can be empty 672 ) 673 { 674 ULONG cReplaceLen = pstrReplaceWith->ulLength; 675 // can be 0! 676 677 // length of new string 678 ULONG cbNeeded = pxstr->ulLength 679 + cReplaceLen 680 - cReplLen 681 + 1; // null terminator 682 // offset where pszSearch was found 683 // ulFirstReplOfs = pFound - pxstr->psz; now ulFirstReplOfs 684 PSZ pFound = pxstr->psz + ulFirstReplOfs; 685 686 // now check if we have enough memory... 687 if (pxstr->cbAllocated < cbNeeded) 688 { 689 // no, we need more memory: 690 // allocate new buffer 691 PSZ pszNew = (PSZ)malloc(cbNeeded); 692 693 if (ulFirstReplOfs) 694 // "found" was not at the beginning: 695 // copy from beginning up to found-offset 696 memcpy(pszNew, 697 pxstr->psz, 698 ulFirstReplOfs); // up to "found" 699 700 if (cReplaceLen) 701 { 702 // we have a replacement: 703 // insert it next 704 memcpy(pszNew + ulFirstReplOfs, 705 pstrReplaceWith->psz, 706 cReplaceLen + 1); // include null terminator 707 } 708 709 // copy rest: 710 // pxstr frontFOUNDtail 711 // 0 1 712 // 01234567890123 713 // ³ ³ ³ ³ 714 // ³ ³ ÀÄ ulFirstReplOfs + cReplLen = 10 715 // ³ ³ ³ 716 // ³ ÀÄ ulFirstReplOfs = 5 717 // ³ ³ 718 // pxstr->ulLength = 14 719 memcpy(pszNew + ulFirstReplOfs + cReplaceLen, 720 pFound + cReplLen, 721 // remaining bytes: 722 pxstr->ulLength - ulFirstReplOfs - cReplLen // 9 723 + 1); // null terminator 724 725 // replace old buffer with new one 726 free(pxstr->psz); 727 pxstr->psz = pszNew; 728 pxstr->ulLength = cbNeeded - 1; 729 pxstr->cbAllocated = cbNeeded; 730 } // end if (pxstr->cbAllocated < cbNeeded) 731 else 732 { 733 // we have enough memory left, 734 // we can just overwrite in the middle... 735 736 PSZ pszAfterFoundBackup = 0; 737 // calc length of string after "found" 738 ULONG cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen; 739 740 // if "replace" is longer than "found", 741 // make a backup of the stuff after "found", 742 // or this would get overwritten 743 if (cReplaceLen > cReplLen) 744 { 745 pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1); 746 memcpy(pszAfterFoundBackup, 747 pFound + cReplLen, 748 cTailLength + 1); 749 } 750 751 // now overwrite "found" in the middle 752 if (cReplaceLen) 753 { 754 memcpy(pxstr->psz + ulFirstReplOfs, 755 pstrReplaceWith->psz, 756 cReplaceLen); // no null terminator 757 } 758 759 // now append tail (stuff after "found") again... 760 if (pszAfterFoundBackup) 761 { 762 // we made a backup above: 763 memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen, 764 pszAfterFoundBackup, 765 cTailLength + 1); 766 free(pszAfterFoundBackup); 767 // done! 768 } 769 else 770 // no backup: 771 if (cReplaceLen < cReplLen) 772 // "replace" is shorter than "found: 773 memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen, 774 pFound + cReplLen, 775 cTailLength + 1); 776 // else (cReplaceLen == cReplLen): 777 // we can leave the tail as it is 778 779 pxstr->ulLength = cbNeeded - 1; 780 } 781 782 ulrc = cbNeeded - 1; 783 } // end checks 784 785 return (ulrc); 786 } 787 788 /* 603 789 *@@ xstrFindWord: 604 790 * searches for pstrFind in pxstr, starting at ulOfs. … … 668 854 669 855 /* 670 *@@ xstr rpl:856 *@@ xstrFindReplace: 671 857 * replaces the first occurence of pstrSearch with 672 858 * pstrReplace in pxstr. … … 688 874 * (*pulOffset == 0), this starts from the beginning 689 875 * of pxstr. 876 * 690 877 * If the string was found, *pulOffset will be set to the 691 878 * first character after the new replacement string. This … … 697 884 * speed advantage): 698 885 * 699 * -- strh rploperates on C strings only;700 * 701 * -- xstr crpluses C strings for the search and replace886 * -- strhFindReplace operates on C strings only; 887 * 888 * -- xstrFindReplaceC uses C strings for the search and replace 702 889 * parameters. 703 890 * 704 891 * <B>Example usage:</B> 705 892 * 706 + XSTRING str; 707 + ULONG ulOffset = 0; 708 + xstrInit(&str, 0); 709 + xstrcpy(&str, "Test phrase 1. Test phrase 2."); 710 + while (xstrrpl(&str, 711 + &ulPos, // in/out: offset 712 + "Test", // search 713 + "Dummy") // replace 893 + XSTRING strBuf, 894 + strFind, 895 + strRepl; 896 + size_t ShiftTable[256]; 897 + BOOL fRepeat = FALSE; 898 + ULONG ulOffset = 0; 899 + 900 + xstrInitCopy(&strBuf, "Test phrase 1. Test phrase 2.", 0); 901 + xstrInitSet(&strFind, "Test"); 902 + xstrInitSet(&strRepl, "Dummy"); 903 + while (xstrFindReplace(&str, 904 + &ulPos, // in/out: offset 905 + &strFind, // search 906 + &strRepl, // replace 907 + ShiftTable, 908 + &fRepeat)) 714 909 + ; 715 910 * … … 722 917 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 723 918 *@@changed V0.9.6 (2000-11-12) [umoeller]: now using strhmemfind 724 */ 725 726 ULONG xstrrpl(PXSTRING pxstr, // in/out: string 727 PULONG pulOfs, // in: where to begin search (0 = start); 728 // out: ofs of first char after replacement string 729 const XSTRING *pstrSearch, // in: search string; cannot be NULL 730 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL 731 size_t *pShiftTable, // in: shift table (see strhmemfind) 732 PBOOL pfRepeatFind) // in: repeat find? (see strhmemfind) 919 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrrpl; extracted new xstrrpl 920 */ 921 922 ULONG xstrFindReplace(PXSTRING pxstr, // in/out: string 923 PULONG pulOfs, // in: where to begin search (0 = start); 924 // out: ofs of first char after replacement string 925 const XSTRING *pstrSearch, // in: search string; cannot be NULL 926 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL 927 size_t *pShiftTable, // in: shift table (see strhmemfind) 928 PBOOL pfRepeatFind) // in: repeat find? (see strhmemfind) 733 929 { 734 930 ULONG ulrc = 0; // default: not found … … 744 940 { 745 941 // yes: 746 /* PSZ pFound = strstr(pxstr->psz + *pulOfs,747 pstrSearch->psz); */748 PSZ pFound = (PSZ)strhmemfind(pxstr->psz + *pulOfs, // in: haystack749 pxstr->ulLength - *pulOfs,750 pstrSearch->psz,751 cSearchLen,752 pShiftTable,753 pfRepeatFind);942 ULONG ulOfs = *pulOfs; 943 const char *pFound 944 = (const char *)strhmemfind(pxstr->psz + ulOfs, // in: haystack 945 pxstr->ulLength - ulOfs, 946 pstrSearch->psz, 947 cSearchLen, 948 pShiftTable, 949 pfRepeatFind); 754 950 if (pFound) 755 951 { 952 ULONG ulFirstReplOfs = pFound - pxstr->psz; 756 953 // found in buffer from ofs: 757 ULONG cReplaceLen = pstrReplace->ulLength; 758 // can be 0! 759 760 // length of new string 761 ULONG cbNeeded = pxstr->ulLength 762 + cReplaceLen 763 - cSearchLen 764 + 1, // null terminator 765 // offset where pszSearch was found 766 ulFoundOfs = pFound - pxstr->psz; 767 768 // now check if we have enough memory... 769 if (pxstr->cbAllocated < cbNeeded) 770 { 771 // no, we need more memory: 772 // allocate new buffer 773 PSZ pszNew = (PSZ)malloc(cbNeeded); 774 775 if (ulFoundOfs) 776 // "found" was not at the beginning: 777 // copy from beginning up to found-offset 778 memcpy(pszNew, 779 pxstr->psz, 780 ulFoundOfs); // up to "found" 781 782 if (cReplaceLen) 783 { 784 // we have a replacement: 785 // insert it next 786 memcpy(pszNew + ulFoundOfs, 787 pstrReplace->psz, 788 cReplaceLen + 1); // include null terminator 789 } 790 791 // copy rest: 792 // pxstr frontFOUNDtail 793 // 0 1 794 // 01234567890123 795 // ³ ³ ³ ³ 796 // ³ ³ ÀÄ ulFoundOfs + cSearchLen = 10 797 // ³ ³ ³ 798 // ³ ÀÄ ulFoundOfs = 5 799 // ³ ³ 800 // pxstr->ulLength = 14 801 memcpy(pszNew + ulFoundOfs + cReplaceLen, 802 pFound + cSearchLen, 803 // remaining bytes: 804 pxstr->ulLength - ulFoundOfs - cSearchLen // 9 805 + 1); // null terminator 806 807 // replace old buffer with new one 808 free(pxstr->psz); 809 pxstr->psz = pszNew; 810 pxstr->ulLength = cbNeeded - 1; 811 pxstr->cbAllocated = cbNeeded; 812 } // end if (pxstr->cbAllocated < cbNeeded) 813 else 814 { 815 // we have enough memory left, 816 // we can just overwrite in the middle... 817 818 PSZ pszAfterFoundBackup = 0; 819 // calc length of string after "found" 820 ULONG cTailLength = pxstr->ulLength - ulFoundOfs - cSearchLen; 821 822 // if "replace" is longer than "found", 823 // make a backup of the stuff after "found", 824 // or this would get overwritten 825 if (cReplaceLen > cSearchLen) 826 { 827 pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1); 828 memcpy(pszAfterFoundBackup, 829 pFound + cSearchLen, 830 cTailLength + 1); 831 } 832 833 // now overwrite "found" in the middle 834 if (cReplaceLen) 835 { 836 memcpy(pxstr->psz + ulFoundOfs, 837 pstrReplace->psz, 838 cReplaceLen); // no null terminator 839 } 840 841 // now append tail (stuff after "found") again... 842 if (pszAfterFoundBackup) 843 { 844 // we made a backup above: 845 memcpy(pxstr->psz + ulFoundOfs + cReplaceLen, 846 pszAfterFoundBackup, 847 cTailLength + 1); 848 free(pszAfterFoundBackup); 849 // done! 850 } 851 else 852 // no backup: 853 if (cReplaceLen < cSearchLen) 854 // "replace" is shorter than "found: 855 memcpy(pxstr->psz + ulFoundOfs + cReplaceLen, 856 pFound + cSearchLen, 857 cTailLength + 1); 858 // else (cReplaceLen == cSearchLen): 859 // we can leave the tail as it is 860 861 pxstr->ulLength = cbNeeded - 1; 862 } 954 // replace pFound with pstrReplace 955 ulrc = xstrrpl(pxstr, 956 ulFirstReplOfs, // where to start 957 cSearchLen, // chars to replace 958 pstrReplace); 863 959 864 960 // return new length 865 ulrc = cbNeeded - 1; 866 *pulOfs = ulFoundOfs + cReplaceLen; 961 *pulOfs = ulFirstReplOfs + pstrReplace->ulLength; 867 962 } // end if (pFound) 868 963 } // end if ( (*pulOfs < pxstr->ulLength) ... … … 873 968 874 969 /* 875 *@@ xstr crpl:876 * wrapper around xstr rpl() which allows using C strings877 * for the find and replace parameters.970 *@@ xstrFindReplaceC: 971 * wrapper around xstrFindReplace() which allows using 972 * C strings for the find and replace parameters. 878 973 * 879 974 * This creates two temporary XSTRING's for pcszSearch 880 * pcszReplace. As a result, this is slower than xstrrpl. 975 * and pcszReplace and thus cannot use the shift table 976 * for repetitive searches. As a result, this is slower 977 * than xstrFindReplace. 978 * 881 979 * If you search with the same strings several times, 882 * you'll be better off using xstr rpl() directly.980 * you'll be better off using xstrFindReplace() directly. 883 981 * 884 982 *@@added V0.9.6 (2000-11-01) [umoeller] 885 * /886 887 ULONG xstrcrpl(PXSTRING pxstr, // in/out: string 888 PULONG pulOfs, // in: where to begin search (0 = start); 889 // out: ofs of first char after replacement string890 const char *pcszSearch, // in: search string; cannot be NULL891 const char *pcszReplace) // in: replacementstring; cannot be NULL892 { 893 // ULONG ulrc = 0; 983 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrcrpl 984 */ 985 986 ULONG xstrFindReplaceC(PXSTRING pxstr, // in/out: string 987 PULONG pulOfs, // in: where to begin search (0 = start); 988 // out: ofs of first char after replacement string 989 const char *pcszSearch, // in: search string; cannot be NULL 990 const char *pcszReplace) // in: replacement string; cannot be NULL 991 { 894 992 XSTRING xstrFind, 895 993 xstrReplace; … … 902 1000 xstrInitSet(&xstrReplace, (PSZ)pcszReplace); 903 1001 904 return (xstrrpl(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat)); 1002 return (xstrFindReplace(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat)); 1003 } 1004 1005 /* 1006 *@@ xstrConvertLineFormat: 1007 * converts between line formats. 1008 * 1009 * If (fToCFormat == TRUE), all \r\n pairs are replaced 1010 * with \n chars (UNIX or C format). 1011 * 1012 * Reversely, if (fToCFormat == FALSE), all \n chars 1013 * are converted to \r\n pairs (DOS and OS/2 formats). 1014 * No check is made whether this has already been done. 1015 * 1016 *@@added V0.9.7 (2001-01-15) [umoeller] 1017 */ 1018 1019 VOID xstrConvertLineFormat(PXSTRING pxstr, 1020 BOOL fToCFormat) // in: if TRUE, to C format; if FALSE, to OS/2 format. 1021 { 1022 XSTRING strFind, 1023 strRepl; 1024 size_t ShiftTable[256]; 1025 BOOL fRepeat = FALSE; 1026 ULONG ulOfs = 0; 1027 1028 if (fToCFormat) 1029 { 1030 // OS/2 to C: 1031 xstrInitSet(&strFind, "\r\n"); 1032 xstrInitSet(&strRepl, "\n"); 1033 } 1034 else 1035 { 1036 // C to OS/2: 1037 xstrInitSet(&strFind, "\n"); 1038 xstrInitSet(&strRepl, "\r\n"); 1039 } 1040 1041 while (xstrFindReplace(pxstr, 1042 &ulOfs, 1043 &strFind, 1044 &strRepl, 1045 ShiftTable, 1046 &fRepeat)) 1047 ; 905 1048 } 906 1049 … … 912 1055 strFind, 913 1056 strReplace; 1057 size_t shift[256]; 1058 BOOL fRepeat = FALSE; 914 1059 ULONG ulOfs = 0; 915 1060 … … 918 1063 xstrInit(&strReplace, 0); 919 1064 920 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !" );921 xstrcpy(&strFind, "Test" );922 xstrcpy(&strReplace, "Dummy" );1065 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0); 1066 xstrcpy(&strFind, "Test", 0); 1067 xstrcpy(&strReplace, "Dummy", 0); 923 1068 924 1069 printf("Old string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 925 1070 926 while (xstrrpl(&str, 927 ulOfs, 1071 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1072 1073 fRepeat = FALSE; 1074 ulOfs = 0; 1075 while (xstrFindReplace(&str, 1076 &ulOfs, 1077 &strFind, 1078 &strReplace, 1079 shift, &fRepeat)); 1080 ; 1081 1082 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1083 1084 xstrcpy(&strFind, strReplace.psz, 0); 1085 xstrClear(&strReplace); 1086 1087 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1088 1089 fRepeat = FALSE; 1090 ulOfs = 0; 1091 while (xstrFindReplace(&str, 1092 &ulOfs, 928 1093 &strFind, 929 1094 &strReplace, 930 &ulOfs))1095 shift, &fRepeat)); 931 1096 ; 932 1097 933 1098 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 934 1099 935 xstrcpy(&strFind, strReplace.psz); 936 xstrClear(&strReplace); 1100 xstrcpy(&strFind, " ", 0); 1101 xstrcpy(&strReplace, ".", 0); 1102 1103 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1104 1105 fRepeat = FALSE; 937 1106 ulOfs = 0; 938 while (xstr rpl(&str,939 ulOfs,1107 while (xstrFindReplace(&str, 1108 &ulOfs, 940 1109 &strFind, 941 1110 &strReplace, 942 &ulOfs))1111 shift, &fRepeat)); 943 1112 ; 944 1113 945 1114 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 946 1115 947 xstrcpy(&strFind, " "); 948 xstrcpy(&strReplace, "."); 1116 xstrcpy(&strFind, ".", 0); 1117 xstrcpy(&strReplace, "*.........................*", 0); 1118 1119 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1120 1121 fRepeat = FALSE; 949 1122 ulOfs = 0; 950 while (xstr rpl(&str,951 ulOfs,1123 while (xstrFindReplace(&str, 1124 &ulOfs, 952 1125 &strFind, 953 1126 &strReplace, 954 &ulOfs))1127 shift, &fRepeat)); 955 1128 ; 956 1129 957 1130 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 958 1131 959 xstrcpy(&strFind, "."); 960 xstrcpy(&strReplace, "***************************"); 1132 printf("Reserving extra mem.\n"); 1133 1134 xstrReserve(&str, 6000); 1135 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1136 1137 xstrcpy(&strFind, "..........", 0); 1138 xstrcpy(&strReplace, "@", 0); 1139 1140 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1141 1142 fRepeat = FALSE; 961 1143 ulOfs = 0; 962 while (xstr rpl(&str,963 ulOfs,1144 while (xstrFindReplace(&str, 1145 &ulOfs, 964 1146 &strFind, 965 1147 &strReplace, 966 &ulOfs))1148 shift, &fRepeat)); 967 1149 ; 968 1150 969 1151 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 970 1152 971 xstrcpy(&strFind, "*"); 972 xstrClear(&strReplace); 973 ulOfs = 0; 974 while (xstrrpl(&str, 975 ulOfs, 976 &strFind, 977 &strReplace, 978 &ulOfs)) 979 ; 980 981 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 982 } */ 983 1153 return (0); 1154 } 1155 */ 1156 1157
Note:
See TracChangeset
for help on using the changeset viewer.