Skip to content

Commit

Permalink
Merge branch 'master' into ObjC
Browse files Browse the repository at this point in the history
  • Loading branch information
clwi committed Mar 5, 2021
2 parents 4f4464c + e547f67 commit 6ee2c2d
Show file tree
Hide file tree
Showing 25 changed files with 373 additions and 374 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CWPack

CWPack is a lightweight and yet complete implementation of the
[MessagePack](http://msgpack.org) serialization format
CWPack is a lightweight and yet complete implementation of the
[MessagePack](http://msgpack.org) serialization format
[version 5](https://github.com/msgpack/msgpack/blob/master/spec.md).
It also supports the Timestamp extension type.

Expand All @@ -14,7 +14,7 @@ Together with [MPack](https://github.com/ludocode/mpack), CWPack is the fastest

CWPack does no memory allocations and no file handling in its basic setup. All that is done outside of CWPack. Example extensions are included.

CWPack is working against memory buffers. User defined handlers are called when buffers are filled up (packing) or needs refill (unpack).
CWPack is working against memory buffers. User defined handlers are called when buffers are filled up (packing) or needs refill (unpack).

Containers (arrays, maps) are read/written in parts, first the item containing the size and then the contained items one by one. Exception to this is the `cw_skip_items` function which skip whole containers.

Expand Down Expand Up @@ -49,7 +49,7 @@ void example (void)
if (cw_unpack_next_str_lengh(&uc) != 6) ERROR;
if (strncmp("schema", uc.item.as.str.start, 6)) ERROR;
if (cw_unpack_next_signed32(&uc) != 0) ERROR;

if (uc.return_code != CWP_RC_OK) ERROR;
cw_unpack_next(&uc);
if (uc.return_code != CWP_RC_END_OF_INPUT) ERROR;
Expand Down
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The example contains a program that takes a json file and converts it to a messa

In the script runExample.sh the 2 json files are also diffed.

The files `item.*` contains a memory tree representation of json data and the conversion routines:
The files `item.*` contains a memory tree representation of json data and the conversion routines:

- Item Tree To Json File
- Item Tree To MessagePack File
Expand Down
76 changes: 38 additions & 38 deletions example/item.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* CWPack/example - item.c */
/*
The MIT License (MIT)
Copyright (c) 2017 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,
Expand Down Expand Up @@ -44,7 +44,7 @@ void freeItem3 (item_root* root)
{
freeItem3(ic->items[i]);
}

default:
free(root);
}
Expand All @@ -62,7 +62,7 @@ static void item32jsonFile (FILE* file, item_root* item)
char c;
unsigned u, ti;
static unsigned tabs = 0;

#define NEW_LINE {fprintf (file, "\n"); for (ti=0; ti<tabs; ti++) fprintf (file, "\t");}

switch (item->item_type)
Expand All @@ -83,7 +83,7 @@ static void item32jsonFile (FILE* file, item_root* item)
NEW_LINE
fprintf (file, "}");
break;

case ITEM_ARRAY:
jc = (item_container*)item;
fprintf (file, "[");
Expand All @@ -98,23 +98,23 @@ static void item32jsonFile (FILE* file, item_root* item)
NEW_LINE
fprintf(file, "]");
break;

case ITEM_NIL:
fprintf (file, "null");
break;

case ITEM_TRUE:
fprintf (file, "true");
break;

case ITEM_FALSE:
fprintf (file, "false");
break;

case ITEM_INTEGER:
fprintf (file, "%lld", ((item_integer*)item)->value);
break;

case ITEM_REAL:
sprintf (tmp, "%-25.15g", ((item_real*)item)->value);
for (i=0;i<30;i++)
Expand All @@ -129,7 +129,7 @@ static void item32jsonFile (FILE* file, item_root* item)
}
fprintf (file, "%s", tmp);
break;

case ITEM_STRING:
fprintf (file, "\"");
cp = ((item_string*)item)->string;
Expand Down Expand Up @@ -161,15 +161,15 @@ static void item32jsonFile (FILE* file, item_root* item)
case 0x0a: fprintf (file, "\\n"); break; /* LF */
case 0x0c: fprintf (file, "\\f"); break; /* FF */
case 0x0d: fprintf (file, "\\r"); break; /* CR */

default:
fprintf (file, "%c", c);
break;
}
}
fprintf (file, "\"");
break;

default: break;
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ static item_root* jsonString2item3 (const char** ptr)
{
scanSpace;
item_root* result = NULL;

char c = *(*ptr)++;
switch (c) {
case '{':
Expand All @@ -309,20 +309,20 @@ static item_root* jsonString2item3 (const char** ptr)
else
result = (item_root*)pullMapPair (ptr, 0);
break;

case '[':
scanSpace;
if (**ptr == ']')
result = (item_root*)allocate_container (ITEM_ARRAY, 0);
else
result = (item_root*)pullArray (ptr, 0);
break;

case '"': result = (item_root*)pullString (ptr, 0);break;
case 'n': result = allocate_item(item_root,ITEM_NIL,0); *ptr+=3;break;
case 't': result = allocate_item(item_root,ITEM_TRUE,0); *ptr+=3;break;
case 'f': result = allocate_item(item_root,ITEM_FALSE,0); *ptr+=4;break;

case '-':
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
{
Expand Down Expand Up @@ -355,11 +355,11 @@ static item_root* jsonString2item3 (const char** ptr)
}
}
break;

default:
break;
}

return result;
}

Expand All @@ -369,7 +369,7 @@ item_root* jsonFile2item3 (FILE* file)
fseek (file, 0, SEEK_END);
long length = ftell(file);
char* buffer = malloc (length+1);

fseek (file, 0l, SEEK_SET);
fread (buffer, 1, length, file);
buffer[length] = 0;
Expand All @@ -387,7 +387,7 @@ static void item32packContext(cw_pack_context* pc, item_root* item)
int i;
item_container* ic;
char* cp;

switch (item->item_type)
{
case ITEM_MAP:
Expand All @@ -396,39 +396,39 @@ static void item32packContext(cw_pack_context* pc, item_root* item)
for( i=0; i< ic->count; i++)
item32packContext (pc, ic->items[i]);
break;

case ITEM_ARRAY:
ic = (item_container*)item;
cw_pack_array_size(pc, ic->count);
for( i=0; i< ic->count; i++)
item32packContext (pc, ic->items[i]);
break;

case ITEM_NIL:
cw_pack_nil (pc);
break;

case ITEM_TRUE:
cw_pack_boolean(pc, true);
break;

case ITEM_FALSE:
cw_pack_boolean(pc, false);
break;

case ITEM_INTEGER:
cw_pack_signed(pc, ((item_integer*)item)->value);
break;

case ITEM_REAL:
cw_pack_double(pc, ((item_real*)item)->value);
break;

case ITEM_STRING:
cp = ((item_string*)item)->string;
cw_pack_str(pc, cp, (unsigned)strlen(cp));
break;

default: break;
}
}
Expand Down Expand Up @@ -457,7 +457,7 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
case CWP_ITEM_NIL:
result = allocate_item(item_root,ITEM_NIL,0);
break;

