Skip to content

Commit

Permalink
20210304
Browse files Browse the repository at this point in the history
  • Loading branch information
clwi committed Mar 5, 2021
1 parent 4320cb8 commit 4f4464c
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 29 deletions.
2 changes: 1 addition & 1 deletion goodies/basic-contexts/basic_contexts.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void init_file_pack_context (file_pack_context* fpc, unsigned long initial_buffe
fpc->pc.return_code = CWP_RC_MALLOC_ERROR;
return;
}
fpc->fileDescriptor = fileDescriptor;

fpc->fileDescriptor = fileDescriptor;
fpc->barrier = NULL;

Expand Down
21 changes: 16 additions & 5 deletions goodies/dump/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
Dump is a small program taking a msgpack file as input and produces a human readable file as output. The output is not json as msgpack is more feature-rich.

Syntax:
cwpack_dump [-t 9] [-v] [-h] < msgpackFile > humanReadableFile
cwpack_dump [-t 9] [-v][-r] [-h] < msgpackFile > humanReadableFile
-t 9 Tab size
-v Version
-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 All @@ -17,10 +18,10 @@ If Tab size isn't given, structures are written on a single line.

```
0 [10000000 3.14 "åäöÅÄÖ"]
1c {"binary": (62696e617279) "extension": (-5,68656c6c6f) "time": '2020-05-20 18:40:00'}
1c {"binary": <62696e617279> "extension": (-5,<68656c6c6f>) "time": '2020-05-20 18:40:00'}
49
```
and `cwpack_dump -t 4 < testdump.msgpack` prints:
and `cwpack_dump -t 4 < testdump.msgpack` prints:

```
0 [
Expand All @@ -29,10 +30,20 @@ and `cwpack_dump -t 4 < testdump.msgpack` prints:
f "åäöÅÄÖ"
1c ]
1c {
1d "binary": (62696e617279)
2c "extension": (-5,68656c6c6f)
1d "binary": <62696e617279>
2c "extension": (-5,<68656c6c6f>)
3e "time": '2020-05-20 18:40:00'
49 }
49
```
The -r option makes dump recognize Objective-C objects. `cwpack_dump < testdump2.msgpack` prints:

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

```
0 1->MyClass(2->MyClass(->1))
```

Binary file added goodies/dump/cwpack_dump
Binary file not shown.
104 changes: 86 additions & 18 deletions goodies/dump/cwpack_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#include <unistd.h>
#include <string.h>
#include "basic_contexts.h"
#include "numeric_extensions.h"

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(" ")
Expand All @@ -43,9 +45,16 @@ static void dump_as_hex(const void* area, long length)
printf("%02x",c);
}
}

static void dump_item( cw_unpack_context* context, int tabLevel);

static void dump_next_item( cw_unpack_context* context, int tabLevel)
{
cw_unpack_next (context);
if (context->return_code) return;
dump_item (context, tabLevel);
}

static void dump_item( cw_unpack_context* context, int tabLevel)
{

long long dim =99;
Expand All @@ -55,20 +64,18 @@ static void dump_next_item( cw_unpack_context* context, int tabLevel)
char s[128];

if (!tabLevel) NEW_LINE;
cw_unpack_next (context);
if (context->return_code) return;

switch (context->item.type)
{
case CWP_ITEM_NIL:
printf("null");
printf("nil");
break;

case CWP_ITEM_BOOLEAN:
if (context->item.as.boolean)
printf("true");
printf("YES");
else
printf("false");
printf("NO");
break;

case CWP_ITEM_POSITIVE_INTEGER:
Expand Down Expand Up @@ -115,24 +122,79 @@ static void dump_next_item( cw_unpack_context* context, int tabLevel)
break;}

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

case CWP_ITEM_ARRAY:
printf("[");
{
dim = context->item.as.array.size;
tabLevel++;
for (i = 0; i < dim; i++)
if (!dim)
{
printf("[]");
break;
}

cw_unpack_next (context);
if (context->return_code) break;

if (recognizeObjects && (context->item.type == 127))
{
long label = get_ext_integer(context);
bool userObject = label >= 0;
if (dim == 1) /* reference */
{
printf("->%ld",label);
break;
}
if (label)
printf("%ld->",labs(label));
if (!userObject)
{
if (dim != 2)
{
context->return_code = CWP_RC_MALFORMED_INPUT;
break;
}
dump_next_item(context,tabLevel);
break;
}
cw_unpack_next (context);
if (context->return_code) break;
if (context->item.type != CWP_ITEM_STR)
{
context->return_code = CWP_RC_MALFORMED_INPUT;
break;
}
printf("%.*s(",context->item.as.str.length, context->item.as.str.start);
tabLevel++;
for (i = 2; i < dim; i++)
{
CHECK_NEW_LINE;
dump_next_item(context,tabLevel);
}
tabLevel--;
if(*tabString) NEW_LINE;
printf(")");
}
else
{
printf("[");
tabLevel++;
CHECK_NEW_LINE;
dump_next_item(context,tabLevel);
dump_item(context,tabLevel);
for (i = 1; i < dim; i++)
{
CHECK_NEW_LINE;
dump_next_item(context,tabLevel);
}
tabLevel--;
if(*tabString) NEW_LINE;
printf("]");
}
tabLevel--;
if(*tabString) NEW_LINE;
printf("]");
break;
}

case CWP_ITEM_MAP:
printf("{");
Expand All @@ -152,7 +214,8 @@ static void dump_next_item( cw_unpack_context* context, int tabLevel)

case CWP_ITEM_TIMESTAMP:
printf("'");
gmtime_r(&context->item.as.time.tv_sec,&tm);
time_t tv_sec = context->item.as.time.tv_sec;
gmtime_r(&tv_sec,&tm);
strftime(s,128,"%F %T", &tm);
printf("%s",s);
if (context->item.as.time.tv_nsec)
Expand Down Expand Up @@ -200,12 +263,17 @@ int main(int argc, const char * argv[])
printf("cwpack_dump version = 1.0\n");
exit(0);
}
else if (!strcmp(argv[i],"-r"))
{
recognizeObjects = true;
}
else
{
printf("cwpack_dump [-t 9] [-v] [-h]\n");
printf("cwpack_dump [-t 9] [-r] [-v] [-h]\n");
printf("-h Help\n");
printf("-r Recognize records\n");
printf("-t 9 Tab size\n");
printf("-v Version\n");
printf("-h Help\n");
printf("\nIf Tab size isn't given, structures are written on a single line\n");
printf("\nInput is taken from stdin and output is written to stdout\n");
exit(0);
Expand Down
2 changes: 1 addition & 1 deletion goodies/dump/runCWpack_dump.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
clang -ansi -I ../../src/ -I ../basic-contexts/ -o cwpack_dump *.c ../../src/cwpack.c ../basic-contexts/*.c
clang -ansi -I ../../src/ -I ../basic-contexts/ -I ../numeric-extensions/ -o cwpack_dump *.c ../../src/cwpack.c ../basic-contexts/*.c ../numeric-extensions/numeric_extensions.c
./cwpack_dump < testdump.msgpack
./cwpack_dump -t 4 < testdump.msgpack
3 changes: 3 additions & 0 deletions goodies/numeric-extensions/numeric_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ int64_t get_ext_integer (cw_unpack_context* unpack_context)
}

switch (unpack_context->item.as.ext.length) {
case 0:
return 0;

case 1:
return *(int8_t*)unpack_context->item.as.ext.start;

Expand Down
2 changes: 1 addition & 1 deletion goodies/objC/cwpack_objc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
- (void) packIn:(cw_pack_context*) buff;

/* *********************** U N P A C K *************************/
- (id) initFromContext:(cw_unpack_context*) buff;
+ (instancetype) unpackFrom:(cw_unpack_context*) buff;

@end
33 changes: 30 additions & 3 deletions goodies/objC/cwpack_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ software and associated documentation files (the "Software"), to deal in the Sof
#import "cwpack_objc.h"
#import "cwpack_utils.h"

#define CWP_ITEM_CLASS_NAME 127


id cwObjectFromBuffer (cw_unpack_context* inbuf);

Expand All @@ -32,8 +34,8 @@ @implementation NSObject (cwPack)

- (void) packIn:(cw_pack_context*) buff
{
buff->return_code = CWP_RC_ILLEGAL_CALL; // No pack defined for this object type
[NSException raise:@"[NSObject packIn:]" format:@"PackIn not defined for class: %@", [self class]];
const char *className= object_getClassName(self);
cw_pack_ext (buff, CWP_ITEM_CLASS_NAME, className, (uint32_t)strlen(className));
}


Expand Down Expand Up @@ -61,6 +63,16 @@ + (instancetype) unpackFrom:(cw_unpack_context*) buff
return result;
}


- (id) initFromContext:(cw_unpack_context*) buff
{
self = [self init]; // satisfy compiler
buff->return_code = CWP_RC_ILLEGAL_CALL; // No unpack defined for this object type
[NSException raise:@"Not defined" format:@"[%@ initFromContext:]", [self class]];
return nil;
}


@end


Expand Down Expand Up @@ -192,6 +204,8 @@ - (void) packIn:(cw_pack_context*) buff
NSTimeInterval ti = self.timeIntervalSince1970;
cw_pack_time_interval (buff, ti);
}


@end


Expand Down Expand Up @@ -227,7 +241,7 @@ id cwObjectFromBuffer (cw_unpack_context* inbuf)
return [NSNumber numberWithDouble:inbuf->item.as.long_real];

case CWP_ITEM_STR:
return [[[NSString alloc] initWithBytes:inbuf->item.as.str.start length:inbuf->item.as.str.length encoding:NSUTF8StringEncoding] autorelease];
return [[NSString alloc] initWithBytes:inbuf->item.as.str.start length:inbuf->item.as.str.length encoding:NSUTF8StringEncoding];

case CWP_ITEM_BIN:
return [NSData dataWithBytes:inbuf->item.as.bin.start length:inbuf->item.as.bin.length];
Expand Down Expand Up @@ -263,6 +277,19 @@ id cwObjectFromBuffer (cw_unpack_context* inbuf)
{
return [NSDate dateWithTimeIntervalSince1970:inbuf->item.as.time.tv_sec + inbuf->item.as.time.tv_nsec / 1000000000.0];
}

case CWP_ITEM_CLASS_NAME:
{
NSString *cName = [[NSString alloc] initWithBytes:inbuf->item.as.ext.start length:inbuf->item.as.ext.length encoding:NSUTF8StringEncoding];
Class objectClass = NSClassFromString(cName);
if (objectClass == NULL)
{
[NSException raise:@"cwObjectFromBuffer" format:@"Class not defined for class: %@", cName];
}
else
return [[objectClass alloc] initFromContext:inbuf];
}


default:
return nil;
Expand Down
Loading

0 comments on commit 4f4464c

Please sign in to comment.