Skip to content

Commit 748097d

Browse files
committed
20210310
1 parent 6ee2c2d commit 748097d

File tree

14 files changed

+1009
-41
lines changed

14 files changed

+1009
-41
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ cwpackModuleTest
1212
json2cwpack2json
1313

1414
# Data files
15-
*.msgpack
1615
*.msgpack.json

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,15 @@ CWPack has no dependencies to other libraries.
7777
## Test
7878
7979
Included in the test folder are a module test and a performance test and shell scripts to run them.
80+
81+
# Objective-C
82+
83+
CWPack also contains an Objective-C interface. The MessagePack home page example would look as:
84+
85+
```C
86+
CWPackContext *pc = [CWPackContext newWithContext:my_cw_pack_context];
87+
[pc packObject:@{@"compact":@YES, @"schema":@0}];
88+
89+
CWUnpackContext *uc = [CWUnpackContext newWithContext:my_cw_unpack_context];
90+
NSDictionary *dict = [uc unpackNextObject];
91+
```

goodies/dump/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Syntax:
77
cwpack_dump [-t 9] [-v][-r] [-h] < msgpackFile > humanReadableFile
88
-t 9 Tab size
99
-v Version
10-
-r Recognize records
10+
-r Recognize records
1111
-h Help
1212

1313
Each topmost msgpack item in the file starts on a new line. Each line starts with a file offset (hex) of the first item on the line.
@@ -39,11 +39,15 @@ and `cwpack_dump -t 4 < testdump.msgpack` prints:
3939
The -r option makes dump recognize Objective-C objects. `cwpack_dump < testdump2.msgpack` prints:
4040

4141
```
42-
0 [(127,<01>) "MyClass" [(127,<02>) "MyClass" [(127,<01>)]]]
42+
0 [(127,<ff>) [[(127,<ff>)]]]
43+
9 [(127,<01>) "MyClass" 10 [(127,<02>) "MyClass" 20 [(127,<01>)]]]
44+
27
4345
```
4446
and `cwpack_dump -r < testdump2.msgpack` prints
4547

4648
```
47-
0 1->MyClass(2->MyClass(->1))
49+
0 -1->[->-1]
50+
9 1->MyClass(10 2->MyClass(20 ->1))
51+
27
4852
```
4953

goodies/dump/cwpack_dump

-100 KB
Binary file not shown.

goodies/dump/cwpack_dump.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* CWPack/goodies - cwpack_dump.c */
1+
2+
/* CWPack/goodies/dump - cwpack_dump.c */
3+
24
/*
35
The MIT License (MIT)
46
@@ -30,20 +32,22 @@
3032
char tabString[21] = " ";
3133
bool recognizeObjects = false;
3234

33-
#define NEW_LINE {printf ("\n%6x ",(unsigned)(context->current - context->start)); for (ti=0; ti<tabLevel; ti++) printf ("%s",tabString);}
34-
#define CHECK_NEW_LINE if(*tabString) NEW_LINE else if (i) printf(" ")
35+
#define NEW_LINE(tablevel) {printf ("\n%6x ",(unsigned)(context->current - context->start)); for (ti=0; ti<tablevel; ti++) printf ("%s",tabString);}
36+
#define CHECK_NEW_LINE if(*tabString) NEW_LINE(tabLevel) else if (i) printf(" ")
3537

3638
/******************************* DUMP NEXT ITEM **********************************/
3739

3840
static void dump_as_hex(const void* area, long length)
3941
{
4042
unsigned int i;
4143
unsigned char c;
44+
printf("<");
4245
for (i=0; i < length; i++)
4346
{
4447
c = ((unsigned char*)area)[i];
4548
printf("%02x",c);
4649
}
50+
printf(">");
4751
}
4852
static void dump_item( cw_unpack_context* context, int tabLevel);
4953

@@ -62,8 +66,6 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
6266
struct tm tm;
6367
char s[128];
6468

65-
if (!tabLevel) NEW_LINE;
66-
6769
switch (context->item.type)
6870
{
6971
case CWP_ITEM_NIL:
@@ -121,9 +123,7 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
121123
break;}
122124

123125
case CWP_ITEM_BIN:
124-
printf("<");
125126
dump_as_hex (context->item.as.bin.start, context->item.as.bin.length);
126-
printf(">");
127127
break;
128128

