xref: /sqlite-3.40.0/ext/lsm1/lsm_str.c (revision 038af0bd)
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.
10 **
11 *************************************************************************
12 **
13 ** Dynamic string functions.
14 */
15 #include "lsmInt.h"
16 
17 /*
18 ** Turn bulk and uninitialized memory into an LsmString object
19 */
lsmStringInit(LsmString * pStr,lsm_env * pEnv)20 void lsmStringInit(LsmString *pStr, lsm_env *pEnv){
21   memset(pStr, 0, sizeof(pStr[0]));
22   pStr->pEnv = pEnv;
23 }
24 
25 /*
26 ** Increase the memory allocated for holding the string.  Realloc as needed.
27 **
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.
31 */
lsmStringExtend(LsmString * pStr,int nNew)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;
42     }
43     pStr->nAlloc = nAlloc;
44     pStr->z = zNew;
45   }
46   return (pStr->z ? LSM_OK : LSM_NOMEM_BKPT);
47 }
48 
49 /*
50 ** Clear an LsmString object, releasing any allocated memory that it holds.
51 ** This also clears the error indication (if any).
52 */
lsmStringClear(LsmString * pStr)53 void lsmStringClear(LsmString *pStr){
54   lsmFree(pStr->pEnv, pStr->z);
55   lsmStringInit(pStr, pStr->pEnv);
56 }
57 
58 /*
59 ** Append N bytes of text to the end of an LsmString object.  If
60 ** N is negative, append the entire string.
61 **
62 ** If the string is in an error state, this routine is a no-op.
63 */
lsmStringAppend(LsmString * pStr,const char * z,int N)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;
71   }
72   return rc;
73 }
74 
lsmStringBinAppend(LsmString * pStr,const u8 * a,int n)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;
81   }
82   return rc;
83 }
84 
85 /*
86 ** Append printf-formatted content to an LsmString.
87 */
lsmStringVAppendf(LsmString * pStr,const char * zFormat,va_list ap1,va_list ap2)88 void lsmStringVAppendf(
89   LsmString *pStr,
90   const char *zFormat,
91   va_list ap1,
92   va_list ap2
93 ){
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.
101     */;
102 #endif
103   int nWrite;
104   int nAvail;
105 
106   nAvail = pStr->nAlloc - pStr->n;
107   nWrite = vsnprintf(pStr->z + pStr->n, nAvail, zFormat, ap1);
108 
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);
113   }
114 
115   pStr->n += nWrite;
116   pStr->z[pStr->n] = 0;
117 }
118 
lsmStringAppendf(LsmString * pStr,const char * zFormat,...)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);
126 }
127 
lsmStrlen(const char * zName)128 int lsmStrlen(const char *zName){
129   int nRet = 0;
130   while( zName[nRet] ) nRet++;
131   return nRet;
132 }
133 
134 /*
135 ** Write into memory obtained from lsm_malloc().
136 */
lsmMallocPrintf(lsm_env * pEnv,const char * zFormat,...)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);
148 }
149