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