]> pd.if.org Git - zpackage/blob - lib/quote.c
cleanup error handling in zpm_init
[zpackage] / lib / quote.c
1 #include <stddef.h>
2 #include <stdio.h>
3
4 #include "sqlite3.h"
5
6 int zpm_quote(char *value, char *dest, size_t n) {
7         size_t dlen = 0;
8         int nch = 0;
9         char *s;
10
11         /* figure out length of quoted string */
12         for (s = value; *s; s++) {
13                 dlen++;
14                 if (*s == '\'') dlen++;
15         }
16
17         /* if there's enough room for the string and the terminating null
18          * byte, quote the string
19          */
20         if (dest && dlen < n) {
21                 while (*value) {
22                         if (*value == '\'') {
23                                 *dest++ = '\'';
24                                 nch++;
25                         }
26                         *dest++ = *value++;
27                         nch++;
28                 }
29                 dest[nch] = 0;
30         }
31
32
33         /* returns either the number of characters written or would
34          * be written
35          */
36         return dlen;
37 }