From 001a7667ffa1aaf5dc664cffa6dae6016c9e4cbc Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sat, 15 Sep 2018 08:03:29 +0000 Subject: [PATCH] add zpm function to quote a string for sqlite --- lib/quote.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/quote.c 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; +} -- 2.40.0