ObjFW  Check-in [0890f73f75]

Overview
Comment:Add OFSeekableStream and implement seeking for OFFile.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0890f73f75718e4b0be80f4c24bfb24ddcbd62232ea6ffc8c3b5b06a413bee53
User & Date: js on 2010-04-09 15:12:58
Other Links: manifest | tags
Context
2010-04-09
15:22
Add -[{read,write}LittleEndianInt{16,32,64}{,:}] to OFStream. check-in: 6217d56014 user: js tags: trunk
15:12
Add OFSeekableStream and implement seeking for OFFile. check-in: 0890f73f75 user: js tags: trunk
14:59
Better hiding of the internal OFStream methods. check-in: 2a9e91b67c user: js tags: trunk
Changes

Modified ObjFW.xcodeproj/project.pbxproj from [25cf49574b] to [a3aca486b4].

124
125
126
127
128
129
130


131
132
133
134
135
136
137
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139







+
+







		4B67998A1099E7C50041064A /* OFXMLParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLParser.m; path = src/OFXMLParser.m; sourceTree = "<group>"; };
		4B67998B1099E7C50041064A /* threading.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = threading.h; path = src/threading.h; sourceTree = "<group>"; };
		4B67998C1099E7C50041064A /* unicode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = unicode.h; path = src/unicode.h; sourceTree = "<group>"; };
		4B6AF96C10A8D3E40003FB0A /* asprintf.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = asprintf.m; path = src/asprintf.m; sourceTree = "<group>"; };
		4B6AF96F10A8D40E0003FB0A /* iso_8859_15.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = iso_8859_15.m; path = src/iso_8859_15.m; sourceTree = "<group>"; };
		4B6AF97210A8D42E0003FB0A /* windows_1252.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = windows_1252.m; path = src/windows_1252.m; sourceTree = "<group>"; };
		4B6AF97310A8D4450003FB0A /* ObjFW.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjFW.h; path = src/ObjFW.h; sourceTree = "<group>"; };
		4B981CDE116F71DD00294DB7 /* OFSeekableStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSeekableStream.h; path = src/OFSeekableStream.h; sourceTree = SOURCE_ROOT; };
		4B981CDF116F71DD00294DB7 /* OFSeekableStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSeekableStream.m; path = src/OFSeekableStream.m; sourceTree = SOURCE_ROOT; };
		4BBA36C411406AB700CBA3AC /* atomic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = atomic.h; path = src/atomic.h; sourceTree = SOURCE_ROOT; };
		4BBA36C511406AB700CBA3AC /* macros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = macros.h; path = src/macros.h; sourceTree = SOURCE_ROOT; };
		4BFBDD1610A0724800051AFB /* unicode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = unicode.m; path = src/unicode.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXGroup section */
		08FB7794FE84155DC02AAC07 /* ObjFW */ = {
167
168
169
170
171
172
173


174
175
176
177
178
179
180
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184







+
+







				4B6799741099E7C50041064A /* OFMutableString.m */,
				4B6799751099E7C50041064A /* OFNumber.h */,
				4B6799761099E7C50041064A /* OFNumber.m */,
				4B6799771099E7C50041064A /* OFObject.h */,
				4B6799781099E7C50041064A /* OFObject.m */,
				4B6799791099E7C50041064A /* OFPlugin.h */,
				4B67997A1099E7C50041064A /* OFPlugin.m */,
				4B981CDE116F71DD00294DB7 /* OFSeekableStream.h */,
				4B981CDF116F71DD00294DB7 /* OFSeekableStream.m */,
				4B67997B1099E7C50041064A /* OFSocket.h */,
				4B67997C1099E7C50041064A /* OFSocket.m */,
				4B67997D1099E7C50041064A /* OFStream.h */,
				4B67997E1099E7C50041064A /* OFStream.m */,
				4B67997F1099E7C50041064A /* OFString.h */,
				4B6799801099E7C50041064A /* OFString.m */,
				4B6799811099E7C50041064A /* OFTCPSocket.h */,

Modified src/Makefile from [ec470858d6] to [a0ae7ee314].

17
18
19
20
21
22
23

24
25
26
27
28
29
30
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31







+







       OFList.m			\
       OFMutableArray.m		\
       OFMutableDictionary.m	\
       OFMutableString.m	\
       OFNumber.m		\
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSeekableStream.m	\
       OFSocket.m		\
       OFStream.m		\
       OFString.m		\
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\
       OFURLEncoding.m		\
       OFXMLElement.m		\

Modified src/OFExceptions.h from [1d2de381ea] to [ae635852ff].

284
285
286
287
288
289
290
291

292
293
294
295
296
297
298
284
285
286
287
288
289
290

291
292
293
294
295
296
297
298







-
+







/**
 * \return A string of the mode in which the file should have been opened
 */
- (OFString*)mode;
@end

/**
 * \brief An exception indicating a read or write to a file failed.
 * \brief An exception indicating a read or write to a stream failed.
 */
@interface OFReadOrWriteFailedException: OFException
{
	size_t req_size;
	int    err;
}

322
323
324
325
326
327
328
329

330
331
332
333
334
335

336
337
338
339














340
341
342
343
344
345
346
322
323
324
325
326
327
328

329
330
331
332
333
334

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360







-
+





-
+




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







/**
 * \return The requested size of the data that couldn't be read / written
 */
- (size_t)requestedSize;
@end

/**
 * \brief An exception indicating a read on a file failed.
 * \brief An exception indicating a read on a stream failed.
 */
@interface OFReadFailedException: OFReadOrWriteFailedException {}
@end

/**
 * \brief An exception indicating a write to a file failed.
 * \brief An exception indicating a write to a stream failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
@end

/**
 * \brief An exception indicating that seeking in a stream failed.
 */
@interface OFSeekFailedException: OFException
{
	int err;
}

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;
@end

/**
 * \brief An exception indicating that changing the mode of a file failed.
 */
@interface OFChangeFileModeFailedException: OFException
{
	OFString *path;
	mode_t mode;

Modified src/OFExceptions.m from [a830318935] to [b73f7f5e15].

482
483
484
485
486
487
488




























489
490
491
492
493
494
495
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523







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







	string = [[OFString alloc] initWithFormat:
	    @"Failed to write %zu bytes in class %s! " ERRFMT, req_size,
	    [class_ className], ERRPARAM];

	return string;
}
@end

@implementation OFSeekFailedException
- initWithClass: (Class)class__
{
	self = [super initWithClass: class__];

	err = GET_ERR;

	return self;
}

- (OFString*)string
{
	if (string != nil)
		return string;

	string = [[OFString alloc] initWithFormat:
	    @"Seeking failed in class %s! " ERRFMT, [class_ className],
	    ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;
}
@end

@implementation OFChangeFileModeFailedException
+ newWithClass: (Class)class__
	  path: (OFString*)path_
	  mode: (mode_t)mode_
{
	return [(OFChangeFileModeFailedException*)[self alloc]

Modified src/OFFile.h from [e16ab950d2] to [efe9dcf448].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23

24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11


12
13

14
15
16
17
18
19
20

21
22
23
24
25
26
27
28











-
-


-
+






-
+







/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include <stdio.h>

#include <sys/types.h>

#import "OFStream.h"
#import "OFSeekableStream.h"

@class OFString;

/**
 * \brief A class which provides functions to read, write and manipulate files.
 */
@interface OFFile: OFStream
@interface OFFile: OFSeekableStream
{
	int fd;
	BOOL closable;
	BOOL eos;
}

/**

Modified src/OFFile.m from [77172f2547] to [a4ba59f372].

250
251
252
253
254
255
256




























257
258
259
260
261
262
263
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291







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







{
	size_t ret;

	if (fd == -1 || eos || (ret = write(fd, buf, size)) < size)
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];

	return ret;
}

- _seekToOffset: (off_t)offset
{
	if (lseek(fd, offset, SEEK_SET) == -1)
		@throw [OFSeekFailedException newWithClass: isa];

	return self;
}

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

	if ((ret = lseek(fd, offset, SEEK_CUR)) == -1)
		@throw [OFSeekFailedException newWithClass: isa];

	return ret;
}

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

	if ((ret = lseek(fd, offset, SEEK_END)) == -1)
		@throw [OFSeekFailedException newWithClass: isa];

	return ret;
}

- close
{
	if (fd != -1)
		close(fd);

Added src/OFSeekableStream.h version [1d030f1b22].


















































1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include <unistd.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!
 */
@interface OFSeekableStream: OFStream
/**
 * Seeks to the specified absolute offset.
 *
 * \param offset The offset in bytes
 */
- seekToOffset: (off_t)offset;

/**
 * Seeks to the specified offset, relative to the current location.
 *
 * \param offset The offset relative to the current location
 * \return The absolute offset
 */
- (off_t)seekForwardWithOffset: (off_t)offset;

/**
 * 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;
@end

Added src/OFSeekableStream.m version [2f22e7d287].





































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFSeekableStream.h"
#import "OFExceptions.h"

@implementation OFSeekableStream
- _seekToOffset: (off_t)offset
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

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

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

	return self;
}

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

	ret = [self _seekForwardWithOffset: offset - cache_len];

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

	return ret;
}

- (off_t)seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret = [self _seekToOffsetRelativeToEnd: offset];

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

	return ret;
}
@end