ObjFW  Diff

Differences From Artifact [30955b0805]:

To Artifact [621aa06394]:


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
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







+









-
-
-
-
-
-
-
-
-
-
-




+
-
+


-
+








#include "config.h"

#include <stdlib.h>

#import "OFObject.h"
#import "OFObject+KeyValueCoding.h"
#import "OFMethodSignature.h"
#import "OFString.h"
#import "OFNumber.h"

#import "OFInvalidArgumentException.h"
#import "OFOutOfMemoryException.h"
#import "OFUndefinedKeyException.h"

int _OFObject_KeyValueCoding_reference;

static char OF_INLINE
nextType(const char **typeEncoding)
{
	char ret = *(*typeEncoding)++;

	while (**typeEncoding >= '0' && **typeEncoding <= '9')
		(*typeEncoding)++;

	return ret;
}

@implementation OFObject (KeyValueCoding)
- (id)valueForKey: (OFString *)key
{
	SEL selector = sel_registerName([key UTF8String]);
	OFMethodSignature *methodSignature =
	const char *typeEncoding = [self typeEncodingForSelector: selector];
	    [self methodSignatureForSelector: selector];
	id ret;

	if (typeEncoding == NULL) {
	if (methodSignature == nil) {
		size_t keyLength;
		char *name;

		if ((keyLength = [key UTF8StringLength]) < 1)
			return [self valueForUndefinedKey: key];

		if ((name = malloc(keyLength + 3)) == NULL)
66
67
68
69
70
71
72
73

74
75
76






77
78
79
80









81
82
83
84
85
86
87
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







-
+

-
-
+
+
+
+
+
+

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







			name[2] = of_ascii_toupper(name[2]);

			selector = sel_registerName(name);
		} @finally {
			free(name);
		}

		typeEncoding = [self typeEncodingForSelector: selector];
		methodSignature = [self methodSignatureForSelector: selector];

		if (typeEncoding == NULL ||
		    *typeEncoding == '@' || *typeEncoding == '#')
		if (methodSignature == NULL)
			return [self valueForUndefinedKey: key];

		switch (*[methodSignature methodReturnType]) {
		case '@':
		case '#':
			return [self valueForUndefinedKey: key];
	}

	switch (nextType(&typeEncoding)) {
		}
	}

	if ([methodSignature numberOfArguments] != 2 ||
	    *[methodSignature argumentTypeAtIndex: 0] != '@' ||
	    *[methodSignature argumentTypeAtIndex: 1] != ':')
		return [self valueForUndefinedKey: key];

	switch (*[methodSignature methodReturnType]) {
	case '@':
	case '#':
		ret = [self performSelector: selector];
		break;
#define CASE(encoding, type, method)					  \
	case encoding:							  \
		{							  \
104
105
106
107
108
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
105
106
107
108
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







-
-
-
-















+
-
+
-







	CASE('f', float, numberWithFloat:)
	CASE('d', double, numberWithDouble:)
#undef CASE
	default:
		return [self valueForUndefinedKey: key];
	}

	if (nextType(&typeEncoding) != '@' || nextType(&typeEncoding) != ':' ||
	    *typeEncoding != 0)
		return [self valueForUndefinedKey: key];

	return ret;
}

- (id)valueForUndefinedKey: (OFString *)key
{
	@throw [OFUndefinedKeyException exceptionWithObject: self
							key: key];
}

- (void)setValue: (id)value
	  forKey: (OFString *)key
{
	size_t keyLength;
	char *name;
	SEL selector;
	OFMethodSignature *methodSignature;
	const char *typeEncoding;
	const char *valueType;
	char valueType;

	if ((keyLength = [key UTF8StringLength]) < 1) {
		[self	 setValue: value
		  forUndefinedKey: key];
		return;
	}

148
149
150
151
152
153
154
155

156


157
158



159
160
161
162
163
164
165

166
167
168
169
170
171


172
173
174
175
176
177

178
179
180
181
182
183
184
145
146
147
148
149
150
151

152
153
154
155


156
157
158
159
160
161
162
163


164






165
166

167
168
169
170

171
172
173
174
175
176
177
178







-
+

+
+
-
-
+
+
+





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




-
+







		name[3] = of_ascii_toupper(name[3]);

		selector = sel_registerName(name);
	} @finally {
		free(name);
	}

	typeEncoding = [self typeEncodingForSelector: selector];
	methodSignature = [self methodSignatureForSelector: selector];

	if (methodSignature == nil ||
	    [methodSignature numberOfArguments] != 3 ||
	if (typeEncoding == NULL || nextType(&typeEncoding) != 'v' ||
	    nextType(&typeEncoding) != '@' || nextType(&typeEncoding) != ':') {
	    *[methodSignature methodReturnType] != 'v' ||
	    *[methodSignature argumentTypeAtIndex: 0] != '@' ||
	    *[methodSignature argumentTypeAtIndex: 1] != ':') {
		[self    setValue: value
		  forUndefinedKey: key];
		return;
	}

	valueType = nextType(&typeEncoding);

	valueType = [methodSignature argumentTypeAtIndex: 2];
	if (*typeEncoding != 0) {
		[self    setValue: value
		  forUndefinedKey: key];
		return;
	}


	if (*valueType != '@' && *valueType != '#' && value == nil) {
	if (valueType != '@' && valueType != '#' && value == nil) {
		[self setNilValueForKey: key];
		return;
	}

	switch (valueType) {
	switch (*valueType) {
	case '@':
	case '#':
		{
			void (*setter)(id, SEL, id) = (void (*)(id, SEL, id))
			    [self methodForSelector: selector];
			setter(self, selector, value);
		}