lsm_str.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. ** Turn bulk and uninitialized memory into an LsmString object
  18. */
  19. void lsmStringInit(LsmString *pStr, lsm_env *pEnv){
  20. memset(pStr, 0, sizeof(pStr[0]));
  21. pStr->pEnv = pEnv;
  22. }
  23. /*
  24. ** Increase the memory allocated for holding the string. Realloc as needed.
  25. **
  26. ** If a memory allocation error occurs, set pStr->n to -1 and free the existing
  27. ** allocation. If a prior memory allocation has occurred, this routine is a
  28. ** no-op.
  29. */
  30. int lsmStringExtend(LsmString *pStr, int nNew){
  31. assert( nNew>0 );
  32. if( pStr->n<0 ) return LSM_NOMEM;
  33. if( pStr->n + nNew >= pStr->nAlloc ){
  34. int nAlloc = pStr->n + nNew + 100;
  35. char *zNew = lsmRealloc(pStr->pEnv, pStr->z, nAlloc);
  36. if( zNew==0 ){
  37. lsmFree(pStr->pEnv, pStr->z);
  38. nAlloc = 0;
  39. pStr->n = -1;
  40. }
  41. pStr->nAlloc = nAlloc;
  42. pStr->z = zNew;
  43. }
  44. return (pStr->z ? LSM_OK : LSM_NOMEM_BKPT);
  45. }
  46. /*
  47. ** Clear an LsmString object, releasing any allocated memory that it holds.
  48. ** This also clears the error indication (if any).
  49. */
  50. void lsmStringClear(LsmString *pStr){
  51. lsmFree(pStr->pEnv, pStr->z);
  52. lsmStringInit(pStr, pStr->pEnv);
  53. }
  54. /*
  55. ** Append N bytes of text to the end of an LsmString object. If
  56. ** N is negative, append the entire string.
  57. **
  58. ** If the string is in an error state, this routine is a no-op.
  59. */
  60. int lsmStringAppend(LsmString *pStr, const char *z, int N){
  61. int rc;
  62. if( N<0 ) N = (int)strlen(z);
  63. rc = lsmStringExtend(pStr, N+1);
  64. if( pStr->nAlloc ){
  65. memcpy(pStr->z+pStr->n, z, N+1);
  66. pStr->n += N;
  67. }
  68. return rc;
  69. }
  70. int lsmStringBinAppend(LsmString *pStr, const u8 *a, int n){
  71. int rc;
  72. rc = lsmStringExtend(pStr, n);
  73. if( pStr->nAlloc ){
  74. memcpy(pStr->z+pStr->n, a, n);
  75. pStr->n += n;
  76. }
  77. return rc;
  78. }
  79. /*
  80. ** Append printf-formatted content to an LsmString.
  81. */
  82. void lsmStringVAppendf(
  83. LsmString *pStr,
  84. const char *zFormat,
  85. va_list ap1,
  86. va_list ap2
  87. ){
  88. #if (!defined(__STDC_VERSION__) || (__STDC_VERSION__<199901L)) && \
  89. !defined(__APPLE__)
  90. extern int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  91. /* Compatibility crutch for C89 compilation mode. sqlite3_vsnprintf()
  92. does not work identically and causes test failures if used here.
  93. For the time being we are assuming that the target has vsnprintf(),
  94. but that is not guaranteed to be the case for pure C89 platforms.
  95. */;
  96. #endif
  97. int nWrite;
  98. int nAvail;
  99. nAvail = pStr->nAlloc - pStr->n;
  100. nWrite = vsnprintf(pStr->z + pStr->n, nAvail, zFormat, ap1);
  101. if( nWrite>=nAvail ){
  102. lsmStringExtend(pStr, nWrite+1);
  103. if( pStr->nAlloc==0 ) return;
  104. nWrite = vsnprintf(pStr->z + pStr->n, nWrite+1, zFormat, ap2);
  105. }
  106. pStr->n += nWrite;
  107. pStr->z[pStr->n] = 0;
  108. }
  109. void lsmStringAppendf(LsmString *pStr, const char *zFormat, ...){
  110. va_list ap, ap2;
  111. va_start(ap, zFormat);
  112. va_start(ap2, zFormat);
  113. lsmStringVAppendf(pStr, zFormat, ap, ap2);
  114. va_end(ap);
  115. va_end(ap2);
  116. }
  117. int lsmStrlen(const char *zName){
  118. int nRet = 0;
  119. while( zName[nRet] ) nRet++;
  120. return nRet;
  121. }
  122. /*
  123. ** Write into memory obtained from lsm_malloc().
  124. */
  125. char *lsmMallocPrintf(lsm_env *pEnv, const char *zFormat, ...){
  126. LsmString s;
  127. va_list ap, ap2;
  128. lsmStringInit(&s, pEnv);
  129. va_start(ap, zFormat);
  130. va_start(ap2, zFormat);
  131. lsmStringVAppendf(&s, zFormat, ap, ap2);
  132. va_end(ap);
  133. va_end(ap2);
  134. if( s.n<0 ) return 0;
  135. return (char *)lsmReallocOrFree(pEnv, s.z, s.n+1);
  136. }