diff options
| author | navewindre <boneyaard@gmail.com> | 2025-09-03 20:10:09 +0200 |
|---|---|---|
| committer | navewindre <boneyaard@gmail.com> | 2025-09-03 20:10:09 +0200 |
| commit | f8b92ce3aa08b1445c9f956d8166830946562d12 (patch) | |
| tree | 94e63a5aec9f8f52b577f56799e0c9201fd976a5 /src/util/config/serializers.cpp | |
a
Diffstat (limited to 'src/util/config/serializers.cpp')
| -rw-r--r-- | src/util/config/serializers.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/util/config/serializers.cpp b/src/util/config/serializers.cpp new file mode 100644 index 0000000..01f416c --- /dev/null +++ b/src/util/config/serializers.cpp @@ -0,0 +1,109 @@ +#include <cstdarg> +#include <cstdio> + +#include "../config.h" + +void serialize_node( CFG_SERIALIZER* s, CFG_NODE* n, char* buf ) { + U32 len = strlen( buf ); + for( U32 i = 0; i < s->tabc * 2; ++i ) buf[i + len] = ' '; + buf[len + s->tabc * 2] = 0; + sprintf( buf, "%s%s %s", buf, cfg_types[n->type].def, n->name ); +} + +void cfg_serialize_section( CFG_SERIALIZER* s, CFG_NODE *n, char *buf ) { + CFG_SECTION* sec = (CFG_SECTION*)n; + if( sec->parent ) { + serialize_node( s, n, buf ); + strcat( buf, " {\n" ); + s->tabc++; + } + + char line[8192]; + for( U32 i = 0; i < sec->children.size; ++i ) { + line[0] = 0; + CFG_NODE* c = sec->children[i]; + if( c->type == CFGT_SECTION ) { + cfg_serialize_section( s, c, buf ); + continue; + } + + cfg_types[c->type].serializer( s, c, line ); + strcat( buf, line ); + strcat( buf, "\n" ); + } + + if( sec->parent ) { + s->tabc--; + char tabbuf[512] = { 0 }; + for( U32 i = 0 ; i < s->tabc * 2; ++i ) + tabbuf[i] = ' '; + tabbuf[s->tabc * 2] = 0; + strcat( buf, tabbuf ); + strcat( buf, "}" ); + if( sec->parent ) + strcat( buf, "\n" ); + } +} + +void cfg_serialize_bytes( CFG_SERIALIZER* s, CFG_NODE* n, char* buf ) { + CFG_BYTES* b = (CFG_BYTES*)n; + U32 size = b->size; + U8* bytes = b->bytes; + + serialize_node( s, n, buf ); + sprintf( buf, "%s[%d] = \"", buf, size ); + U32 len = strlen( buf ); + for( U32 i = 0; i < size; ++i ) { + sprintf( buf + len + i * 2, "%02X", bytes[i] ); + } + + sprintf( buf, "%s%s", buf, "\";" ); +} + +void cfg_serialize_str( CFG_SERIALIZER* s, CFG_NODE* n, char *buf ) { + CFG_STR* sn = (CFG_STR*)n; + char* str = sn->str; + + serialize_node( s, n, buf ); + sprintf( buf, "%s[%d] = \"%s\";", buf, sn->len, str ); +} + +void cfg_serialize_int( CFG_SERIALIZER* s, CFG_NODE* n, char *buf ) { + CFG_INT* i = (CFG_INT*)n; + I32 ival = i->value; + + serialize_node( s, n, buf ); + sprintf( buf, "%s = %d;", buf, ival ); +} + +void cfg_serialize_float( CFG_SERIALIZER* s, CFG_NODE* n, char *buf ) { + CFG_FLOAT* f = (CFG_FLOAT*)n; + F32 fval = f->value; + + serialize_node( s, n, buf ); + sprintf( buf, "%s = %g;", buf, fval ); +} + +void cfg_serialize_vec2( CFG_SERIALIZER* s, CFG_NODE* n, char *buf ) { + CFG_VEC2* v = (CFG_VEC2*)n; + VEC2 val = v->value; + + serialize_node( s, n, buf ); + sprintf( buf, "%s = { %g, %g };", buf, val.x, val.y ); +} + +void cfg_serialize_vec3( CFG_SERIALIZER* s, CFG_NODE* n, char *buf ) { + CFG_VEC3* v = (CFG_VEC3*)n; + VEC3 val = v->value; + + serialize_node( s, n, buf ); + sprintf( buf, "%s = { %g, %g, %g };", buf, val.x, val.y, val.z ); +} + +void cfg_serialize_clr( CFG_SERIALIZER* s, CFG_NODE* n, char *buf ) { + CFG_CLR* v = (CFG_CLR*)n; + CLR val = v->value; + + serialize_node( s, n, buf ); + sprintf( buf, "%s = { %g, %g, %g, %g };", buf, val.r, val.g, val.b, val.a ); +} |
