ObjFW  Check-in [36d50170c9]

Overview
Comment:Throw exception if the key was not found in the OFDictionary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 36d50170c9d0a6d7c2e82423c6be3d5303d62c5af0cb351da70c70d0c00ed0bf
User & Date: js on 2009-04-22 22:37:40
Other Links: manifest | tags
Context
2009-04-22
22:43
gcc seems to accept a @throw instead of a return as well. check-in: 8dbc052d49 user: js tags: trunk
22:37
Throw exception if the key was not found in the OFDictionary. check-in: 36d50170c9 user: js tags: trunk
22:17
Better handling of closed and ended files in OFFile. check-in: b13a0f08e7 user: js tags: trunk
Changes

Modified src/OFDictionary.m from [c3ececb121] to [e981a8bffe].

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

- get: (OFObject*)key
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		return nil;

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next)
		if ([iter->object isEqual: key])
			return iter->next->object;

	return nil;
}

- remove: (OFObject*)key
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		return self; // FIXME: Throw exception?

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) {
		if ([iter->object isEqual: key]) {
			[data[hash] remove: iter->next];
			[data[hash] remove: iter];

			return self;
		}
	}

	return self;  // FIXME: Throw exception?
}

/* FIXME: Implement this! */
/*
- (BOOL)isEqual
{
}
*/
@end







|





|








|










|









109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

- get: (OFObject*)key
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		@throw [OFNotInSetException newWithClass: isa];

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next)
		if ([iter->object isEqual: key])
			return iter->next->object;

	@throw [OFNotInSetException newWithClass: isa];
}

- remove: (OFObject*)key
{
	uint32_t hash = [key hash] & (size - 1);
	of_list_object_t *iter;

	if (data[hash] == nil)
		@throw [OFNotInSetException newWithClass: isa];

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) {
		if ([iter->object isEqual: key]) {
			[data[hash] remove: iter->next];
			[data[hash] remove: iter];

			return self;
		}
	}

	@throw [OFNotInSetException newWithClass: isa];
}

/* FIXME: Implement this! */
/*
- (BOOL)isEqual
{
}
*/
@end

Modified src/OFExceptions.h from [0feac0a56e] to [a0a41c0e56].

208
209
210
211
212
213
214
215






216
217
218
219
220
221
222
 */
@interface OFInvalidFormatException: OFException {}
@end

/**
 * An OFException indicating that initializing something failed.
 */
@interface OFInitializationFailedException: OFException






@end

/**
 * An OFException indicating the file couldn't be opened.
 */
@interface OFOpenFileFailedException: OFException
{







|
>
>
>
>
>
>







208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
 */
@interface OFInvalidFormatException: OFException {}
@end

/**
 * An OFException indicating that initializing something failed.
 */
@interface OFInitializationFailedException: OFException {}
@end

/**
 * An OFException indicating that the requested key is not in the set.
 */
@interface OFNotInSetException: OFException {}
@end

/**
 * An OFException indicating the file couldn't be opened.
 */
@interface OFOpenFileFailedException: OFException
{

Modified src/OFExceptions.m from [5d90848615] to [0456aef792].

265
266
267
268
269
270
271













272
273
274
275
276
277
278
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Initialization failed for class %s!", [class name]);














	return string;
}
@end

@implementation OFOpenFileFailedException
+ newWithClass: (Class)class_
       andPath: (const char*)path_







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







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
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Initialization failed for class %s!", [class name]);

	return string;
}
@end

@implementation OFNotInSetException
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "The requested key is not in the set of type %s!",
	    [class name]);

	return string;
}
@end

@implementation OFOpenFileFailedException
+ newWithClass: (Class)class_
       andPath: (const char*)path_

Modified tests/OFDictionary/OFDictionary.m from [4a2ea7d8a4] to [c41b0b3f31].

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
#include <stdio.h>
#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFConstString.h"
#import "OFString.h"


int
main()
{


	OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16];

	OFAutoreleasePool *pool = [OFAutoreleasePool new];
	OFString *key1 = [OFString stringWithCString: "key1"];
	OFString *key2 = [OFString stringWithCString: "key2"];
	OFString *value1 = [OFString stringWithCString: "value1"];
	OFString *value2 = [OFString stringWithCString: "value2"];

	[dict set: key1
	       to: value1];
	[dict set: key2
	       to: value2];
	[pool release];

	if (strcmp([[dict get: @"key1"] cString], "value1")) {
		puts("\033[K\033[1;31mTest 1/2 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/2 failed!\033[m");
		return 1;
	}
























	puts("\033[1;32mTests successful: 2/2\033[0m");
	return 0;
}







>




>
>















|




|



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


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
#include <stdio.h>
#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFConstString.h"
#import "OFString.h"
#import "OFExceptions.h"

int
main()
{
	BOOL caught;

	OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16];

	OFAutoreleasePool *pool = [OFAutoreleasePool new];
	OFString *key1 = [OFString stringWithCString: "key1"];
	OFString *key2 = [OFString stringWithCString: "key2"];
	OFString *value1 = [OFString stringWithCString: "value1"];
	OFString *value2 = [OFString stringWithCString: "value2"];

	[dict set: key1
	       to: value1];
	[dict set: key2
	       to: value2];
	[pool release];

	if (strcmp([[dict get: @"key1"] cString], "value1")) {
		puts("\033[K\033[1;31mTest 1/4 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/4 failed!\033[m");
		return 1;
	}

	caught = NO;
	@try {
		[dict get: @"key3"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 3/4 failed!\033[m");
		return 1;
	}

	[dict remove: @"key2"];
	caught = NO;
	@try {
		[dict remove: @"key2"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 4/4 failed!\033[m");
		return 1;
	}

	puts("\033[1;32mTests successful: 4/4\033[0m");
	return 0;
}