#include "lsmInt.h"
void lsmStringInit(LsmString *pStr, lsm_env *pEnv){
memset(pStr, 0, sizeof(pStr[0]));
pStr->pEnv = pEnv;
}
int lsmStringExtend(LsmString *pStr, int nNew){
assert( nNew>0 );
if( pStr->n<0 ) return LSM_NOMEM;
if( pStr->n + nNew >= pStr->nAlloc ){
int nAlloc = pStr->n + nNew + 100;
char *zNew = lsmRealloc(pStr->pEnv, pStr->z, nAlloc);
if( zNew==0 ){
lsmFree(pStr->pEnv, pStr->z);
nAlloc = 0;
pStr->n = -1;
}
pStr->nAlloc = nAlloc;
pStr->z = zNew;
}
return (pStr->z ? LSM_OK : LSM_NOMEM_BKPT);
}
void lsmStringClear(LsmString *pStr){
lsmFree(pStr->pEnv, pStr->z);
lsmStringInit(pStr, pStr->pEnv);
}
int lsmStringAppend(LsmString *pStr, const char *z, int N){
int rc;
if( N<0 ) N = (int)strlen(z);
rc = lsmStringExtend(pStr, N+1);
if( pStr->nAlloc ){
memcpy(pStr->z+pStr->n, z, N+1);
pStr->n += N;
}
return rc;
}
int lsmStringBinAppend(LsmString *pStr, const u8 *a, int n){
int rc;
rc = lsmStringExtend(pStr, n);
if( pStr->nAlloc ){
memcpy(pStr->z+pStr->n, a, n);
pStr->n += n;
}
return rc;
}
void lsmStringVAppendf(
LsmString *pStr,
const char *zFormat,
va_list ap1,
va_list ap2
){
#if (!defined(__STDC_VERSION__) || (__STDC_VERSION__<199901L)) && \
!defined(__APPLE__)
extern int vsnprintf(char *str, size_t size, const char *format, va_list ap)
;
#endif
int nWrite;
int nAvail;
nAvail = pStr->nAlloc - pStr->n;
nWrite = vsnprintf(pStr->z + pStr->n, nAvail, zFormat, ap1);
if( nWrite>=nAvail ){
lsmStringExtend(pStr, nWrite+1);
if( pStr->nAlloc==0 ) return;
nWrite = vsnprintf(pStr->z + pStr->n, nWrite+1, zFormat, ap2);
}
pStr->n += nWrite;
pStr->z[pStr->n] = 0;
}
void lsmStringAppendf(LsmString *pStr, const char *zFormat, ...){
va_list ap, ap2;
va_start(ap, zFormat);
va_start(ap2, zFormat);
lsmStringVAppendf(pStr, zFormat, ap, ap2);
va_end(ap);
va_end(ap2);
}
int lsmStrlen(const char *zName){
int nRet = 0;
while( zName[nRet] ) nRet++;
return nRet;
}
char *lsmMallocPrintf(lsm_env *pEnv, const char *zFormat, ...){
LsmString s;
va_list ap, ap2;
lsmStringInit(&s, pEnv);
va_start(ap, zFormat);
va_start(ap2, zFormat);
lsmStringVAppendf(&s, zFormat, ap, ap2);
va_end(ap);
va_end(ap2);
if( s.n<0 ) return 0;
return (char *)lsmReallocOrFree(pEnv, s.z, s.n+1);
}