4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
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]));
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
32 int lsmStringExtend(LsmString
*pStr
, int nNew
){
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
);
39 lsmFree(pStr
->pEnv
, pStr
->z
);
43 pStr
->nAlloc
= nAlloc
;
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
){
66 if( N
<0 ) N
= (int)strlen(z
);
67 rc
= lsmStringExtend(pStr
, N
+1);
69 memcpy(pStr
->z
+pStr
->n
, z
, N
+1);
75 int lsmStringBinAppend(LsmString
*pStr
, const u8
*a
, int n
){
77 rc
= lsmStringExtend(pStr
, n
);
79 memcpy(pStr
->z
+pStr
->n
, a
, n
);
86 ** Append printf-formatted content to an LsmString.
88 void lsmStringVAppendf(
94 #if (!defined(__STDC_VERSION__) || (__STDC_VERSION__<199901L)) && \
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.
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
);
116 pStr
->z
[pStr
->n
] = 0;
119 void lsmStringAppendf(LsmString
*pStr
, const char *zFormat
, ...){
121 va_start(ap
, zFormat
);
122 va_start(ap2
, zFormat
);
123 lsmStringVAppendf(pStr
, zFormat
, ap
, ap2
);
128 int lsmStrlen(const char *zName
){
130 while( zName
[nRet
] ) nRet
++;
135 ** Write into memory obtained from lsm_malloc().
137 char *lsmMallocPrintf(lsm_env
*pEnv
, const char *zFormat
, ...){
140 lsmStringInit(&s
, pEnv
);
141 va_start(ap
, zFormat
);
142 va_start(ap2
, zFormat
);
143 lsmStringVAppendf(&s
, zFormat
, ap
, ap2
);
146 if( s
.n
<0 ) return 0;
147 return (char *)lsmReallocOrFree(pEnv
, s
.z
, s
.n
+1);