diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2005-10-18 16:10:18 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2005-10-18 16:10:18 +0000 |
commit | 2c7187de39b1e731057b47752e3989a954acffb8 (patch) | |
tree | a4e4b208f5e5fb590dc970b0ec0e00885e728711 /stringbuf.c | |
parent | bb0d1da6daeede6fc1236bff37814353ef5480ee (diff) | |
download | rsyslog-2c7187de39b1e731057b47752e3989a954acffb8.tar.gz rsyslog-2c7187de39b1e731057b47752e3989a954acffb8.tar.xz rsyslog-2c7187de39b1e731057b47752e3989a954acffb8.zip |
begin implementing BSD-style program blocks
Diffstat (limited to 'stringbuf.c')
-rwxr-xr-x | stringbuf.c | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/stringbuf.c b/stringbuf.c index 6d6d122d..28b3c14f 100755 --- a/stringbuf.c +++ b/stringbuf.c @@ -56,7 +56,7 @@ rsRetVal rsCStrConstructFromszStr(rsCStrObj **ppThis, char *sz) if((pThis = rsCStrConstruct()) == NULL) return RS_RET_OUT_OF_MEMORY; - pThis->iStrLen = strlen(sz); + pThis->iBufSize = pThis->iStrLen = strlen(sz); if((pThis->pBuf = (char*) malloc(sizeof(char) * pThis->iStrLen)) == NULL) { RSFREEOBJ(pThis); return RS_RET_OUT_OF_MEMORY; @@ -69,6 +69,33 @@ rsRetVal rsCStrConstructFromszStr(rsCStrObj **ppThis, char *sz) return RS_RET_OK; } +/* construct from CStr object. only the counted string is + * copied, not the szString. + * rgerhards 2005-10-18 + */ +rsRetVal rsCStrConstructFromCStr(rsCStrObj **ppThis, rsCStrObj *pFrom) +{ + rsCStrObj *pThis; + + assert(ppThis != NULL); + rsCHECKVALIDOBJECT(pFrom, OIDrsCStr); + + if((pThis = rsCStrConstruct()) == NULL) + return RS_RET_OUT_OF_MEMORY; + + pThis->iBufSize = pThis->iStrLen = pFrom->iStrLen; + if((pThis->pBuf = (char*) malloc(sizeof(char) * pThis->iStrLen)) == NULL) { + RSFREEOBJ(pThis); + return RS_RET_OUT_OF_MEMORY; + } + + /* copy properties */ + memcpy(pThis->pBuf, pFrom->pBuf, pThis->iStrLen); + + *ppThis = pThis; + return RS_RET_OK; +} + void rsCStrDestruct(rsCStrObj *pThis) { @@ -168,6 +195,46 @@ rsRetVal rsCStrAppendChar(rsCStrObj *pThis, char c) } +/* Sets the string object to the classigal sz-string provided. + * Any previously stored vlaue is discarded. If a NULL pointer + * the the new value (pszNew) is provided, an empty string is + * created (this is NOT an error!). Property iAllocIncrement is + * not modified by this function. + * rgerhards, 2005-10-18 + */ +rsRetVal rsCStrSetSzStr(rsCStrObj *pThis, char *pszNew) +{ + rsCHECKVALIDOBJECT(pThis, OIDrsCStr); + + if(pThis->pBuf != NULL) + free(pThis->pBuf); + if(pThis->pszBuf != NULL) + free(pThis->pszBuf); + if(pszNew == NULL) { + pThis->iStrLen = 0; + pThis->iBufSize = 0; + pThis->pBuf = NULL; + pThis->pszBuf = NULL; + } else { + pThis->iStrLen = strlen(pszNew); + pThis->iBufSize = pThis->iStrLen; + pThis->pszBuf = NULL; + /* iAllocIncrement is NOT modified! */ + + /* now save the new value */ + if((pThis->pBuf = (char*) malloc(sizeof(char) * pThis->iStrLen)) == NULL) { + RSFREEOBJ(pThis); + return RS_RET_OUT_OF_MEMORY; + } + + /* we do NOT need to copy the \0! */ + memcpy(pThis->pBuf, pszNew, pThis->iStrLen); + } + + return RS_RET_OK; +} + + /* Converts the CStr object to a classical zero-terminated C string * and returns that string. The caller must not free it and must not * destroy the CStr object as long as the ascii string is used. |