From: Nathan Wagner Date: Sat, 15 Sep 2018 08:03:29 +0000 (+0000) Subject: add zpm function to quote a string for sqlite X-Git-Tag: v0.1.6~43 X-Git-Url: https://pd.if.org/git/?a=commitdiff_plain;ds=sidebyside;h=001a7667ffa1aaf5dc664cffa6dae6016c9e4cbc;p=zpackage add zpm function to quote a string for sqlite --- diff --git a/lib/quote.c b/lib/quote.c new file mode 100644 index 0000000..61d28c3 --- /dev/null +++ b/lib/quote.c @@ -0,0 +1,35 @@ +#include + +#include "sqlite3.h" + +int zpm_quote(char *value, char *dest, size_t n) { + size_t dlen = 0; + int nch = 0; + char *s; + + /* figure out length of quoted string */ + for (s = value; *s; s++) { + dlen++; + if (*s == '\'') dlen++; + } + + /* if there's enough room for the string and the terminating null + * byte, quote the string + */ + if (dest && dlen < n) { + while (*value) { + if (*value == '\'') { + *dest++ = '\''; + nch++; + } + *dest++ = *value++; + nch++; + } + dest[nch] = 0; + } + + /* returns either the number of characters written or would + * be written + */ + return dlen; +}