ObjFW  Check-in [dc1093e2a5]

Overview
Comment:Simplify seeking.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dc1093e2a52a6446c822aa4fde1042eda5caa762dde3d590573b4dc0b2741936
User & Date: js on 2012-10-29 13:03:53
Other Links: manifest | tags
Context
2012-10-29
13:35
Documentation improvements (add references). check-in: e685b742d9 user: js tags: trunk
13:03
Simplify seeking. check-in: dc1093e2a5 user: js tags: trunk
12:35
Documentation fixes. check-in: 318d5d7e53 user: js tags: trunk
Changes

Modified src/OFFile.m from [ffd2b8da5e] to [d382f915ee].

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
713
714
715
716
717
718
719
720
721

722
723
724
725

726


























727
728
729
730
731
732
733







+

-
+



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







	if (fd == -1 || atEndOfStream || write(fd, buffer, length) < length)
		@throw [OFWriteFailedException exceptionWithClass: [self class]
							   stream: self
						  requestedLength: length];
}

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

- (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)lowlevelSeekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

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

	return ret;
}

- (int)fileDescriptorForReading
{
	return fd;
}

818
819
820
821
822
823
824
825
826
827
828
829

830
831
832
833
834
835
836
837
838
839
840
841
793
794
795
796
797
798
799





800







801
802
803
804
805







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





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

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

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

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

Modified src/OFSeekableStream.h from [d4678f54b9] to [f4e43f22ad].

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

75
76

77
78
79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
95

96
97


98
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







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






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

-
-
+
+
-
-
-
-
-
-
-










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

-
+
+

#include <sys/types.h>

#import "OFStream.h"

/*!
 * @brief A stream that supports seeking.
 *
 * @note If you want to subclass this, override lowlevelSeekToOffset:,
 * @note If you want to subclass this, override
 *	 lowlevelSeekForwardWithOffset: and lowlevelSeekToOffsetRelativeToEnd:,
 *	 but nothing else, as they 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
 *	 @ref lowlevelSeekToOffset:whence:. OFSeekableStream uses this method
 *	 and makes it work together with the caching of OFStream. If you
 *	 override this methods without the lowlevel prefix, you <i>will</i>
 *	 break caching, get broken results and seek to the wrong position!
 *	 position!
 */
@interface OFSeekableStream: OFStream
/*!
 * @brief Seeks to the specified absolute offset.
 *
 * @param offset The offset in bytes
 */
- (void)seekToOffset: (off_t)offset;

 * @param whence From where to seek. Possible values are:
/*!
 * @brief Seeks to the specified offset, relative to the current location.
 *		  * SEEK_SET: Seek to the specified byte.
 *
 * @param offset The offset relative to the current location
 *		  * SEEK_CUR: Seek to the current location + offset.
 * @return The absolute offset
 *		  * SEEK_END: Seek to the end of the stream + offset.
 */
- (off_t)seekForwardWithOffset: (off_t)offset;

- (void)seekToOffset: (off_t)offset
	      whence: (int)whence;
/*!
 * @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;

 * @param whence From where to seek. Possible values are:
/*!
 * @brief Seek the stream on the lowlevel.
 *		  * SEEK_SET: Seek to the specified byte.
 *
 * @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;

 *		  * SEEK_CUR: Seek to the current location + 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
 *		  * SEEK_END: Seek to the end of the stream + offset.
 */
- (off_t)lowlevelSeekToOffsetRelativeToEnd: (off_t)offset;
- (void)lowlevelSeekToOffset: (off_t)offset
		      whence: (int)whence;
@end

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

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
16
17
18
19
20
21
22





23

24
25
26
27
28






29
30
31


32
33




34







35
36
37

38















39







-
-
-
-
-
+
-





-
-
-
-
-
-

+

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



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


#import "OFSeekableStream.h"

#import "OFNotImplementedException.h"

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

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

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

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

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


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

	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 lowlevelSeekToOffsetRelativeToEnd: offset];

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

	return ret;
}
@end