ObjFW  Check-in [61c1932caf]

Overview
Comment:OFStream: Use lowlevel as prefix instead of _.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 61c1932cafe507b85cd66894706e4b5b0343600963000a90d2d51f342db3be76
User & Date: js on 2012-09-16 15:43:45
Other Links: manifest | tags
Context
2012-09-16
21:59
Make it possible to invalidate OFTimers. check-in: 8658d1a22a user: js tags: trunk
15:43
OFStream: Use lowlevel as prefix instead of _. check-in: 61c1932caf user: js tags: trunk
15:27
Prefix all private methods with OF_. check-in: c137da5e5b user: js tags: trunk
Changes

Modified src/OFFile.m from [f9e4a0a447] to [a11a54fb53].

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
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
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
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







-
+







-
+















-
+








-
+








-
+












-
+







	self = [super init];

	fd = fileDescriptor;

	return self;
}

- (BOOL)_isAtEndOfStream
- (BOOL)lowlevelIsAtEndOfStream
{
	if (fd == -1)
		return YES;

	return atEndOfStream;
}

- (size_t)_readIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
		   length: (size_t)length
{
	ssize_t ret;

	if (fd == -1 || atEndOfStream || (ret = read(fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithClass: [self class]
							  stream: self
						 requestedLength: length];

	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

- (void)_writeBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void*)buffer
	      length: (size_t)length
{
	if (fd == -1 || atEndOfStream || write(fd, buffer, length) < length)
		@throw [OFWriteFailedException exceptionWithClass: [self class]
							   stream: self
						  requestedLength: length];
}

- (void)_seekToOffset: (off_t)offset
- (void)lowlevelSeekToOffset: (off_t)offset
{
	if (lseek(fd, offset, SEEK_SET) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_SET];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
- (off_t)lowlevelSeekForwardWithOffset: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fd, offset, SEEK_CUR)) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_CUR];

	return ret;
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
- (off_t)lowlevelSeekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fd, offset, SEEK_END)) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
819
820
821
822
823
824
825
826

827
828
829
830
831
832

833
834
835
836
837
838

839
840
841
842
843
819
820
821
822
823
824
825

826
827
828
829
830
831

832
833
834
835
836
837

838
839
840
841
842
843







-
+





-
+





-
+





- (void)dealloc
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
	[super dealloc];	/* Get rid of stupid warning */
}

- (void)_seekToOffset: (off_t)offset
- (void)lowlevelSeekToOffset: (off_t)offset
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
- (off_t)lowlevelSeekForwardWithOffset: (off_t)offset
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
- (off_t)lowlevelSeekToOffsetRelativeToEnd: (off_t)offset
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}
@end

Modified src/OFProcess.m from [fe517b8418] to [7f1558d0a7].

221
222
223
224
225
226
227
228

229
230
231
232
233
234
235
236
237
238
239
240

241
242
243
244
245
246
247
221
222
223
224
225
226
227

228
229
230
231
232
233
234
235
236
237
238
239

240
241
242
243
244
245
246
247







-
+











-
+







		[self release];
		@throw e;
	}

	return self;
}

- (BOOL)_isAtEndOfStream
- (BOOL)lowlevelIsAtEndOfStream
{
#ifndef _WIN32
	if (readPipe[0] == -1)
#else
	if (readPipe[0] == NULL)
#endif
		return YES;

	return atEndOfStream;
}

- (size_t)_readIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
		   length: (size_t)length
{
#ifndef _WIN32
	ssize_t ret;
#else
	DWORD ret;
#endif
265
266
267
268
269
270
271
272

273
274
275
276
277
278
279
265
266
267
268
269
270
271

272
273
274
275
276
277
278
279







-
+








	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

- (void)_writeBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void*)buffer
	      length: (size_t)length
{
#ifndef _WIN32
	if (writePipe[1] == -1 || atEndOfStream ||
	    write(writePipe[1], buffer, length) < length)
#else
	DWORD ret;

Modified src/OFSeekableStream.h from [7d6bf274be] to [38bd04ae73].

24
25
26
27
28
29
30
31
32
33
34
35
36







37
38
39
40
41
42
43
24
25
26
27
28
29
30






31
32
33
34
35
36
37
38
39
40
41
42
43
44







-
-
-
-
-
-
+
+
+
+
+
+
+







#include <sys/types.h>

#import "OFStream.h"

/**
 * \brief A stream that supports seeking.
 *
 * IMPORTANT: If you want to subclass this, override _seekToOffset:,
 * _seekForwardWithOffset: and _seekToOffsetRelativeToEnd:, but nothing else.
 * Those are not defined in the headers, but do the actual work.
 * OFSeekableStream uses those and makes them work together with the caching of
 * OFStream. If you override these methods without the _ prefix, you *WILL*
 * break caching, get broken results and seek to the wrong position!
 * \note If you want to subclass this, override lowlevelSeekToOffset:,
 *	 lowlevelSeekForwardWithOffset: and lowlevelSeekToOffsetRelativeToEnd:,
 *	 but nothing else. Those are not defined in the headers, but do the
 *	 actual work. OFSeekableStream uses those and makes them work together
 *	 with the caching of OFStream. If you override these methods without
 *	 the lowlevel prefix, you <i>will</i> break caching, get broken results
 *	 and seek to the wrong position!
 */
@interface OFSeekableStream: OFStream
/**
 * \brief Seeks to the specified absolute offset.
 *
 * \param offset The offset in bytes
 */
54
55
56
57
58
59
60




































61
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

/**
 * \brief Seeks to the specified offset, relative to the end of the stream.
 *
 * \param offset The offset relative to the end of the stream
 * \return The absolute offset
 */
- (off_t)seekToOffsetRelativeToEnd: (off_t)offset;

/**
 * \brief Seek the stream on the lowlevel.
 *
 * \warning Do not call this directly!
 *
 * Override this with this method with your actual seek implementation when
 * subclassing!
 *
 * \param offset The offset to seek to
 */
- (void)lowlevelSeekToOffset: (off_t)offset;

/**
 * \brief Seek the stream on the lowlevel.
 *
 * \warning Do not call this directly!
 *
 * Override this with this method with your actual seek implementation when
 * subclassing!
 *
 * \param offset The offset to seek forward to
 */
- (off_t)lowlevelSeekForwardWithOffset: (off_t)offset;

/**
 * \brief Seek the stream on the lowlevel.
 *
 * \warning Do not call this directly!
 *
 * Override this with this method with your actual seek implementation when
 * subclassing!
 *
 * \param offset The offset to seek to, relative to the end
 */
- (off_t)lowlevelSeekToOffsetRelativeToEnd: (off_t)offset;
@end

Modified src/OFSeekableStream.m from [e3dd88a6e5] to [3a3c16de1f].

15
16
17
18
19
20
21
22

23
24
25
26
27
28

29
30
31
32
33
34

35
36
37
38
39
40
41
42

43
44
45
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
15
16
17
18
19
20
21

22
23
24
25
26
27

28
29
30
31
32
33

34
35
36
37
38
39
40
41

42
43
44
45
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







-
+





-
+





-
+







-
+










-
+












-
+








 */

#import "OFSeekableStream.h"

#import "OFNotImplementedException.h"

@implementation OFSeekableStream
- (void)_seekToOffset: (off_t)offset
- (void)lowlevelSeekToOffset: (off_t)offset
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
- (off_t)lowlevelSeekForwardWithOffset: (off_t)offset
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
- (off_t)lowlevelSeekToOffsetRelativeToEnd: (off_t)offset
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (void)seekToOffset: (off_t)offset
{
	[self _seekToOffset: offset];
	[self lowlevelSeekToOffset: offset];

	[self freeMemory: cache];
	cache = NULL;
	cacheLength = 0;
}

- (off_t)seekForwardWithOffset: (off_t)offset
{
	off_t ret;

	ret = [self _seekForwardWithOffset: offset - cacheLength];
	ret = [self lowlevelSeekForwardWithOffset: offset - cacheLength];

	[self freeMemory: cache];
	cache = NULL;
	cacheLength = 0;

	return ret;
}

- (off_t)seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

	ret = [self _seekToOffsetRelativeToEnd: offset];
	ret = [self lowlevelSeekToOffsetRelativeToEnd: offset];

	[self freeMemory: cache];
	cache = NULL;
	cacheLength = 0;

	return ret;
}
@end

Modified src/OFStream.h from [1a309ecdea] to [67eb3e5d0e].

40
41
42
43
44
45
46
47
48
49
50
51
52






53
54
55
56
57
58
59
40
41
42
43
44
45
46






