ObjFW  Check-in [1c4c60ac36]

Overview
Comment:Add +[OFDataArray dataArrayWithContentsOfFile:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1c4c60ac364a92f280ea2f3d460d2db3aac6bb84043b2308de52fec72fa37717
User & Date: js on 2010-11-13 20:48:01
Other Links: manifest | tags
Context
2010-11-13
21:26
Add -[OFMutableString prependString:]. check-in: 5a75edd60e user: js tags: trunk
20:48
Add +[OFDataArray dataArrayWithContentsOfFile:]. check-in: 1c4c60ac36 user: js tags: trunk
16:15
Add support for BOOL to OFNumber. check-in: 0ec87765f4 user: js tags: trunk
Changes

Modified src/OFDataArray.h from [95c10689ac] to [5c1e81dd26].

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


14
15
16
17
18
19
20
/*
 * 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 "OFObject.h"



/**
 * \brief A class for storing arbitrary data in an array.
 *
 * If you plan to store large hunks of data, you should consider using
 * OFBigDataArray, which allocates the memory in pages rather than in bytes.
 */
@interface OFDataArray: OFObject <OFCopying>













>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * 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 "OFObject.h"

@class OFString;

/**
 * \brief A class for storing arbitrary data in an array.
 *
 * If you plan to store large hunks of data, you should consider using
 * OFBigDataArray, which allocates the memory in pages rather than in bytes.
 */
@interface OFDataArray: OFObject <OFCopying>
33
34
35
36
37
38
39









40
41
42
43
44
45
46
47
48









49
50
51
52
53
54
55
 * Creates a new OFDataArray whose items all have the same size.
 *
 * \param is The size of each element in the OFDataArray
 * \return A new autoreleased OFDataArray
 */
+ dataArrayWithItemSize: (size_t)is;










/**
 * Initializes an already allocated OFDataArray whose items all have the same
 * size.
 *
 * \param is The size of each element in the OFDataArray
 * \return An initialized OFDataArray
 */
- initWithItemSize: (size_t)is;










/**
 * \return The number of items in the OFDataArray
 */
- (size_t)count;

/**
 * \return The size of each item in the OFDataArray in bytes







>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>







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
 * Creates a new OFDataArray whose items all have the same size.
 *
 * \param is The size of each element in the OFDataArray
 * \return A new autoreleased OFDataArray
 */
+ dataArrayWithItemSize: (size_t)is;

/**
 * Creates a new OFDataArary with an item size of 1, containing the data of the
 * specified file.
 *
 * \param path The path of the file
 * \return A new autoreleased OFDataArray
 */
+ dataArrayWithContentsOfFile: (OFString*)path;

/**
 * Initializes an already allocated OFDataArray whose items all have the same
 * size.
 *
 * \param is The size of each element in the OFDataArray
 * \return An initialized OFDataArray
 */
- initWithItemSize: (size_t)is;

/**
 * Initializes an already allocated OFDataArray with an item size of 1,
 * containing the data of the specified file.
 *
 * \param path The path of the file
 * \return An initialized OFDataArray
 */
- initWithContentsOfFile: (OFString*)path;

/**
 * \return The number of items in the OFDataArray
 */
- (size_t)count;

/**
 * \return The size of each item in the OFDataArray in bytes

Modified src/OFDataArray.m from [e2af4c2cc3] to [de22667a5c].

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
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <limits.h>

#import "OFDataArray.h"


#import "OFExceptions.h"
#import "macros.h"

@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
{
	return [[[self alloc] initWithItemSize: is] autorelease];
}






- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithItemSize: (size_t)is
{
	self = [super init];

	@try {
		if (is == 0)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		data = NULL;
		itemSize = is;

































	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}







>
>








>
>
>
>
>




















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
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
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <limits.h>

#import "OFDataArray.h"
#import "OFString.h"
#import "OFFile.h"
#import "OFExceptions.h"
#import "macros.h"

@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
{
	return [[[self alloc] initWithItemSize: is] autorelease];
}

+ dataArrayWithContentsOfFile: (OFString*)path
{
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithItemSize: (size_t)is
{
	self = [super init];

	@try {
		if (is == 0)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		data = NULL;
		itemSize = is;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	self = [super init];

	@try {
		OFFile *file = [[OFFile alloc] initWithPath: path
						       mode: @"rb"];
		itemSize = 1;

		@try {
			char *buf = [self allocMemoryWithSize: of_pagesize];

			while (![file isAtEndOfStream]) {
				size_t size;

				size = [file readNBytes: of_pagesize
					     intoBuffer: buf];
				[self addNItems: size
				     fromCArray: buf];
			}

			[self freeMemory: buf];
		} @finally {
			[file release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}