Hash :
9d2612e2
Author :
Thomas de Grivel
Date :
2024-12-09T10:57:59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "assert.h"
#include "alloc.h"
#include "buf.h"
#include "serialize.h"
#define DEF_SERIALIZE(type) \
s_serialize * serialize_ ## type (s_serialize *serialize, \
type src) \
{ \
sw r; \
assert(serialize); \
if ((r = buf_write_ ## type(&serialize->buf, src)) <= 0) \
return NULL; \
return serialize; \
}
DEF_SERIALIZE(bool)
void serialize_clean (s_serialize *serialize)
{
assert(serialize);
buf_clean(&serialize->buf);
buf_clean(&serialize->heap);
}
void serialize_delete (s_serialize *serialize)
{
serialize_clean(serialize);
free(serialize);
}
s_serialize * serialize_character (s_serialize *serialize,
character c)
{
return serialize_u32(serialize, c);
}
s_serialize * serialize_init (s_serialize *serialize)
{
s_serialize tmp = {0};
buf_init_alloc(&tmp.heap, 1024024);
buf_init_alloc(&tmp.buf, 1024024);
*serialize = tmp;
return serialize;
}
s_serialize * serialize_list (s_serialize *serialize,
const s_list *list);
s_serialize * serialize_new (void)
{
s_serialize *serialize;
if (! (serialize = alloc(sizeof(s_serialize))))
return NULL;
serialize_init(serialize);
return serialize;
}
DEF_SERIALIZE(s8)
DEF_SERIALIZE(s16)
DEF_SERIALIZE(s32)
DEF_SERIALIZE(s64)
s_serialize * serialize_str (s_serialize *serialize, const s_str *src)
{
sw r;
assert(serialize);
assert(src);
if (! serialize_u32(serialize, src->size))
return NULL;
if ((r = buf_write(&serialize->buf, src->ptr.pchar, src->size)) <= 0)
return NULL;
return serialize;
}
DEF_SERIALIZE(sw)
s_serialize * serialize_tag (s_serialize *serialize, const s_tag *tag)
{
switch (tag->type){
case TAG_BOOL: return serialize_bool(serialize, tag->data.bool);
case TAG_CHARACTER:
return serialize_character(serialize, tag->data.character);
case TAG_S8: return serialize_s8(serialize, tag->data.s8);
case TAG_S16: return serialize_s16(serialize, tag->data.s16);
case TAG_S32: return serialize_s32(serialize, tag->data.s32);
case TAG_S64: return serialize_s64(serialize, tag->data.s64);
case TAG_STR: return serialize_str(serialize, &tag->data.str);
case TAG_SW: return serialize_sw(serialize, tag->data.sw);
case TAG_U8: return serialize_u8(serialize, tag->data.u8);
case TAG_U16: return serialize_u16(serialize, tag->data.u16);
case TAG_U32: return serialize_u32(serialize, tag->data.u32);
case TAG_U64: return serialize_u64(serialize, tag->data.u64);
case TAG_UW: return serialize_uw(serialize, tag->data.uw);
default: break;
}
err_puts("serialize_tag: not implemented");
assert(! "serialize_tag: not implemented");
return NULL;
}
sw serialize_to_buf (s_serialize *serialize, s_buf *buf)
{
return buf_xfer(&serialize->buf, buf,
serialize->buf.wpos - serialize->buf.rpos);
}
s_str * serialize_to_str (s_serialize *serialize, s_str *dest)
{
return buf_read_to_str(&serialize->buf, dest);
}
s_serialize * serialize_tuple (s_serialize *serialize,
const s_tuple *tuple);
DEF_SERIALIZE(u8)
DEF_SERIALIZE(u16)
DEF_SERIALIZE(u32)
DEF_SERIALIZE(u64)
DEF_SERIALIZE(uw)