Skip to content

Commit

Permalink
20210310
Browse files Browse the repository at this point in the history
  • Loading branch information
clwi committed Mar 10, 2021
1 parent 6ee2c2d commit 748097d
Show file tree
Hide file tree
Showing 14 changed files with 1,009 additions and 41 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ cwpackModuleTest
json2cwpack2json

# Data files
*.msgpack
*.msgpack.json
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ CWPack has no dependencies to other libraries.
## Test
Included in the test folder are a module test and a performance test and shell scripts to run them.
# Objective-C
CWPack also contains an Objective-C interface. The MessagePack home page example would look as:
```C
CWPackContext *pc = [CWPackContext newWithContext:my_cw_pack_context];
[pc packObject:@{@"compact":@YES, @"schema":@0}];
CWUnpackContext *uc = [CWUnpackContext newWithContext:my_cw_unpack_context];
NSDictionary *dict = [uc unpackNextObject];
```
10 changes: 7 additions & 3 deletions goodies/dump/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Syntax:
cwpack_dump [-t 9] [-v][-r] [-h] < msgpackFile > humanReadableFile
-t 9 Tab size
-v Version
-r Recognize records
-r Recognize records
-h Help

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.
Expand Down Expand Up @@ -39,11 +39,15 @@ and `cwpack_dump -t 4 < testdump.msgpack` prints:
The -r option makes dump recognize Objective-C objects. `cwpack_dump < testdump2.msgpack` prints:

```
0 [(127,<01>) "MyClass" [(127,<02>) "MyClass" [(127,<01>)]]]
0 [(127,<ff>) [[(127,<ff>)]]]
9 [(127,<01>) "MyClass" 10 [(127,<02>) "MyClass" 20 [(127,<01>)]]]
27
```
and `cwpack_dump -r < testdump2.msgpack` prints

```
0 1->MyClass(2->MyClass(->1))
0 -1->[->-1]
9 1->MyClass(10 2->MyClass(20 ->1))
27
```

Binary file removed goodies/dump/cwpack_dump
Binary file not shown.
29 changes: 16 additions & 13 deletions goodies/dump/cwpack_dump.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* CWPack/goodies - cwpack_dump.c */

/* CWPack/goodies/dump - cwpack_dump.c */

/*
The MIT License (MIT)
Expand Down Expand Up @@ -30,20 +32,22 @@
char tabString[21] = " ";
bool recognizeObjects = false;

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

/******************************* DUMP NEXT ITEM **********************************/

static void dump_as_hex(const void* area, long length)
{
unsigned int i;
unsigned char c;
printf("<");
for (i=0; i < length; i++)
{
c = ((unsigned char*)area)[i];
printf("%02x",c);
}
printf(">");
}
static void dump_item( cw_unpack_context* context, int tabLevel);

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

if (!tabLevel) NEW_LINE;

switch (context->item.type)
{
case CWP_ITEM_NIL:
Expand Down Expand Up @@ -121,9 +123,7 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
break;}

case CWP_ITEM_BIN:
printf("<");
dump_as_hex (context->item.as.bin.start, context->item.as.bin.length);
printf(">");
break;

case CWP_ITEM_ARRAY:
Expand All @@ -148,7 +148,7 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
break;
}
if (label)
printf("%ld->",labs(label));
printf("%ld->",label);
if (!userObject)
{
if (dim != 2)
Expand All @@ -168,19 +168,20 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
}
printf("%.*s(",context->item.as.str.length, context->item.as.str.start);
tabLevel++;
for (i = 2; i < dim; i++)
for (i = 0; i < dim-2; i++)
{
CHECK_NEW_LINE;
dump_next_item(context,tabLevel);
}
tabLevel--;
if(*tabString) NEW_LINE;
if(*tabString) NEW_LINE(tabLevel);
printf(")");
}
else
{
printf("[");
tabLevel++;
i = 0;
CHECK_NEW_LINE;
dump_item(context,tabLevel);
for (i = 1; i < dim; i++)
Expand All @@ -189,7 +190,7 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
dump_next_item(context,tabLevel);
}
tabLevel--;
if(*tabString) NEW_LINE;
if(*tabString) NEW_LINE(tabLevel);
printf("]");
}
break;
Expand All @@ -203,11 +204,11 @@ static void dump_item( cw_unpack_context* context, int tabLevel)
{
CHECK_NEW_LINE;
dump_next_item(context,tabLevel);
printf(": ");
printf(":");
dump_next_item(context,tabLevel);
}
tabLevel--;
if(*tabString) NEW_LINE;
if(*tabString) NEW_LINE(tabLevel);
printf("}");
break;

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

while (!context->return_code)
{
int ti;
NEW_LINE(0);
dump_next_item(context,0);
}
printf("\n");
Expand Down
Binary file added goodies/dump/testdump.msgpack
Binary file not shown.
2 changes: 2 additions & 0 deletions goodies/dump/testdump2.msgpack
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
����������MyClass
���MyClass��
97 changes: 97 additions & 0 deletions goodies/objC/CWPackContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

/* CWPack/goodies/ObjC - CWPackContext.h */

/*
The MIT License (MIT)
Copyright (c) 2021 Claes Wihlborg
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


#import <Foundation/Foundation.h>
#include "cwpack.h"

NS_ASSUME_NONNULL_BEGIN





@interface CWPackContext : NSObject

@property (readonly) cw_pack_context *context;
@property (readwrite) BOOL useLabels; // default NO

+ (instancetype) newWithContext:(cw_pack_context*)context;
- (void) packObject:(nullable NSObject*)object;

@end





@interface CWUnpackContext : NSObject

@property (readonly) cw_unpack_context *context;

+ (instancetype) newWithContext:(cw_unpack_context*)context;
- (id) unpackNextObject;

@end




@protocol CWPackable <NSObject>

@required
@property (readonly) int persistentAttributeCount;
- (void) cwPackSub:(CWPackContext*)ctx;
- (void) cwUnpackSub:(CWUnpackContext*)ctx remainingAttributes:(int)remainingAttributes;

@optional
- (instancetype) cwUnpackSubInit:(CWUnpackContext*)ctx remainingAttributes:(int)remainingAttributes;

@end




@interface CWPackExternalItem : NSObject

@property (readonly) int type;
@property (readwrite, strong) NSData* data;

+ (instancetype) itemWithType:(int)type data:(NSData*)data;

@end




@interface CWPackGenericClass : NSObject <CWPackable>

@property (readwrite,strong) NSString *packerClassName;
@property (readwrite,strong) NSMutableArray *attributes;

+ (instancetype) newWithClassName:(NSString*)className;
@end


NS_ASSUME_NONNULL_END
Loading

0 comments on commit 748097d

Please sign in to comment.