ObjFW  Check-in [a1e066138c]

Overview
Comment:Implementation for OFArray.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a1e066138c6d8d79e154ac69bd0e04e6a06d062dfdd0bfb50971fb26b7df1ea4
User & Date: js on 2008-11-02 00:42:36
Other Links: manifest | tags
Context
2008-11-02
02:09
Added -data for OFArray & one new test. check-in: 61fc89489a user: js tags: trunk
00:42
Implementation for OFArray. check-in: a1e066138c user: js tags: trunk
00:27
Some fixes for OFObject. check-in: 9ff3dbe9f9 user: js tags: trunk
Changes

Modified src/OFArray.h from [d335e7706f] to [cfdd21c800].

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

#import <stddef.h>

#import "OFObject.h"

@interface OFArray: OFObject
{
	void   *data;
	size_t itemsize;
	size_t size;
}


- initWithItemSize: (size_t)is;
- (size_t)size;
- (void*)item: (size_t)item;
- (void*)last;
- add: (void*)item;
- addNItems: (size_t)nitems
 fromCArray: (void*)carray;
- removeLastNItems: (size_t)nitems;
@end

@interface OFBigArray: OFArray
{
	size_t realsize;
}

- initWithSize: (size_t)is;
- addNItems: (size_t)nitems
 fromCArray: (void*)carray;
- removeLastNItems: (size_t)nitems;
@end







|

|


>

|





|




|





|

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

#import <stddef.h>

#import "OFObject.h"

@interface OFArray: OFObject
{
	char   *data;
	size_t itemsize;
	size_t items;
}

+ newWithItemSize: (size_t)is;
- initWithItemSize: (size_t)is;
- (size_t)items;
- (void*)item: (size_t)item;
- (void*)last;
- add: (void*)item;
- addNItems: (size_t)nitems
 fromCArray: (void*)carray;
- removeNItems: (size_t)nitems;
@end

@interface OFBigArray: OFArray
{
	size_t size;
}

- initWithSize: (size_t)is;
- addNItems: (size_t)nitems
 fromCArray: (void*)carray;
- removeNItems: (size_t)nitems;
@end

Modified src/OFArray.m from [148d16c543] to [c20f86a7ff].

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. 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 "OFArray.h"

#import "OFExceptions.h"
#import "OFMacros.h"

@implementation OFArray





- initWithItemSize: (size_t)is
{
	if ((self = [super init])) {
		data = NULL;
		itemsize = is;
		size = 0;
	}

	return self;
}

- (size_t)size
{
	return size;
}

- (size_t)itemsize
{
	return itemsize;
}

- (void*)item: (size_t)item
{

	/* FIXME */

	OF_NOT_IMPLEMENTED(NULL)

}

- (void*)last
{
	/* FIXME */
	OF_NOT_IMPLEMENTED(NULL)
}

- add: (void*)item
{

	return [self addNItems: 1
		    fromCArray: item];




}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{


	/* FIXME */



	OF_NOT_IMPLEMENTED(self)


}



- removeLastNItems: (size_t)nitems
{


	/* FIXME */



	OF_NOT_IMPLEMENTED(self)



}
@end

@implementation OFBigArray
- initWithSize: (size_t)is
{
	if ((self = [super init]))
		realsize = 0;

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	/* FIXME */
	OF_NOT_IMPLEMENTED(self)
}

- removeLastNItems: (size_t)nitems
{
	/* FIXME */
	OF_NOT_IMPLEMENTED(self)
}
@end











>
>
>






>
>
>
>
>





|





|

|









>
|
>
|
>




|
<




>
|
|
>
>
>
>





>
>
|
>
>
>
|
>
>
|
>
|
>
|

>
>
|
>
>
>
|
>
>
>







|











|





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
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
119
120
121
122
123
124
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. 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 <stdio.h>
#import <string.h>

#import "OFArray.h"

#import "OFExceptions.h"
#import "OFMacros.h"

@implementation OFArray
+ newWithItemSize: (size_t)is
{
	return [[OFArray alloc] initWithItemSize: is];
}

- initWithItemSize: (size_t)is
{
	if ((self = [super init])) {
		data = NULL;
		itemsize = is;
		items = 0;
	}

	return self;
}

- (size_t)items
{
	return items;
}

- (size_t)itemsize
{
	return itemsize;
}

- (void*)item: (size_t)item
{
	if (item >= items)
		/* FIXME: Maybe OFOutOfRangeException would be better? */
		[[OFOverflowException newWithObject: self] raise];

	return data + item * itemsize;
}

