Raised This Month: $51 Target: $400
 12% 

[ANY] MessagePack


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
bottiger
AlliedModders Donor
Join Date: Dec 2010
Old 03-07-2014 , 23:16   [ANY] MessagePack
Reply With Quote #1

Extension for http://msgpack.org/

Linux build included. See the sourcepawn file for more examples.

Example

Code:
    // format [1, -1, 65536, "foobar"]
    new Handle:pack = MsgPack_NewPack();
    
    MsgPack_PackArray(pack, 4);
    MsgPack_PackInt(pack, 1);
    MsgPack_PackInt(pack, -1);
    MsgPack_PackInt(pack, 65536);
    MsgPack_PackString(pack, "foobar");
    
    new size = MsgPack_GetPackSize(pack);
    decl String:dumped[size];
    MsgPack_GetPackBuffer(pack, dumped, size);
    CloseHandle(pack);
Unpacking Example

Code:
    // format [nil, false, true, {true=>false, "foo"=>1.234200}]

    new Handle:root = MsgPack_NewUnpack(dumped, size);
    new Handle:map = MsgPack_UnpackArray(root, 3);
    new Handle:k = MsgPack_UnpackKey(map, 1);
    new Handle:v = MsgPack_UnpackValue(map, 1);
    
    decl String:key[64];
    MsgPack_UnpackString(k, key, sizeof(key));
    new Float:value = MsgPack_UnpackFloat(v);

    CloseHandle(root);
    CloseHandle(map);
    CloseHandle(k);
    CloseHandle(v);
Code:
enum MsgPack_Type {
	MSGPACK_OBJECT_NIL	= 0x00,
    MSGPACK_OBJECT_BOOLEAN	= 0x01,
    MSGPACK_OBJECT_POSITIVE_INTEGER	= 0x02,
    MSGPACK_OBJECT_NEGATIVE_INTEGER	= 0x03,
    MSGPACK_OBJECT_DOUBLE	= 0x04,
    MSGPACK_OBJECT_RAW	= 0x05,
    MSGPACK_OBJECT_ARRAY	= 0x06,
    MSGPACK_OBJECT_MAP	= 0x07,
};

/*
*  Packing Functions
*/

// create a pack handle to pack with
native Handle:MsgPack_NewPack();

native MsgPack_PackInt(Handle:pack, value);
native MsgPack_PackFloat(Handle:pack, Float:value);
native MsgPack_PackNil(Handle:pack);
native MsgPack_PackTrue(Handle:pack);
native MsgPack_PackFalse(Handle:pack);
native MsgPack_PackArray(Handle:pack, size);
native MsgPack_PackMap(Handle:pack, size);

// pack some bytes. does not null terminate
native MsgPack_PackRaw(Handle:pack, const String:buffer[], buffer_length);

stock MsgPack_PackString(Handle:pack, const String:null_terminated_string[]) {
    return MsgPack_PackRaw(pack, null_terminated_string, strlen(null_terminated_string));
}

// get the pack length in bytes
native MsgPack_GetPackSize(Handle:pack);

// get the buffer so you can send it somewhere
native MsgPack_GetPackBuffer(Handle:pack, const String:buffer[], buffer_length);

/*
*  Unpacking Functions
*/

// create a messagepack object handle.
// offset is the number of bytes consumed
// enabling save_buffer copies the buffer so objects still work properly when the buffer goes out of scope
// the buffer is shared between all objects that come from here and will be deleted when they are all closed
native Handle:MsgPack_NewUnpack(const String:buffer[], buffer_length, &offset=0, bool:save_buffer=true);


// pretty print object to console for debugging
native MsgPack_UnpackPrint(Handle:obj);

// get the object type
native MsgPack_Type:MsgPack_UnpackType(Handle:obj);

// get the number of elements for maps, arrays, and raws. returns 0 if not one of those types
native MsgPack_UnpackCount(Handle:obj);

native bool:MsgPack_UnpackBool(Handle:obj);
native MsgPack_UnpackInt(Handle:obj);
native Float:MsgPack_UnpackFloat(Handle:obj);
native MsgPack_UnpackRaw(Handle:obj, String:buffer[], buffer_length);
stock MsgPack_UnpackString(Handle:obj, String:buffer[], buffer_length) {
    new written = MsgPack_UnpackRaw(obj, buffer, buffer_length);
    if(written-1 >= buffer_length)
        buffer[buffer_length] = 0;
    else
        buffer[written] = 0;
    
    return written;
}
native Handle:MsgPack_UnpackArray(Handle:obj, index);
native Handle:MsgPack_UnpackKey(Handle:obj, index);
native Handle:MsgPack_UnpackValue(Handle:obj, index);
native Handle:MsgPack_UnpackNext(Handle:obj);
native any:MsgPack_UnpackArrayCell(Handle:obj, index);
Changelog

4/23/2015
  • Renamed MsgPack_Object* to MsgPack_Unpack*
  • Added MsgPack_UnpackArrayCell.
Attached Files
File Type: zip msgpack.zip (18.1 KB, 233 views)
File Type: zip msgpack.ext.so.zip (15.8 KB, 209 views)
File Type: inc msgpack.inc (4.2 KB, 253 views)
__________________

Last edited by bottiger; 04-23-2015 at 15:10.
bottiger is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:48.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode