Overview
Comment: | Add -[OFStream tryReadStringWithEncoding:] |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
6e91e313a585f5f54375ece565508554 |
User & Date: | js on 2024-10-19 11:09:55 |
Other Links: | manifest | tags |
Context
2024-10-19
| ||
11:38 | Add -[OFStream asyncReadString] check-in: 95e2910172 user: js tags: trunk | |
11:09 | Add -[OFStream tryReadStringWithEncoding:] check-in: 6e91e313a5 user: js tags: trunk | |
2024-10-10
| ||
00:21 | Better skip UNIX sequenced packet socket tests check-in: e267c68e6f user: js tags: trunk | |
Changes
Modified src/OFStream.h from [47b9cf3e36] to [cca724d404].
︙ | |||
672 673 674 675 676 677 678 679 680 681 682 683 684 685 | 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | + + + + + + + + + + + + + + + + + + + + + + + | * @return OFData with an item size of 1 with all the data of the stream until * the end of the stream is reached. * @throw OFReadFailedException Reading failed * @throw OFNotOpenException The stream is not open */ - (OFData *)readDataUntilEndOfStream; /** * @brief Reads a string until a `\0` appears in the stream or the end of the * stream is reached. * * @throw OFReadFailedException Reading failed * @throw OFInvalidEncodingException The string read from the stream has * invalid encoding * @throw OFNotOpenException The stream is not open */ - (OFString *)readString; /** * @brief Reads a string with the specified encoding until a `\0` appears in * the stream or the end of the stream is reached. * * @param encoding The encoding of the string to read from the stream * @throw OFReadFailedException Reading failed * @throw OFInvalidEncodingException The string read from the stream has * invalid encoding * @throw OFNotOpenException The stream is not open */ - (OFString *)readStringWithEncoding: (OFStringEncoding)encoding; /** * @brief Reads a string with the specified length from the stream. * * If `\0` appears in the stream, the string will be truncated at the `\0` and * the rest of the bytes of the string will be lost. This way, reading from the * stream will not break because of a `\0` because the specified number of * bytes is still being read and only the string gets truncated. |
︙ | |||
832 833 834 835 836 837 838 839 840 841 842 843 844 845 | 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | + + + + + + + + + + + + + + + + + + + + + + + | */ - (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode block: (OFStreamAsyncReadLineBlock)block; # endif #endif /** * @brief Tries to read a string until a `\0` appears in the stream or the end * of the stream is reached. * * @throw OFReadFailedException Reading failed * @throw OFInvalidEncodingException The string read from the stream has * invalid encoding * @throw OFNotOpenException The stream is not open */ - (OFString *)tryReadString; /** * @brief Tries to read a string with the specified encoding until a `\0` * appears in the stream or the end of the stream is reached. * * @param encoding The encoding of the string to read from the stream * @throw OFReadFailedException Reading failed * @throw OFInvalidEncodingException The string read from the stream has * invalid encoding * @throw OFNotOpenException The stream is not open */ - (OFString *)tryReadStringWithEncoding: (OFStringEncoding)encoding; /** * @brief Tries to read a line from the stream (see @ref readLine) and returns * `nil` if no complete line has been received yet. * * @return The line that was read, autoreleased, or `nil` if the line is not * complete yet * @throw OFReadFailedException Reading failed |
︙ |
Modified src/OFStream.m from [f7de880ff7] to [9664024d76].
︙ | |||
423 424 425 426 427 428 429 430 431 432 433 434 435 436 | 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | + + + + + + + + + + + + + + + + | } @finally { OFFreeMemory(buffer); } [data makeImmutable]; return data; } - (OFString *)readString { return [self readStringWithEncoding: OFStringEncodingUTF8]; } - (OFString *)readStringWithEncoding: (OFStringEncoding)encoding { OFString *string = nil; while ((string = [self tryReadStringWithEncoding: encoding]) == nil) if (self.atEndOfStream) return nil; return string; } - (OFString *)readStringWithLength: (size_t)length { return [self readStringWithLength: length encoding: OFStringEncodingUTF8]; } |
︙ | |||
670 671 672 673 674 675 676 677 678 679 680 681 682 683 | 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | encoding: encoding mode: runLoopMode block: block delegate: nil]; } # endif #endif - (OFString *)tryReadString { return [self tryReadStringWithEncoding: OFStringEncodingUTF8]; } - (OFString *)tryReadStringWithEncoding: (OFStringEncoding)encoding { size_t pageSize, bufferLength; char *buffer, *readBuffer; OFString *ret; /* Look if there's something in our buffer */ if (!_waitingForDelimiter && _readBuffer != NULL) { for (size_t i = 0; i < _readBufferLength; i++) { if (_readBuffer[i] == '\0') { ret = [OFString stringWithCString: _readBuffer encoding: encoding length: i]; _readBuffer += i + 1; _readBufferLength -= i + 1; _waitingForDelimiter = false; return ret; } } } /* Read and see if we got a \0 */ pageSize = [OFSystemInfo pageSize]; buffer = OFAllocMemory(1, pageSize); @try { if ([self lowlevelIsAtEndOfStream]) { if (_readBuffer == NULL) { _waitingForDelimiter = false; return nil; } ret = [OFString stringWithCString: _readBuffer encoding: encoding length: _readBufferLength]; OFFreeMemory(_readBufferMemory); _readBuffer = _readBufferMemory = NULL; _readBufferLength = 0; _waitingForDelimiter = false; return ret; } bufferLength = [self lowlevelReadIntoBuffer: buffer length: pageSize]; /* Look if there's a \0 */ for (size_t i = 0; i < bufferLength; i++) { if (buffer[i] == '\0') { size_t retLength; char *retCString; retLength = _readBufferLength + i; retCString = OFAllocMemory(retLength, 1); memcpy(retCString, _readBuffer, _readBufferLength); memcpy(retCString + _readBufferLength, buffer, i); @try { ret = [OFString stringWithCString: retCString encoding: encoding length: retLength]; } @catch (id e) { if (bufferLength > 0) { /* * Append data to _readBuffer * to prevent loss of data. */ readBuffer = OFAllocMemory( _readBufferLength + bufferLength, 1); memcpy(readBuffer, _readBuffer, _readBufferLength); memcpy(readBuffer + _readBufferLength, buffer, bufferLength); OFFreeMemory(_readBufferMemory); _readBuffer = readBuffer; _readBufferMemory = readBuffer; _readBufferLength += bufferLength; } @throw e; } @finally { OFFreeMemory(retCString); } readBuffer = OFAllocMemory(bufferLength - i - 1, 1); if (readBuffer != NULL) memcpy(readBuffer, buffer + i + 1, bufferLength - i - 1); OFFreeMemory(_readBufferMemory); _readBuffer = _readBufferMemory = readBuffer; _readBufferLength = bufferLength - i - 1; _waitingForDelimiter = false; return ret; } } /* No \0 was found */ if (bufferLength > 0) { readBuffer = OFAllocMemory( _readBufferLength + bufferLength, 1); memcpy(readBuffer, _readBuffer, _readBufferLength); memcpy(readBuffer + _readBufferLength, buffer, bufferLength); OFFreeMemory(_readBufferMemory); _readBuffer = _readBufferMemory = readBuffer; _readBufferLength += bufferLength; } } @finally { OFFreeMemory(buffer); } _waitingForDelimiter = true; return nil; } - (OFString *)tryReadLine { return [self tryReadLineWithEncoding: OFStringEncodingUTF8]; } - (OFString *)tryReadUntilDelimiter: (OFString *)delimiter |
︙ | |||
836 837 838 839 840 841 842 | 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | - | } @finally { OFFreeMemory(buffer); } _waitingForDelimiter = true; return nil; } |
︙ |
Modified tests/OFStreamTests.m from [c1be2e2921] to [63b992a9ad].
︙ | |||
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | + + + + + + - + | OTAssertEqualObjects([stream readLine], @"foo"); string = [stream readLine]; OTAssertNotNil(string); OTAssertEqual(string.length, pageSize - 3); OTAssertEqual(strcmp(string.UTF8String, cString), 0); string = [stream readString]; OTAssertEqualObjects(string, @"aaa"); string = [stream readString]; OTAssertEqualObjects(string, @"b"); } @finally { OFFreeMemory(cString); } } @end @implementation OFTestStream - (bool)lowlevelIsAtEndOfStream { |
︙ | |||
80 81 82 83 84 85 86 87 88 89 90 91 | 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | return 0; memcpy(buffer, "oo\n", 3); memset((char *)buffer + 3, 'X', pageSize - 3); _state++; return pageSize; case 2: if (size < 1) return 0; memcpy(buffer, "", 1); _state++; return 1; case 3: case 4: case 5: if (size < 1) return 0; memcpy(buffer, "a", 1); _state++; return 1; case 6: if (size < 1) return 0; memcpy(buffer, "", 1); _state++; return 1; case 7: if (size < 1) return 0; memcpy(buffer, "b", 1); _state++; return 1; } return 0; } @end |