From 07ddfbd5994267e0f1aba52040a25db90ed8924b Mon Sep 17 00:00:00 2001 From: Kirill Volinsky Date: Sat, 22 Dec 2012 21:43:08 +0000 Subject: IMO2sProxy reverted. maybe will be needed later git-svn-id: http://svn.miranda-ng.org/main/trunk@2809 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/!NotAdopted/IMO2sProxy/src/common/fifo.c | 144 +++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 plugins/!NotAdopted/IMO2sProxy/src/common/fifo.c (limited to 'plugins/!NotAdopted/IMO2sProxy/src/common/fifo.c') diff --git a/plugins/!NotAdopted/IMO2sProxy/src/common/fifo.c b/plugins/!NotAdopted/IMO2sProxy/src/common/fifo.c new file mode 100644 index 0000000000..d703ac33d4 --- /dev/null +++ b/plugins/!NotAdopted/IMO2sProxy/src/common/fifo.c @@ -0,0 +1,144 @@ +#include +#include +#include "fifo.h" + +struct _tagFIFO +{ + unsigned int uiCount; + unsigned int uiCapacity; + unsigned int uiActInd; + char *acStorage; +}; + + +TYP_FIFO *Fifo_Init(unsigned int uiCapacity) +{ + TYP_FIFO *pstHandle; + + pstHandle = (TYP_FIFO *)malloc(sizeof(TYP_FIFO)); + if (!pstHandle) return NULL; + pstHandle->uiCount = pstHandle->uiActInd = 0; + pstHandle->uiCapacity = uiCapacity; + if (uiCapacity == 0) + pstHandle->acStorage = NULL; + else + { + pstHandle->acStorage = (char *)malloc(uiCapacity); + if (!pstHandle->acStorage) + { + free (pstHandle); + return NULL; + } + } + return pstHandle; +} + +void Fifo_Exit(TYP_FIFO *pstHandle) +{ + if (pstHandle->acStorage) + free (pstHandle->acStorage); + free (pstHandle); +} + +char *Fifo_AllocBuffer(TYP_FIFO *pstHandle, unsigned int uiPCount) +{ + unsigned int uiCount = pstHandle->uiCount; + + if (!Fifo_Add (pstHandle, NULL, uiPCount)) return NULL; + return &pstHandle->acStorage[pstHandle->uiActInd+uiCount]; + +} + +BOOL Fifo_Add(TYP_FIFO *pstHandle, char *acPBytes, unsigned int uiPCount) +{ + unsigned int uiFree; + + if (uiPCount == 0) return TRUE; + if (pstHandle->uiCapacity == 0) + { + if (!(pstHandle->acStorage = (char *)calloc(1, uiPCount))) + return FALSE; + if (acPBytes) + memcpy(pstHandle->acStorage, acPBytes, uiPCount); + else + memset(pstHandle->acStorage, 0, uiPCount); + pstHandle->uiCapacity = pstHandle->uiCount = uiPCount; + pstHandle->uiActInd = 0; + } + else + { + uiFree = pstHandle->uiCapacity-(pstHandle->uiActInd+pstHandle->uiCount); + if (uiFree < uiPCount) + { + if (pstHandle->uiActInd>=uiPCount && pstHandle->uiActInd*4>pstHandle->uiCount) + { + memmove(pstHandle->acStorage, pstHandle->acStorage+pstHandle->uiActInd, pstHandle->uiCount); + pstHandle->uiActInd = 0; + } + else + { + char *acBuf; + unsigned int uiNewLen; + + if (pstHandle->uiCapacity*2 < + pstHandle->uiCount+pstHandle->uiActInd+uiPCount) + uiNewLen = pstHandle->uiCount+pstHandle->uiActInd+uiPCount; + else + uiNewLen = pstHandle->uiCapacity*2; + acBuf = realloc(pstHandle->acStorage, uiNewLen); + if (acBuf == NULL) return FALSE; + pstHandle->acStorage = acBuf; + memset (acBuf+pstHandle->uiCapacity, 0, uiNewLen-pstHandle->uiCapacity); + pstHandle->uiCapacity = uiNewLen; + } + } + if (acPBytes) + memcpy (&pstHandle->acStorage[pstHandle->uiActInd+pstHandle->uiCount], acPBytes, uiPCount); + else + memset (&pstHandle->acStorage[pstHandle->uiActInd+pstHandle->uiCount], 0, uiPCount); + + pstHandle->uiCount += uiPCount; + } + + return TRUE; +} + +BOOL Fifo_AddString(TYP_FIFO *pstHandle, char *pszString) +{ + BOOL bRet; + + while (pstHandle->uiCount && pstHandle->acStorage[pstHandle->uiCount+pstHandle->uiActInd-1]==0) + pstHandle->uiCount--; + bRet = Fifo_Add (pstHandle, pszString, strlen(pszString)+1); + return bRet; +} + +char *Fifo_Get (TYP_FIFO *pstHandle, unsigned int *uiPCount) +{ + unsigned int uiCount; + char *pRet; + + if (!uiPCount) uiCount = pstHandle->uiCount; + else + { + if (pstHandle->uiCount < *uiPCount) + *uiPCount = pstHandle->uiCount; + uiCount = *uiPCount; + } + if (!uiCount) return NULL; + + pRet = &pstHandle->acStorage[pstHandle->uiActInd]; + pstHandle->uiActInd += uiCount; + pstHandle->uiCount -= uiCount; + return pRet; +} + +void Fifo_Reset (TYP_FIFO *pstHandle) +{ + pstHandle->uiCount = pstHandle->uiActInd = 0; +} + +unsigned int Fifo_Count (TYP_FIFO *pstHandle) +{ + return pstHandle->uiCount; +} -- cgit v1.2.3