From 1b1249bdb0887c44a46b189153816eeed49806a4 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 10 Mar 2020 18:16:07 +0900 Subject: [PATCH] [ruamoko] Add vsprintf builtin --- libs/ruamoko/pr_cmds.c | 20 ++++++++++++++++++++ ruamoko/include/string.h | 1 + ruamoko/lib/string.r | 1 + 3 files changed, 22 insertions(+) diff --git a/libs/ruamoko/pr_cmds.c b/libs/ruamoko/pr_cmds.c index 8c6aa3d58..9a531fb14 100644 --- a/libs/ruamoko/pr_cmds.c +++ b/libs/ruamoko/pr_cmds.c @@ -583,6 +583,25 @@ PF_sprintf (progs_t *pr) dstring_delete (dstr); } +static void +PF_vsprintf (progs_t *pr) +{ + const char *fmt = P_GSTRING (pr, 0); + __auto_type args = &P_PACKED (pr, pr_va_list_t, 1); + pr_type_t *list_start = PR_GetPointer (pr, args->list); + pr_type_t **list = alloca (args->count * sizeof (*list)); + dstring_t *dstr; + + for (int i = 0; i < args->count; i++) { + list[i] = list_start + i * pr->pr_param_size; + } + + dstr = dstring_newstr (); + PR_Sprintf (pr, dstr, "PF_vsprintf", fmt, args->count, list); + RETURN_STRING (pr, dstr->str); + dstring_delete (dstr); +} + /* string () gametype */ @@ -643,6 +662,7 @@ static builtin_t builtins[] = { {"strlen", PF_strlen, QF 100}, {"charcount", PF_charcount, QF 101}, {"sprintf", PF_sprintf, QF 109}, + {"vsprintf", PF_vsprintf, -1}, {"ftoi", PF_ftoi, QF 110}, {"itof", PF_itof, QF 111}, {"itos", PF_itos, QF 112}, diff --git a/ruamoko/include/string.h b/ruamoko/include/string.h index c4136e2fc..d41e94ef8 100644 --- a/ruamoko/include/string.h +++ b/ruamoko/include/string.h @@ -7,6 +7,7 @@ @extern float strlen (string s); @extern float charcount (string goal, string s); @extern string sprintf (string fmt, ...); +@extern string vsprintf (string fmt, @va_list args); @extern string itos (int i); @extern int stoi (string s); @extern vector stov (string s); diff --git a/ruamoko/lib/string.r b/ruamoko/lib/string.r index 36247bf91..c6c239fa6 100644 --- a/ruamoko/lib/string.r +++ b/ruamoko/lib/string.r @@ -6,6 +6,7 @@ float (string s) stof = #81; float (string s) strlen = #0x000f0000 + 100; float (string goal, string s) charcount = #0x000f0000 + 101; string (string fmt, ...) sprintf = #0x000f0000 + 109; +string vsprintf (string fmt, @va_list args) = #0; string (int i) itos = #0x000f0000 + 112; int (string s) stoi = #0x000f0000 + 113; vector (string s) stov = #0x000f0000 + 114;