Skip to content

Commit ee9a620

Browse files
author
Chris Hulbert
committed
Packer
1 parent 1abff29 commit ee9a620

12 files changed

+226
-71
lines changed

MessagePack.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
// Copyright 2011 Media Innovations
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
15-
// MessagePack.h
16-
// This file is to be your main include point for access to this framework
171
#import "NSData+MessagePack.h"
2+
#import "NSDictionary+MessagePack.h"
3+
#import "NSArray+MessagePack.h"

MessagePackPacker.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MessagePackPacker.h
3+
// Fetch TV Remote
4+
//
5+
// Created by Chris Hulbert on 13/10/11.
6+
// Copyright (c) 2011 Digital Five. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface MessagePackPacker : NSObject
12+
13+
// Given an array or dictionary, this messagepacks it
14+
+ (NSData*)pack:(id)obj;
15+
16+
@end

MessagePackPacker.m

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// MessagePackPacker.m
3+
// Fetch TV Remote
4+
//
5+
// Created by Chris Hulbert on 13/10/11.
6+
// Copyright (c) 2011 Digital Five. All rights reserved.
7+
//
8+
9+
#import "MessagePackPacker.h"
10+
#include "msgpack_src/msgpack.h"
11+
12+
@implementation MessagePackPacker
13+
14+
// Pack a single number, figuring out which type of number it is
15+
+ (void)packNumber:(NSNumber*)num into:(msgpack_packer*)pk {
16+
CFNumberType numberType = CFNumberGetType((CFNumberRef)num);
17+
switch (numberType) {
18+
case kCFNumberSInt8Type:
19+
msgpack_pack_int8(pk, num.shortValue);
20+
break;
21+
case kCFNumberSInt16Type:
22+
case kCFNumberShortType:
23+
msgpack_pack_int16(pk, num.shortValue);
24+
break;
25+
case kCFNumberSInt32Type:
26+
case kCFNumberIntType:
27+
case kCFNumberLongType:
28+
case kCFNumberCFIndexType:
29+
case kCFNumberNSIntegerType:
30+
msgpack_pack_int32(pk, num.intValue);
31+
break;
32+
case kCFNumberSInt64Type:
33+
case kCFNumberLongLongType:
34+
msgpack_pack_int64(pk, num.longLongValue);
35+
break;
36+
case kCFNumberFloat32Type:
37+
case kCFNumberFloatType:
38+
case kCFNumberCGFloatType:
39+
msgpack_pack_int8(pk, num.floatValue);
40+
break;
41+
case kCFNumberFloat64Type:
42+
case kCFNumberDoubleType:
43+
msgpack_pack_int8(pk, num.doubleValue);
44+
break;
45+
case kCFNumberCharType: {
46+
int theValue = num.intValue;
47+
if (theValue == 0)
48+
msgpack_pack_false(pk);
49+
else if (theValue == 1)
50+
msgpack_pack_true(pk);
51+
else
52+
msgpack_pack_int16(pk, theValue);
53+
}
54+
break;
55+
default:
56+
NSLog(@"Could not messagepack number, cannot recognise type: %@", num);
57+
}
58+
}
59+
60+
// Pack a single object into the given packer
61+
+ (void)packObject:(id)obj into:(msgpack_packer*)pk {
62+
if ([obj isKindOfClass:[NSArray class]]) {
63+
msgpack_pack_array(pk, ((NSArray*)obj).count);
64+
for (id arrayElement in obj) {
65+
[self packObject:arrayElement into:pk];
66+
}
67+
} else if ([obj isKindOfClass:[NSDictionary class]]) {
68+
msgpack_pack_map(pk, ((NSDictionary*)obj).count);
69+
for(id key in obj) {
70+
[self packObject:key into:pk];
71+
[self packObject:[obj objectForKey:key] into:pk];
72+
}
73+
} else if ([obj isKindOfClass:[NSString class]]) {
74+
const char *str = ((NSString*)obj).UTF8String;
75+
int len = strlen(str);
76+
msgpack_pack_raw(pk, len);
77+
msgpack_pack_raw_body(pk, str, len);
78+
} else if ([obj isKindOfClass:[NSNumber class]]) {
79+
[self packNumber:obj into:pk];
80+
} else if (obj==[NSNull null]) {
81+
msgpack_pack_nil(pk);
82+
} else {
83+
NSLog(@"Could not messagepack object: %@", obj);
84+
}
85+
}
86+
87+
// Given an array or dictionary, this messagepacks it
88+
+ (NSData*)pack:(id)obj {
89+
// Creates buffer and serializer instance
90+
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
91+
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
92+
93+
// Pack the root array or dictionary node, which recurses through the rest
94+
[self packObject:obj into:pk];
95+
96+
// Bridge the data back to obj-c's world
97+
NSData* data = [NSData dataWithBytes:buffer->data length:buffer->size];
98+
99+
// Free
100+
msgpack_sbuffer_free(buffer);
101+
msgpack_packer_free(pk);
102+
103+
return data;
104+
}
105+
106+
@end

MessagePackParser.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
// Copyright 2011 Media Innovations
21
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
152
// MessagePackParser.h
3+
// Fetch TV Remote
4+
//
165
// Created by Chris Hulbert on 23/06/11.
6+
// Copyright 2011 Digital Five. All rights reserved.
7+
//
178

189
#import <Foundation/Foundation.h>
1910

2011
@interface MessagePackParser : NSObject
2112

2213
+ (id)parseData:(NSData*)data;
14+
+ (NSData*)pack:(id)obj;
2315

2416
@end

MessagePackParser.m

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
// Copyright 2011 Media Innovations
21
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
2+
// MessagePackParser.m
3+
// Fetch TV Remote
64
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
5+
// Created by Chris Hulbert on 23/06/11.
6+
// Copyright 2011 Digital Five. All rights reserved.
87
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
15-
// MessagePackParser.m
16-
// Created by Sugendran Ganess and Chris Hulbert
178

189
#import "MessagePackParser.h"
1910
#include "msgpack_src/msgpack.h"

NSArray+MessagePack.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSArray+MessagePack.h
3+
// Fetch TV Remote
4+
//
5+
// Created by Chris Hulbert on 13/10/11.
6+
// Copyright (c) 2011 Digital Five. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
// Adds MessagePack packing to NSArray
12+
@interface NSArray (NSArray_MessagePack)
13+
14+
// Packs the receiver's data into message pack data
15+
- (NSData*)messagePack;
16+
17+
@end

NSArray+MessagePack.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// NSArray+MessagePack.m
3+
// Fetch TV Remote
4+
//
5+
// Created by Chris Hulbert on 13/10/11.
6+
// Copyright (c) 2011 Digital Five. All rights reserved.
7+
//
8+
9+
#import "NSArray+MessagePack.h"
10+
#import "MessagePackPacker.h"
11+
12+
@implementation NSArray (NSArray_MessagePack)
13+
14+
// Packs the receiver's data into message pack data
15+
- (NSData*)messagePack {
16+
return [MessagePackPacker pack:self];
17+
}
18+
19+
@end

NSData+MessagePack.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
// Copyright 2011 Media Innovations
21
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
152
// NSData+MessagePack.h
3+
// Fetch TV Remote
4+
//
165
// Created by Chris Hulbert on 23/06/11.
6+
// Copyright 2011 Digital Five. All rights reserved.
7+
//
178

189
#import <Foundation/Foundation.h>
1910

NSData+MessagePack.m

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
// Copyright 2011 Media Innovations
21
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
152
// NSData+MessagePack.m
3+
// Fetch TV Remote
4+
//
165
// Created by Chris Hulbert on 23/06/11.
6+
// Copyright 2011 Digital Five. All rights reserved.
7+
//
178

189
#import "NSData+MessagePack.h"
10+
1911
#import "MessagePackParser.h"
2012

2113
@implementation NSData (NSData_MessagePack)

NSDictionary+MessagePack.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSDictionary+MessagePack.h
3+
// Fetch TV Remote
4+
//
5+
// Created by Chris Hulbert on 13/10/11.
6+
// Copyright (c) 2011 Digital Five. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
// Adds MessagePack packing to NSDictionary
12+
@interface NSDictionary (NSDictionary_MessagePack)
13+
14+
// Packs the receiver's data into message pack data
15+
- (NSData*)messagePack;
16+
17+
@end

0 commit comments

Comments
 (0)