-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
52 lines (45 loc) · 1.33 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT3
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Esent.h>
#include "sqlite3esent.h"
/* Following function is copy-pasted from SQLite sources */
/*
** This function converts an SQL quoted string into an unquoted string
** and returns a pointer to a buffer allocated using sqlite3_malloc()
** containing the result. The caller should eventually free this buffer
** using sqlite3_free.
**
** Examples:
**
** "abc" becomes abc
** 'xyz' becomes xyz
** [pqr] becomes pqr
** `mno` becomes mno
*/
char* esentDequote(const char* zIn) {
sqlite3_int64 nIn; /* Size of input string, in bytes */
char* zOut; /* Output (dequoted) string */
nIn = strlen(zIn);
zOut = sqlite3_malloc64(nIn + 1);
if (zOut) {
char q = zIn[0]; /* Quote character (if any ) */
if (q != '[' && q != '\'' && q != '"' && q != '`') {
memcpy(zOut, zIn, (size_t)(nIn + 1));
}
else {
int iOut = 0; /* Index of next byte to write to output */
int iIn; /* Index of next byte to read from input */
if (q == '[') q = ']';
for (iIn = 1; iIn < nIn; iIn++) {
if (zIn[iIn] == q) iIn++;
zOut[iOut++] = zIn[iIn];
}
}
assert((int)strlen(zOut) <= nIn);
}
return zOut;
}