From 2d4f1de0e33f5503143eaf50674c6a2760c8ac0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Wed, 18 Aug 2021 17:25:45 +0200 Subject: [PATCH] :art: Add string.save/load --- src/lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/lib/string.c b/src/lib/string.c index b5968bbb..76af1fee 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -365,6 +365,36 @@ static MFUN(string_erase) { STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, c)); } +static MFUN(string_save) { + const m_str path = STRING(*(M_Object*)MEM(SZ_INT)); + FILE *f = fopen(path, "w"); + if(!f) { + *(m_uint*)RETURN = false; + return; + } + const m_str str = STRING(o); + fprintf(f, "%s", str); + fclose(f); + *(m_uint*)RETURN = true; +} + +static MFUN(string_load) { + const m_str path = STRING(*(M_Object*)MEM(SZ_INT)); + FILE *f = fopen(path, "r"); + if(!f) { + *(m_uint*)RETURN = false; + return; + } + fseek(f, 0, SEEK_END); + const size_t sz = ftell(f); + char c[sz + 1]; + rewind(f); + fread(c, 1, sz, f); + fclose(f); + STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, c)); + *(m_uint*)RETURN = true; +} + GWION_IMPORT(string) { const Type t_string = gwi_class_ini(gwi, "string", NULL); gwi->gwion->type[et_string] = t_string; // use func @@ -456,6 +486,14 @@ GWION_IMPORT(string) { gwi_func_arg(gwi, "int", "length"); GWI_BB(gwi_func_end(gwi, string_erase, ae_flag_none)) + gwi_func_ini(gwi, "bool", "save"); + gwi_func_arg(gwi, "string", "path"); + GWI_BB(gwi_func_end(gwi, string_save, ae_flag_none)) + + gwi_func_ini(gwi, "bool", "load"); + gwi_func_arg(gwi, "string", "path"); + GWI_BB(gwi_func_end(gwi, string_load, ae_flag_none)) + GWI_BB(gwi_class_end(gwi)) GWI_BB(gwi_oper_ini(gwi, "string", "string", "bool")) -- 2.43.0