case CWP_ITEM_BOOLEAN:
if (uc->item.as.boolean)
{
Expand All @@ -474,23 +474,23 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
result = (item_root*)allocate_item(item_integer,ITEM_INTEGER,0);
((item_integer*)result)->value = uc->item.as.i64;
break;

case CWP_ITEM_FLOAT:
result = (item_root*)allocate_item(item_real,ITEM_REAL,0);
((item_real*)result)->value = uc->item.as.real;
break;

case CWP_ITEM_DOUBLE:
result = (item_root*)allocate_item(item_real,ITEM_REAL,0);
((item_real*)result)->value = uc->item.as.long_real;
break;

case CWP_ITEM_STR:
result = (item_root*)allocate_item(item_string,ITEM_STRING,uc->item.as.str.length + 1);
strncpy(((item_string*)result)->string, (const char*)uc->item.as.str.start, uc->item.as.str.length);
((item_string*)result)->string[uc->item.as.str.length] = 0;
break;

case CWP_ITEM_MAP:
dim = 2 * uc->item.as.map.size;
ic = allocate_container(ITEM_MAP, dim);
Expand All @@ -500,7 +500,7 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
}
result = (item_root*)ic;
break;

case CWP_ITEM_ARRAY:
dim = uc->item.as.array.size;
ic = allocate_container(ITEM_ARRAY, dim);
Expand All @@ -510,7 +510,7 @@ static item_root* packContext2item3 (cw_unpack_context* uc)
}
result = (item_root*)ic;
break;

default:
result = NULL;
break;
Expand Down
Loading

0 comments on commit 6ee2c2d

Please sign in to comment.