From: Jérémie Astor Date: Wed, 18 Aug 2021 15:25:45 +0000 (+0200) Subject: :art: Add string.save/load X-Git-Tag: nightly~470^2~53 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=2d4f1de0e33f5503143eaf50674c6a2760c8ac0a;p=gwion.git :art: Add string.save/load --- 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"))