mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 13:02:13 +01:00
Refactor packing code.
This commit is contained in:
parent
0b33a634a6
commit
257270c1eb
@ -31,6 +31,7 @@ cdef extern from "pack.h":
|
|||||||
void msgpack_pack_nil(msgpack_packer* pk)
|
void msgpack_pack_nil(msgpack_packer* pk)
|
||||||
void msgpack_pack_true(msgpack_packer* pk)
|
void msgpack_pack_true(msgpack_packer* pk)
|
||||||
void msgpack_pack_false(msgpack_packer* pk)
|
void msgpack_pack_false(msgpack_packer* pk)
|
||||||
|
void msgpack_pack_long(msgpack_packer* pk, long d)
|
||||||
void msgpack_pack_long_long(msgpack_packer* pk, long long d)
|
void msgpack_pack_long_long(msgpack_packer* pk, long long d)
|
||||||
void msgpack_pack_double(msgpack_packer* pk, double d)
|
void msgpack_pack_double(msgpack_packer* pk, double d)
|
||||||
void msgpack_pack_array(msgpack_packer* pk, size_t l)
|
void msgpack_pack_array(msgpack_packer* pk, size_t l)
|
||||||
@ -60,7 +61,7 @@ cdef class Packer(object):
|
|||||||
msgpack_packer_init(&self.pk, <void*>self, <msgpack_packer_write>_packer_write)
|
msgpack_packer_init(&self.pk, <void*>self, <msgpack_packer_write>_packer_write)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
free(self.buff);
|
free(self.buff)
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
"""Flash local buffer and output stream if it has 'flush()' method."""
|
"""Flash local buffer and output stream if it has 'flush()' method."""
|
||||||
@ -117,7 +118,7 @@ cdef class Packer(object):
|
|||||||
msgpack_pack_long_long(&self.pk, llval)
|
msgpack_pack_long_long(&self.pk, llval)
|
||||||
elif isinstance(o, int):
|
elif isinstance(o, int):
|
||||||
longval = o
|
longval = o
|
||||||
msgpack_pack_long_long(&self.pk, longval)
|
msgpack_pack_long(&self.pk, longval)
|
||||||
elif isinstance(o, float):
|
elif isinstance(o, float):
|
||||||
fval = o
|
fval = o
|
||||||
msgpack_pack_double(&self.pk, fval)
|
msgpack_pack_double(&self.pk, fval)
|
||||||
@ -133,12 +134,12 @@ cdef class Packer(object):
|
|||||||
elif PyMapping_Check(o):
|
elif PyMapping_Check(o):
|
||||||
msgpack_pack_map(&self.pk, len(o))
|
msgpack_pack_map(&self.pk, len(o))
|
||||||
for k,v in o.iteritems():
|
for k,v in o.iteritems():
|
||||||
self.pack(k)
|
self.__pack(k)
|
||||||
self.pack(v)
|
self.__pack(v)
|
||||||
elif PySequence_Check(o):
|
elif PySequence_Check(o):
|
||||||
msgpack_pack_array(&self.pk, len(o))
|
msgpack_pack_array(&self.pk, len(o))
|
||||||
for v in o:
|
for v in o:
|
||||||
self.pack(v)
|
self.__pack(v)
|
||||||
else:
|
else:
|
||||||
# TODO: Serialize with defalt() like simplejson.
|
# TODO: Serialize with defalt() like simplejson.
|
||||||
raise TypeError, "can't serialize %r" % (o,)
|
raise TypeError, "can't serialize %r" % (o,)
|
||||||
@ -154,7 +155,7 @@ cdef int _packer_write(Packer packer, const_char_ptr b, unsigned int l):
|
|||||||
if packer.length + l > packer.allocated:
|
if packer.length + l > packer.allocated:
|
||||||
if packer.length > 0:
|
if packer.length > 0:
|
||||||
packer.strm.write(PyString_FromStringAndSize(packer.buff, packer.length))
|
packer.strm.write(PyString_FromStringAndSize(packer.buff, packer.length))
|
||||||
if l > 64:
|
if l > packer.allocated/4:
|
||||||
packer.strm.write(PyString_FromStringAndSize(b, l))
|
packer.strm.write(PyString_FromStringAndSize(b, l))
|
||||||
packer.length = 0
|
packer.length = 0
|
||||||
else:
|
else:
|
||||||
@ -176,7 +177,6 @@ def packb(object o):
|
|||||||
buf = StringIO()
|
buf = StringIO()
|
||||||
packer = Packer(buf)
|
packer = Packer(buf)
|
||||||
packer.pack(o)
|
packer.pack(o)
|
||||||
packer.flush()
|
|
||||||
return buf.getvalue()
|
return buf.getvalue()
|
||||||
|
|
||||||
packs = packb
|
packs = packb
|
||||||
|
@ -34,9 +34,6 @@ typedef struct msgpack_packer {
|
|||||||
|
|
||||||
static inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback);
|
static inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback);
|
||||||
|
|
||||||
static inline msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback);
|
|
||||||
static inline void msgpack_packer_free(msgpack_packer* pk);
|
|
||||||
|
|
||||||
static inline int msgpack_pack_short(msgpack_packer* pk, short d);
|
static inline int msgpack_pack_short(msgpack_packer* pk, short d);
|
||||||
static inline int msgpack_pack_int(msgpack_packer* pk, int d);
|
static inline int msgpack_pack_int(msgpack_packer* pk, int d);
|
||||||
static inline int msgpack_pack_long(msgpack_packer* pk, long d);
|
static inline int msgpack_pack_long(msgpack_packer* pk, long d);
|
||||||
@ -90,20 +87,6 @@ static inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_p
|
|||||||
pk->callback = callback;
|
pk->callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback)
|
|
||||||
{
|
|
||||||
msgpack_packer* pk = (msgpack_packer*)calloc(1, sizeof(msgpack_packer));
|
|
||||||
if(!pk) { return NULL; }
|
|
||||||
msgpack_packer_init(pk, data, callback);
|
|
||||||
return pk;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void msgpack_packer_free(msgpack_packer* pk)
|
|
||||||
{
|
|
||||||
free(pk);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user