129129
case CWP_ITEM_ARRAY:
@@ -148,7 +148,7 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
148148
break;
149149
}
150150
if (label)
151-
printf("%ld->",labs(label));
151+
printf("%ld->",label);
152152
if (!userObject)
153153
{
154154
if (dim != 2)
@@ -168,19 +168,20 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
168168
}
169169
printf("%.*s(",context->item.as.str.length, context->item.as.str.start);
170170
tabLevel++;
171-
for (i = 2; i < dim; i++)
171+
for (i = 0; i < dim-2; i++)
172172
{
173173
CHECK_NEW_LINE;
174174
dump_next_item(context,tabLevel);
175175
}
176176
tabLevel--;
177-
if(*tabString) NEW_LINE;
177+
if(*tabString) NEW_LINE(tabLevel);
178178
printf(")");
179179
}
180180
else
181181
{
182182
printf("[");
183183
tabLevel++;
184+
i = 0;
184185
CHECK_NEW_LINE;
185186
dump_item(context,tabLevel);
186187
for (i = 1; i < dim; i++)
@@ -189,7 +190,7 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
189190
dump_next_item(context,tabLevel);
190191
}
191192
tabLevel--;
192-
if(*tabString) NEW_LINE;
193+
if(*tabString) NEW_LINE(tabLevel);
193194
printf("]");
194195
}
195196
break;
@@ -203,11 +204,11 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
203204
{
204205
CHECK_NEW_LINE;
205206
dump_next_item(context,tabLevel);
206-
printf(": ");
207+
printf(":");
207208
dump_next_item(context,tabLevel);
208209
}
209210
tabLevel--;
210-
if(*tabString) NEW_LINE;
211+
if(*tabString) NEW_LINE(tabLevel);
211212
printf("}");
212213
break;
213214

@@ -288,6 +289,8 @@ int main(int argc, const char * argv[])
288289

289290
while (!context->return_code)
290291
{
292+
int ti;
293+
NEW_LINE(0);
291294
dump_next_item(context,0);
292295
}
293296
printf("\n");

goodies/dump/testdump.msgpack

73 Bytes
Binary file not shown.

goodies/dump/testdump2.msgpack

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
����������MyClass
2+
���MyClass��

goodies/objC/CWPackContext.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
/* CWPack/goodies/ObjC - CWPackContext.h */
3+
4+
/*
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2021 Claes Wihlborg
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
10+
software and associated documentation files (the "Software"), to deal in the Software
11+
without restriction, including without limitation the rights to use, copy, modify,
12+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
13+
persons to whom the Software is furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all copies or
16+
substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
19+
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
26+
#import <Foundation/Foundation.h>
27+
#include "cwpack.h"
28+
29+
NS_ASSUME_NONNULL_BEGIN
30+
31+
32+
33+
34+
35+
@interface CWPackContext : NSObject
36+
37+
@property (readonly) cw_pack_context *context;
38+
@property (readwrite) BOOL useLabels; // default NO
39+
40+
+ (instancetype) newWithContext:(cw_pack_context*)context;
41+
- (void) packObject:(nullable NSObject*)object;
42+
43+
@end
44+
45+
46+
47+
48+
49+
@interface CWUnpackContext : NSObject
50+
51+
@property (readonly) cw_unpack_context *context;
52+
53+
+ (instancetype) newWithContext:(cw_unpack_context*)context;
54+
- (id) unpackNextObject;
55+
56+
@end
57+
58+
59+
60+
61+
@protocol CWPackable <NSObject>
62+
63+
@required
64+
@property (readonly) int persistentAttributeCount;
65+
- (void) cwPackSub:(CWPackContext*)ctx;
66+
- (void) cwUnpackSub:(CWUnpackContext*)ctx remainingAttributes:(int)remainingAttributes;
67+
68+
@optional
69+
- (instancetype) cwUnpackSubInit:(CWUnpackContext*)ctx remainingAttributes:(int)remainingAttributes;
70+
71+
@end
72+
73+
74+
75+
76+
@interface CWPackExternalItem : NSObject
77+
78+
@property (readonly) int type;
79+
@property (readwrite, strong) NSData* data;
80+
81+
+ (instancetype) itemWithType:(int)type data:(NSData*)data;
82+
83+
@end
84+
85+
86+
87+
88+
@interface CWPackGenericClass : NSObject <CWPackable>
89+
90+
@property (readwrite,strong) NSString *packerClassName;
91+
@property (readwrite,strong) NSMutableArray *attributes;
92+
93+
+ (instancetype) newWithClassName:(NSString*)className;
94+
@end
95+
96+
97+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)