diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-29 18:09:12 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-29 18:09:12 -0500 |
| commit | f66f8ad7119207b99f00ea2ea389550ee65db5b3 (patch) | |
| tree | 5b5a7c887b311e3de2f2cb293b1228598c5b9eb1 /stdlib/text.c | |
| parent | 4b5e4cd1f21582f5e5fa682ab4e4bff252963468 (diff) | |
Add serialization and deserialization
Diffstat (limited to 'stdlib/text.c')
| -rw-r--r-- | stdlib/text.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/stdlib/text.c b/stdlib/text.c index 40a3988b..fed113e0 100644 --- a/stdlib/text.c +++ b/stdlib/text.c @@ -1377,6 +1377,24 @@ PUREFUNC public bool Text$is_none(const void *t, const TypeInfo_t*) return ((Text_t*)t)->length < 0; } +public void Text$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *) +{ + const char *str = Text$as_c_string(*(Text_t*)obj); + int64_t len = (int64_t)strlen(str); + Int64$serialize(&len, out, pointers, &Int64$info); + fwrite(str, sizeof(char), (size_t)len, out); +} + +public void Text$deserialize(FILE *in, void *out, Array_t *pointers, const TypeInfo_t *) +{ + int64_t len = -1; + Int64$deserialize(in, &len, pointers, &Int64$info); + char *buf = GC_MALLOC_ATOMIC((size_t)len+1); + fread(buf, sizeof(char), (size_t)len, in); + buf[len+1] = '\0'; + *(Text_t*)out = Text$from_strn(buf, (size_t)len); +} + public const TypeInfo_t Text$info = { .size=sizeof(Text_t), .align=__alignof__(Text_t), |