- (void*)last
{
	return data + (items - 1) * itemsize;

}

- add: (void*)item
{
	data = [self resizeMem: data
		      toNItems: items + 1
			ofSize: itemsize];

	memcpy(data + items++ * itemsize, item, itemsize);

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	if (nitems > SIZE_MAX - items)
		[[OFOverflowException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items + nitems
			ofSize: itemsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;

	return self;
}

- removeNItems: (size_t)nitems
{
	if (nitems > items)
		[[OFOverflowException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items - nitems
			ofSize: itemsize];

	items -= nitems;

	return self;
}
@end

@implementation OFBigArray
- initWithSize: (size_t)is
{
	if ((self = [super init]))
		size = 0;

	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	/* FIXME */
	OF_NOT_IMPLEMENTED(self)
}

- removeNItems: (size_t)nitems
{
	/* FIXME */
	OF_NOT_IMPLEMENTED(self)
}
@end

Modified tests/Makefile from [a5c3f52b03] to [18ee2d1c94].

1
2
3
SUBDIRS = OFObject OFHashes OFString OFList OFWideString OFXMLFactory

include ../buildsys.mk
|


1
2
3
SUBDIRS = OFObject OFArray OFHashes OFString OFList OFWideString OFXMLFactory

include ../buildsys.mk

Added tests/OFArray/Makefile version [7f2447d330].







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PROG_NOINST = ofarray
SRCS = OFArray.m

include ../../buildsys.mk

CPPFLAGS += -I../../src
LIBS += -lobjc -L../../src -lobjfw

.PHONY: run

all: run
run: ${PROG_NOINST}
	rm -f libobjfw.so.1 libobjfw.dylib
	ln -s ../../src/libobjfw.so libobjfw.so.1
	ln -s ../../src/libobjfw.dylib libobjfw.dylib
	LD_LIBRARY_PATH=. \
	DYLD_LIBRARY_PATH=. \
	./${PROG_NOINST}
	rm -f libobjfw.so.1 libobjfw.dylib

Added tests/OFArray/OFArray.m version [83725e7e15].













































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. 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 <stdio.h>
#import <stdlib.h>
#import <string.h>

#import "OFArray.h"
#import "OFExceptions.h"

#define CATCH_EXCEPTION(code, exception)		\
	caught = NO;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = YES;				\
		puts("CAUGHT! Error string was:");	\
		fputs([e cString], stdout);		\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}

int
main()
{
	BOOL caught;
	OFArray *a;
	void *p, *q;
	size_t i;

	puts("Trying to add too much to an array...");
	a = [OFArray newWithItemSize: 4096];
	CATCH_EXCEPTION([a addNItems: SIZE_MAX
			  fromCArray: NULL],
	    OFOverflowException)

	puts("Trying to add something after that error...");
	p = [a getMemWithSize: 4096];
	memset(p, 255, 4096);
	[a add: p];
	if (!memcmp([a last], p, 4096))
		puts("[a last] matches with p!");
	else {
		puts("[a last] does not match p!");
		abort();
	}
	[a freeMem: p];

	puts("Adding more data...");
	q = [a getMemWithSize: 4096];
	memset(q, 42, 4096);
	[a add: q];
	if (!memcmp([a last], q, 4096))
		puts("[a last] matches with q!");
	else {
		puts("[a last] does not match q!");
		abort();
	}
	[a freeMem: q];

	puts("Adding multiple items at once...");
	p = [a getMemWithSize: 8192];
	memset(p, 64, 8192);
	[a addNItems: 2
	  fromCArray: p];
	if (!memcmp([a last], [a item: [a items] - 2], 4096) &&
	    !memcmp([a item: [a items] - 2], p, 4096))
		puts("[a last], [a item: [a items] - 2] and p match!");
	else {
		puts("[a last], [a item: [a items] - 2] and p did not match!");
		abort();
	}
	[a freeMem: p];

	i = [a items];
	puts("Removing 2 items...");
	[a removeNItems: 2];
	if ([a items] + 2 != i) {
		puts("[a items] + 2 != i!");
		abort();
	}

	puts("Trying to remove more data than we added...");
	CATCH_EXCEPTION([a removeNItems: [a items] + 1], OFOverflowException);

	puts("Trying to access an index that does not exist...");
	CATCH_EXCEPTION([a item: [a items]], OFOverflowException);

	[a free];
	return 0;
}