Add DynStringInsertWithLen and have DynStringInsert use it
r3062 | dcl | 2011-02-18 11:13:22 +1100 (Fri, 18 Feb 2011) | 1 line
This commit is contained in:
44
dynstring.c
44
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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user