This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOItem+Binary.m
More file actions
342 lines (304 loc) · 8.34 KB
/
COItem+Binary.m
File metadata and controls
342 lines (304 loc) · 8.34 KB
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
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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#import "COItem+Binary.h"
#import "COBinaryWriter.h"
#import "COBinaryReader.h"
#import "COPath.h"
#import <EtoileFoundation/Macros.h>
typedef enum {
co_reader_expect_object_uuid,
co_reader_expect_object_schemaname,
co_reader_expect_property,
co_reader_expect_type,
co_reader_expect_value,
co_reader_error
} reader_state;
@interface COReaderState : NSObject
{
@public
ETUUID *uuid;
NSString *schemaName;
NSMutableDictionary *values;
NSMutableDictionary *types;
NSString *currentProperty;
COType currentType;
BOOL isReadingMultivalue;
id multivalue;
reader_state state;
}
@end
@implementation COReaderState
- (id) init
{
SUPERINIT;
values = [[NSMutableDictionary alloc] init];
types = [[NSMutableDictionary alloc] init];
state = co_reader_expect_object_uuid;
return self;
}
- (void) dealloc
{
[uuid release];
[values release];
[types release];
[currentProperty release];
[super dealloc];
}
@end
@implementation COItem (Binary)
static NSNull *NSNullCached;
+ (id) initialize
{
if (self == [COItem class])
{
NSNullCached = [[NSNull null] retain];
}
}
static void writePrimitiveValue(co_buffer_t *dest, id aValue, COType aType)
{
if (aValue == NSNullCached)
{
co_buffer_store_null(dest);
return;
}
switch (COPrimitiveType(aType))
{
case kCOInt64Type:
co_buffer_store_integer(dest, [aValue longLongValue]);
break;
case kCODoubleType:
co_buffer_store_double(dest, [aValue doubleValue]);
break;
case kCOStringType:
co_buffer_store_string(dest, aValue);
break;
case kCOBlobType:
co_buffer_store_bytes(dest, [aValue bytes], [aValue length]);
break;
case kCOCompositeReferenceType:
co_buffer_store_uuid(dest, aValue);
break;
case kCOReferenceType:
if ([aValue isKindOfClass: [COPath class]])
{
co_buffer_store_string(dest, [(COPath *)aValue stringValue]);
}
else
{
co_buffer_store_uuid(dest, aValue);
}
break;
case kCOAttachmentType:
co_buffer_store_bytes(dest, [aValue bytes], [aValue length]);
break;
default:
[NSException raise: NSInvalidArgumentException format: @"unknown type %d", aType];
}
}
static void writeValue(co_buffer_t *dest, id aValue, COType aType)
{
if (COTypeIsPrimitive(aType))
{
return writePrimitiveValue(dest, aValue, aType);
}
else
{
co_buffer_begin_array(dest);
for (id obj in aValue)
{
writePrimitiveValue(dest, obj, aType);
}
co_buffer_end_array(dest);
}
}
- (NSData *) dataValue
{
co_buffer_t buf;
co_buffer_init(&buf);
co_buffer_store_uuid(&buf, [self UUID]);
co_buffer_store_string(&buf, [self schemaName]);
co_buffer_begin_object(&buf);
for (NSString *prop in [self attributeNames])
{
COType type = [self typeForAttribute: prop];
id val = [self valueForAttribute: prop];
co_buffer_store_string(&buf, prop);
co_buffer_store_integer(&buf, type);
writeValue(&buf, val, type);
}
co_buffer_end_object(&buf);
NSData *result = [NSData dataWithBytes:co_buffer_get_data(&buf)
length:co_buffer_get_length(&buf)];
co_buffer_free(&buf);
return result;
}
// Read
static void co_read_object_value(COReaderState *state, id obj) {
if (state->isReadingMultivalue)
{
[state->multivalue addObject: obj];
}
else
{
[state->values setObject: obj
forKey: state->currentProperty];
state->state = co_reader_expect_property;
}
}
static void co_read_int64(void *ctx, int64_t val)
{
COReaderState *state = (COReaderState *)ctx;
switch (state->state)
{
case co_reader_expect_value:
co_read_object_value(state, [NSNumber numberWithLongLong: val]);
break;
case co_reader_expect_type:
state->currentType = (COType)val;
[state->types setObject: [NSNumber numberWithLongLong: val]
forKey: state->currentProperty];
state->state = co_reader_expect_value;
break;
default:
state->state = co_reader_error;
break;
}
}
static void co_read_double(void *ctx, double val)
{
COReaderState *state = (COReaderState *)ctx;
switch (state->state)
{
case co_reader_expect_value:
co_read_object_value(state, [NSNumber numberWithDouble: val]);
break;
default:
state->state = co_reader_error;
break;
}
}
static void co_read_string(void *ctx, NSString *val)
{
COReaderState *state = (COReaderState *)ctx;
switch (state->state)
{
case co_reader_expect_value:
co_read_object_value(state, val);
break;
case co_reader_expect_property:
ASSIGN(state->currentProperty, val);
state->state = co_reader_expect_type;
break;
case co_reader_expect_object_schemaname:
ASSIGN(state->schemaName, val);
state->state = co_reader_expect_property;
break;
default:
state->state = co_reader_error;
break;
}
}
static void co_read_uuid(void *ctx, ETUUID *uuid)
{
COReaderState *state = (COReaderState *)ctx;
switch (state->state)
{
case co_reader_expect_value:
co_read_object_value(state, uuid);
break;
case co_reader_expect_object_uuid:
ASSIGN(state->uuid, uuid);
state->state = co_reader_expect_object_schemaname;
break;
default:
state->state = co_reader_error;
break;
}
}
static void co_read_bytes(void *ctx, const unsigned char *val, size_t size)
{
COReaderState *state = (COReaderState *)ctx;
switch (state->state)
{
case co_reader_expect_value:
co_read_object_value(state, [NSData dataWithBytes: val length: size]);
break;
default:
state->state = co_reader_error;
break;
}
}
static void co_read_begin_object(void *ctx)
{
}
static void co_read_end_object(void *ctx)
{
}
static void co_read_begin_array(void *ctx)
{
COReaderState *state = (COReaderState *)ctx;
state->isReadingMultivalue = YES;
assert(COTypeIsMultivalued(state->currentType));
if (COTypeIsOrdered(state->currentType))
{
state->multivalue = [[NSMutableArray alloc] init];
}
else
{
state->multivalue = [[NSMutableSet alloc] init];
}
}
static void co_read_end_array(void *ctx)
{
COReaderState *state = (COReaderState *)ctx;
state->isReadingMultivalue = NO;
// Save the value
[state->values setObject: state->multivalue
forKey: state->currentProperty];
ASSIGN(state->multivalue, nil);
// Do state transition
state->state = co_reader_expect_property;
}
static void co_read_null(void *ctx)
{
COReaderState *state = (COReaderState *)ctx;
switch (state->state)
{
case co_reader_expect_value:
co_read_object_value(state, NSNullCached);
break;
case co_reader_expect_object_schemaname:
ASSIGN(state->schemaName, nil);
state->state = co_reader_expect_property;
break;
default:
state->state = co_reader_error;
break;
}
}
- (id) initWithData: (NSData *)aData
{
COReaderState *state = [[COReaderState alloc] init];
co_reader_callback_t cb = {
co_read_int64,
co_read_double,
co_read_string,
co_read_uuid,
co_read_bytes,
co_read_begin_object,
co_read_end_object,
co_read_begin_array,
co_read_end_array,
co_read_null
};
co_reader_read([aData bytes],
[aData length],
state,
cb);
SUPERINIT;
ASSIGN(uuid, state->uuid);
ASSIGN(types, state->types);
ASSIGN(values, state->values);
ASSIGN(schemaName, state->schemaName);
[state release];
return self;
}
@end