47
48
49
50
51
52
53
54
55
56
57
58
59







-
-
-
-
-
-
+
+
+
+
+
+







 * \warning Even though the OFCopying protocol is implemented, it does
 *	    <i>not</i> return an independent copy of the stream, but instead
 *	    retains it. This is so that the stream can be used as a key for a
 *	    dictionary, so context can be associated with a stream. Using a
 *	    stream in more than one thread at the same time is not thread-safe,
 *	    even if copy was called to create one "instance" for every thread!
 *
 * \warning If you want to subclass this, override _readIntoBuffer:length:,
 *	    _writeBuffer:length: and _isAtEndOfStream, but nothing else, as
 *	    those are are the methods that do the actual work. OFStream uses
 *	    those for all other methods and does all the caching and other
 *	    stuff for you. If you override these methods without the _ prefix,
 *	    you <i>will</i> break caching and get broken results!
 * \note If you want to subclass this, override lowlevelReadIntoBuffer:length:,
 *	 lowlevelWriteBuffer:length: and lowlevelIsAtEndOfStream, but nothing
 *	 else, as those are are the methods that do the actual work. OFStream
 *	 uses those for all other methods and does all the caching and other
 *	 stuff for you. If you override these methods without the lowlevel
 *	 prefix, you <i>will</i> break caching and get broken results!
 */
@interface OFStream: OFObject <OFCopying>
{
	char   *cache;
	char   *writeBuffer;
	size_t cacheLength, writeBufferLength;
	BOOL   writeBufferEnabled;
879
880
881
882
883
884
885











886

887












888

889











890

891
892
893
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896

897
898
899
900
901
902
903
904
905
906
907
908
909
910

911
912
913
914
915
916
917
918
919
920
921
922
923

924
925
926
927







+
+
+
+
+
+
+
+
+
+
+
-
+

+
+
+
+
+
+
+
+
+
+
+
+
-
+

+
+
+
+
+
+
+
+
+
+
+
-
+



- (int)fileDescriptorForWriting;

/**
 * \brief Closes the stream.
 */
- (void)close;

/**
 * \brief Performs a lowlevel read.
 *
 * \warning Do not call this directly!
 *
 * Override this method with your actual read implementation when subclassing!
 *
 * \param buffer The buffer for the data to read
 * \param length The length of the buffer
 * \return The number of bytes read
 */
- (size_t)_readIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
		   length: (size_t)length;


/**
 * \brief Performs a lowlevel write.
 *
 * \warning Do not call this directly!
 *
 * Override this method with your actual write implementation when subclassing!
 *
 * \param buffer The buffer with the data to write
 * \param length The length of the data to write
 */
- (void)_writeBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void*)buffer
	      length: (size_t)length;

/**
 * \brief Returns whether the lowlevel is at the end of the stream.
 *
 * \warning Do not call this directly!
 *
 * Override this method with your actual end of stream checking implementation
 * when subclassing!
 *
 * \return Whether the lowlevel is at the end of the stream
 */
- (BOOL)_isAtEndOfStream;
- (BOOL)lowlevelIsAtEndOfStream;

- (BOOL)OF_isWaitingForDelimiter;
@end

Modified src/OFStream.m from [c9efb96401] to [92797b60e4].

67
68
69
70
71
72
73
74

75
76
77
78
79
80

81
82
83
84
85
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
67
68
69
70
71
72
73

74
75
76
77
78
79

80
81
82
83
84
85
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







-
+





-
+






-
+
















-
+






-
+







	cache = NULL;
	writeBuffer = NULL;
	blocking = YES;

	return self;
}

- (BOOL)_isAtEndOfStream
- (BOOL)lowlevelIsAtEndOfStream
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (size_t)_readIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
		   length: (size_t)length
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (void)_writeBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void*)buffer
	      length: (size_t)length
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- copy
{
	return [self retain];
}

- (BOOL)isAtEndOfStream
{
	if (cache != NULL)
		return NO;

	return [self _isAtEndOfStream];
	return [self lowlevelIsAtEndOfStream];
}

