This repository was archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNULDBUtilities.mm
More file actions
80 lines (55 loc) · 1.96 KB
/
NULDBUtilities.mm
File metadata and controls
80 lines (55 loc) · 1.96 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
//
// NULDBUtilities.m
// NULevelDB
//
// Created by Brent Gulanowski on 11-08-10.
// Copyright 2011 Nulayer Inc. All rights reserved.
//
#import "NULDBUtilities.h"
Class stringClass;
Class dataClass;
Class dictClass;
Class arrayClass;
NSData *NULDBEncodedObject(id<NSCoding>object) {
char type = 'o';
if([(id)object isKindOfClass:stringClass]) type = 's';
else if([(id)object isKindOfClass:dataClass]) type = 'd';
else if([(id)object isKindOfClass:dictClass] || [(id)object isKindOfClass:arrayClass]) type = 'h';
NSMutableData *d = [NSMutableData dataWithBytes:&type length:1];
switch (type) {
case 's':
[d appendData:[(NSString *)object dataUsingEncoding:NSUTF8StringEncoding]];
break;
case 'd':
[d appendData:(NSData *)object];
break;
case 'h':
[d appendData:[NSPropertyListSerialization dataWithPropertyList:(id)object format:NSPropertyListBinaryFormat_v1_0 options:0 error:NULL]];
break;
default:
[d appendData:[NSKeyedArchiver archivedDataWithRootObject:object]];
break;
}
return d;
}
extern id NULDBDecodedObject(NSData *data) {
NSData *value = [data subdataWithRange:NSMakeRange(1, [data length] - 1)];
char type;
[data getBytes:&type length:1];
switch (type) {
case 's':
return [[[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding] autorelease];
break;
case 'd':
return value;
break;
case 'h':
return [NSPropertyListSerialization propertyListWithData:value options:NSPropertyListImmutable format:NULL error:NULL];
break;
default:
break;
}
return [NSKeyedUnarchiver unarchiveObjectWithData:value];
}
@implementation NULDBUtilities
@end