diff --git a/dynstring.c b/dynstring.c index e6e92fcb..ea9562c1 100644 --- a/dynstring.c +++ b/dynstring.c @@ -178,6 +178,7 @@ return 1; } /*---------------------------------------------------------------------------*/ +#if 0 /* to be removed DFC */ int DynStringInsert(pDynString self, char *pText, int iPos) { int iRequested, iRet, iPush, iRest; @@ -216,6 +217,49 @@ self->iTextLen = iRequested; return 1; } +/*---------------------------------------------------------------------------*/ +#endif + int DynStringInsertWithLen(pDynString self, char *pText, int iPos, int len) + { + int iRequested, iRet, iPush, iRest; + char *pPtr; + + assert(self); + assert(self->iMAGIC == DYNMAGIC); + + /* provide space */ + iPush = len; + iRequested = self->iTextLen + iPush; + if(iRequested >= self->iBufferSize) + { + iRet = Resize(self,iRequested); + if(!iRet) + { + return 0; + } + } + + /* we need a temporary buffer to hold the backend of the string */ + iRest = self->iTextLen - iPos; + pPtr = (char *)malloc((iRest)*sizeof(char)); + if(!pPtr) + { + return 0; + } + memcpy(pPtr, &self->pBuffer[iPos], (iRest)*sizeof(char)); + + /* OK build the result string */ + memcpy(&self->pBuffer[iPos], pText, len*sizeof(char)); + memcpy(&self->pBuffer[iPos + len],pPtr, (iRest)*sizeof(char)); + free(pPtr); + self->iTextLen = iRequested; + return 1; + } +/*---------------------------------------------------------------------------*/ + int DynStringInsert(pDynString self, char *pText, int iPos) + { + return DynStringInsertWithLen(self, pText, iPos, strlen(pText)); + } /*--------------------------------------------------------------------------*/ int DynStringReplaceWithLen(pDynString self, char *pText, int iPos, int len) { int iRequested, iRet; diff --git a/dynstring.h b/dynstring.h index 8f91843d..32232bfc 100644 --- a/dynstring.h +++ b/dynstring.h @@ -94,4 +94,9 @@ /* Useful for making dynstrings containing null chars and other non-Ascii chars */ + + int DynStringInsertWithLen(pDynString self, char *pText, int iPos, int len); + /* + Useful for making dynstrings containing null chars and other non-Ascii chars + */ #endif