- (size_t)readIntoBuffer: (void*)buffer
		  length: (size_t)length
{
	if (cache == NULL)
		return [self _readIntoBuffer: buffer
		return [self lowlevelReadIntoBuffer: buffer
				      length: length];

	if (length >= cacheLength) {
		size_t ret = cacheLength;
		memcpy(buffer, cache, cacheLength);

		[self freeMemory: cache];
558
559
560
561
562
563
564
565

566
567
568
569
570
571
572
558
559
560
561
562
563
564

565
566
567
568
569
570
571
572







-
+







		}
	}

	/* Read and see if we get a newline or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if ([self _isAtEndOfStream]) {
		if ([self lowlevelIsAtEndOfStream]) {
			if (cache == NULL) {
				waitingForDelimiter = NO;
				return nil;
			}

			retLength = cacheLength;

581
582
583
584
585
586
587
588

589
590
591
592
593
594
595
581
582
583
584
585
586
587

588
589
590
591
592
593
594
595







-
+







			cache = NULL;
			cacheLength = 0;

			waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self _readIntoBuffer: buffer
		bufferLength = [self lowlevelReadIntoBuffer: buffer
					      length: of_pagesize];

		/* Look if there's a newline or \0 */
		for (i = 0; i < bufferLength; i++) {
			if OF_UNLIKELY (buffer[i] == '\n' ||
			    buffer[i] == '\0') {
				retLength = cacheLength + i;
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
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







-
+

















-
+







		}
	}

	/* Read and see if we get a delimiter or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if ([self _isAtEndOfStream]) {
		if ([self lowlevelIsAtEndOfStream]) {
			if (cache == NULL) {
				waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: cache
						 encoding: encoding
						   length: cacheLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;

			waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self _readIntoBuffer: buffer
		bufferLength = [self lowlevelReadIntoBuffer: buffer
					      length: of_pagesize];

		/* Look if there's a delimiter or \0 */
		for (i = 0; i < bufferLength; i++) {
			if (buffer[i] != delimiterUTF8String[j++])
				j = 0;

887
888
889
890
891
892
893
894

895
896
897
898
899
900
901
902
903
904
905
906

907
908
909
910
911
912
913
887
888
889
890
891
892
893

894
895
896
897
898
899
900
901
902
903
904
905

906
907
908
909
910
911
912
913







-
+











-
+







}

- (void)flushWriteBuffer
{
	if (writeBuffer == NULL)
		return;

	[self _writeBuffer: writeBuffer
	[self lowlevelWriteBuffer: writeBuffer
		    length: writeBufferLength];

	[self freeMemory: writeBuffer];
	writeBuffer = NULL;
	writeBufferLength = 0;
}

- (void)writeBuffer: (const void*)buffer
	     length: (size_t)length
{
	if (!writeBufferEnabled)
		[self _writeBuffer: buffer
		[self lowlevelWriteBuffer: buffer
			    length: length];
	else {
		writeBuffer = [self resizeMemory: writeBuffer
					    size: writeBufferLength + length];
		memcpy(writeBuffer + writeBufferLength, buffer, length);
		writeBufferLength += length;
	}

Modified src/OFStreamSocket.m from [fbc78fae5c] to [b406096bcb].

61
62
63
64
65
66
67
68

69
70
71
72
73

74
75
76
77
78
79
80
61
62
63
64
65
66
67

68
69
70
71
72

73
74
75
76
77
78
79
80







-
+




-
+







#endif

+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_isAtEndOfStream
- (BOOL)lowlevelIsAtEndOfStream
{
	return atEndOfStream;
}

- (size_t)_readIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
		   length: (size_t)length
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithClass: [self class]
							    socket: self];
101
102
103
104
105
106
107
108

109
110
111
112
113
114
115
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115







-
+








	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

- (void)_writeBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void*)buffer
	      length: (size_t)length
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithClass: [self class]
							    socket: self];

	if (atEndOfStream) {

Modified tests/OFStreamTests.m from [cf598bf19b] to [6be1a5b957].

29
30
31
32
33
34
35
36

37
38
39
40
41

42
43
44
45
46
47
48
29
30
31
32
33
34
35

36
37
38
39
40

41
42
43
44
45
46
47
48







-
+




-
+







@interface StreamTester: OFStream
{
	int state;
}
@end

@implementation StreamTester
- (BOOL)_isAtEndOfStream
- (BOOL)lowlevelIsAtEndOfStream
{
	return (state > 1 ? YES : NO);
}

- (size_t)_readIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
		   length: (size_t)size
{
	switch (state) {
	case 0:
		if (size < 1)
			return 0;