Update test file walprotocol.test to account for the changes in the wal
[sqlite.git] / ext / lsm1 / lsm_str.c
blob9b1b63cee248afaecb3e48d5bee20b83c539a883
1 /*
2 ** 2012-04-27
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** Dynamic string functions.
15 #include "lsmInt.h"
18 ** Turn bulk and uninitialized memory into an LsmString object
20 void lsmStringInit(LsmString *pStr, lsm_env *pEnv){
21 memset(pStr, 0, sizeof(pStr[0]));
22 pStr->pEnv = pEnv;
26 ** Increase the memory allocated for holding the string. Realloc as needed.
28 ** If a memory allocation error occurs, set pStr->n to -1 and free the existing
29 ** allocation. If a prior memory allocation has occurred, this routine is a
30 ** no-op.
32 int lsmStringExtend(LsmString *pStr, int nNew){
33 assert( nNew>0 );
34 if( pStr->n<0 ) return LSM_NOMEM;
35 if( pStr->n + nNew >= pStr->nAlloc ){
36 int nAlloc = pStr->n + nNew + 100;
37 char *zNew = lsmRealloc(pStr->pEnv, pStr->z, nAlloc);
38 if( zNew==0 ){
39 lsmFree(pStr->pEnv, pStr->z);
40 nAlloc = 0;
41 pStr->n = -1;
43 pStr->nAlloc = nAlloc;
44 pStr->z = zNew;
46 return (pStr->z ? LSM_OK : LSM_NOMEM_BKPT);
50 ** Clear an LsmString object, releasing any allocated memory that it holds.
51 ** This also clears the error indication (if any).
53 void lsmStringClear(LsmString *pStr){
54 lsmFree(pStr->pEnv, pStr->z);
55 lsmStringInit(pStr, pStr->pEnv);
59 ** Append N bytes of text to the end of an LsmString object. If
60 ** N is negative, append the entire string.
62 ** If the string is in an error state, this routine is a no-op.
64 int lsmStringAppend(LsmString *pStr, const char *z, int N){
65 int rc;
66 if( N<0 ) N = (int)strlen(z);
67 rc = lsmStringExtend(pStr, N+1);
68 if( pStr->nAlloc ){
69 memcpy(pStr->z+pStr->n, z, N+1);
70 pStr->n += N;
72 return rc;
75 int lsmStringBinAppend(LsmString *pStr, const u8 *a, int n){
76 int rc;
77 rc = lsmStringExtend(pStr, n);
78 if( pStr->nAlloc ){
79 memcpy(pStr->z+pStr->n, a, n);
80 pStr->n += n;
82 return rc;
86 ** Append printf-formatted content to an LsmString.
88 void lsmStringVAppendf(
89 LsmString *pStr,
90 const char *zFormat,
91 va_list ap1,
92 va_list ap2
94 #if (!defined(__STDC_VERSION__) || (__STDC_VERSION__<199901L)) && \
95 !defined(__APPLE__)
96 extern int vsnprintf(char *str, size_t size, const char *format, va_list ap)
97 /* Compatibility crutch for C89 compilation mode. sqlite3_vsnprintf()
98 does not work identically and causes test failures if used here.
99 For the time being we are assuming that the target has vsnprintf(),
100 but that is not guaranteed to be the case for pure C89 platforms.
102 #endif
103 int nWrite;
104 int nAvail;
106 nAvail = pStr->nAlloc - pStr->n;
107 nWrite = vsnprintf(pStr->z + pStr->n, nAvail, zFormat, ap1);
109 if( nWrite>=nAvail ){
110 lsmStringExtend(pStr, nWrite+1);
111 if( pStr->nAlloc==0 ) return;
112 nWrite = vsnprintf(pStr->z + pStr->n, nWrite+1, zFormat, ap2);
115 pStr->n += nWrite;
116 pStr->z[pStr->n] = 0;
119 void lsmStringAppendf(LsmString *pStr, const char *zFormat, ...){
120 va_list ap, ap2;
121 va_start(ap, zFormat);
122 va_start(ap2, zFormat);
123 lsmStringVAppendf(pStr, zFormat, ap, ap2);
124 va_end(ap);
125 va_end(ap2);
128 int lsmStrlen(const char *zName){
129 int nRet = 0;
130 while( zName[nRet] ) nRet++;
131 return nRet;
135 ** Write into memory obtained from lsm_malloc().
137 char *lsmMallocPrintf(lsm_env *pEnv, const char *zFormat, ...){
138 LsmString s;
139 va_list ap, ap2;
140 lsmStringInit(&s, pEnv);
141 va_start(ap, zFormat);
142 va_start(ap2, zFormat);
143 lsmStringVAppendf(&s, zFormat, ap, ap2);
144 va_end(ap);
145 va_end(ap2);
146 if( s.n<0 ) return 0;
147 return (char *)lsmReallocOrFree(pEnv, s.z, s.n+1);