diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/kvilib/ext/kvi_databuffer.cpp | 135 | ||||
| -rw-r--r-- | src/kvilib/ext/kvi_databuffer.h | 58 | ||||
| -rw-r--r-- | src/kvilib/ext/kvi_doublebuffer.cpp | 82 | ||||
| -rw-r--r-- | src/kvilib/ext/kvi_doublebuffer.h | 62 |
4 files changed, 193 insertions, 144 deletions
diff --git a/src/kvilib/ext/kvi_databuffer.cpp b/src/kvilib/ext/kvi_databuffer.cpp new file mode 100644 index 000000000..02a74c5f0 --- /dev/null +++ b/src/kvilib/ext/kvi_databuffer.cpp @@ -0,0 +1,135 @@ +//============================================================================= +// +// File : kvi_databuffer.cpp +// Creation date : Thu Aug 23 17:04:24 2001 GMT by Szymon Stefanek +// +// This file is part of the KVirc irc client distribution +// Copyright (C) 2001-2008 Szymon Stefanek (pragma at kvirc dot net) +// +// This program is FREE software. You can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your opinion) any later version. +// +// This program is distributed in the HOPE that it will be USEFUL, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, write to the Free Software Foundation, +// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +//============================================================================= + + +#define _KVI_DEBUG_CHECK_RANGE_ + +#include "kvi_debug.h" + +#include "kvi_databuffer.h" +#include "kvi_malloc.h" +#include "kvi_memmove.h" + +// FIXME: this could resize in chunks!...this would be damn faster :) + +KviDataBuffer::KviDataBuffer(int uSize,const unsigned char * data) +{ + __range_valid(uSize > 0); + m_uSize = uSize; + m_pData = (unsigned char *)kvi_malloc(sizeof(unsigned char) * uSize); + if(data)kvi_memmove(m_pData,data,uSize); +} + +KviDataBuffer::KviDataBuffer() +{ + m_uSize = 0; + m_pData = 0; +} + +KviDataBuffer::~KviDataBuffer() +{ + if(m_pData) + { + __range_valid(m_uSize); + kvi_free(m_pData); + } +} + +int KviDataBuffer::find(const unsigned char * block,int uSize) +{ + if(uSize < 1)return -1; + if(uSize > m_uSize)return -1; + + int uSearchSize = (m_uSize - uSize) + 1; + + for(int i=0;i<uSearchSize;i++) + { + if(m_pData[i] == *block) + { + // good beginning + if(uSize == 1)return i; + int j; + for(j = 1;j<uSize;j++) + { + if(m_pData[i + j] != block[j]) + { + j = 0; + break; + } + } + if(j > 0)return i; + } + } + + return -1; +} + +int KviDataBuffer::find(unsigned char c) +{ + const unsigned char * p = m_pData; + const unsigned char * e = p + m_uSize; + while(p < e) + { + if(*p == c)return (p - m_pData); + p++; + } + return -1; +} + + +void KviDataBuffer::remove(int uSize) +{ + __range_valid((uSize <= m_uSize) && (uSize > 0)); + + m_uSize -= uSize; + + if(m_uSize > 0) + { + kvi_memmove(m_pData,m_pData + uSize,m_uSize); + m_pData = (unsigned char *)kvi_realloc(m_pData,m_uSize * sizeof(unsigned char)); + } else { + kvi_free(m_pData); + m_pData = 0; + } +} + +void KviDataBuffer::resize(int uSize) +{ + __range_valid(uSize >= 0); + if(uSize > 0) + { + m_pData = (unsigned char *)kvi_realloc(m_pData,uSize * sizeof(unsigned char)); + } else { + kvi_free(m_pData); + m_pData = 0; + } + m_uSize = uSize; +} + +void KviDataBuffer::append(const unsigned char * data,int uSize) +{ + m_pData = (unsigned char *)kvi_realloc(m_pData,m_uSize + uSize); + kvi_memmove(m_pData + m_uSize,data,uSize); + m_uSize += uSize; +} diff --git a/src/kvilib/ext/kvi_databuffer.h b/src/kvilib/ext/kvi_databuffer.h new file mode 100644 index 000000000..bbeaf72ff --- /dev/null +++ b/src/kvilib/ext/kvi_databuffer.h @@ -0,0 +1,58 @@ +#ifndef _KVI_DATABUFFER_H_ +#define _KVI_DATABUFFER_H_ +//============================================================================= +// +// File : kvi_databuffer.h +// Creation date : Thu Aug 23 17:04:25 2001 GMT by Szymon Stefanek +// +// This file is part of the KVirc irc client distribution +// Copyright (C) 2001 Szymon Stefanek (pragma at kvirc dot net) +// +// This program is FREE software. You can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your opinion) any later version. +// +// This program is distributed in the HOPE that it will be USEFUL, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, write to the Free Software Foundation, +// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +//============================================================================= + +#include "kvi_settings.h" +#include "kvi_heapobject.h" + +class KVILIB_API KviDataBuffer : public KviHeapObject +{ +public: + // uSize MUST be greater than 0 + // if data is non-zero, it MUST point to a buffer at least uSize bytes long + // and the data is COPIED from that buffer! + KviDataBuffer(int uSize,const unsigned char * data = 0); + KviDataBuffer(); + ~KviDataBuffer(); +private: + int m_uSize; + unsigned char * m_pData; +public: + int size() const { return m_uSize; }; + unsigned char * data() const { return m_pData; }; + // uSize MUST be smaller or equal to size() + // consumes data! + void remove(int uSize); + void clear(){ if(m_uSize > 0)remove(m_uSize); }; + // uSize MUST be greater than 0 + void resize(int uSize); + void addSize(int uSize){ resize(m_uSize + uSize); }; + void append(const unsigned char * data,int uSize); + void append(const KviDataBuffer &b){ append(b.data(),b.size()); }; + int find(unsigned char c); + int find(const unsigned char * block,int uSize); +}; + +#endif //_KVI_DATABUFFER_H_ diff --git a/src/kvilib/ext/kvi_doublebuffer.cpp b/src/kvilib/ext/kvi_doublebuffer.cpp deleted file mode 100644 index 343516281..000000000 --- a/src/kvilib/ext/kvi_doublebuffer.cpp +++ /dev/null @@ -1,82 +0,0 @@ -//============================================================================= -// -// File : kvi_doublebuffer.cpp -// Creation date : Fri 27 Jan 2006 18:59:54 by Szymon Stefanek -// -// This file is part of the KVIrc IRC Client distribution -// Copyright (C) 2006-2008 Szymon Stefanek <pragma at kvirc dot net> -// -// This program is FREE software. You can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your opinion) any later version. -// -// This program is distributed in the HOPE that it will be USEFUL, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, write to the Free Software Foundation, -// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// -//============================================================================= - - -#include "kvi_doublebuffer.h" - -static QPixmap * g_pMemoryPixmap = 0; -static unsigned int g_uMaxRequestedWidth = 0; -static unsigned int g_uMaxRequestedHeight = 0; - -KviDoubleBuffer::KviDoubleBuffer(unsigned int uWidth,unsigned int uHeight) -{ - if((g_pMemoryPixmap->width() < uWidth) || (g_pMemoryPixmap->height() < uHeight)) - { - // The memory buffer is too small - // There is either no such user requirement or it has grown by the meantime - unsigned int uMaxW = uWidth > g_pMemoryPixmap->width() ? uWidth : g_pMemoryPixmap->width(); - unsigned int uMaxH = uHeight > g_pMemoryPixmap->height() ? uHeight : g_pMemoryPixmap->height(); - - // QT4SUX: QPixmap::resize() is missing (it's a widely used function and assigning a new QPixmap() seems to be slower and not intuitive) - *g_pMemoryPixmap = QPixmap(uMaxW,uMaxH); - } - - if(uWidth > g_uMaxRequestedWidth)g_uMaxRequestedWidth = uWidth; - if(uHeight > g_uMaxRequestedHeight)g_uMaxRequestedHeight = uHeight; -} - -KviDoubleBuffer::~KviDoubleBuffer() -{ - // We never shrink here (it's time consuming) -} - -QPixmap * KviDoubleBuffer::pixmap() -{ - return g_pMemoryPixmap; -} - -void KviDoubleBuffer::init() -{ - if(g_pMemoryPixmap)return; - g_pMemoryPixmap = new QPixmap(); -} - -void KviDoubleBuffer::done() -{ - if(!g_pMemoryPixmap)return; - delete g_pMemoryPixmap; - g_pMemoryPixmap = 0; -} - -void KviDoubleBuffer::heartbeat() -{ - if(((g_uMaxRequestedHeight + 64) < g_pMemoryPixmap->height()) || ((g_uMaxRequestedWidth + 64) < g_pMemoryPixmap->width())) - { - // do shrink :) - // QT4SUX: QPixmap::resize() is missing (it's a widely used function and assigning a new QPixmap() seems to be slower and not intuitive) - *g_pMemoryPixmap = QPixmap(g_uMaxRequestedWidth,g_uMaxRequestedHeight); - } - g_uMaxRequestedHeight = 0; - g_uMaxRequestedWidth = 0; -} diff --git a/src/kvilib/ext/kvi_doublebuffer.h b/src/kvilib/ext/kvi_doublebuffer.h deleted file mode 100644 index f950da58a..000000000 --- a/src/kvilib/ext/kvi_doublebuffer.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef _KVI_DOUBLEBUFFER_H_ -#define _KVI_DOUBLEBUFFER_H_ -//============================================================================= -// -// File : kvi_doublebuffer.h -// Creation date : Fri 27 Jan 2006 18:59:54 by Szymon Stefanek -// -// This file is part of the KVIrc IRC Client distribution -// Copyright (C) 2006-2008 Szymon Stefanek <pragma at kvirc dot net> -// -// This program is FREE software. You can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your opinion) any later version. -// -// This program is distributed in the HOPE that it will be USEFUL, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, write to the Free Software Foundation, -// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// -//============================================================================= - -#include "kvi_settings.h" - -#include <QPixmap> - -// -// This class is basically a huge shared memory pixmap meant to be used in double-buffer -// painting operations. The memory buffer is resized on the fly so you will always obtain -// a pixmap that is at least of the specified size. The problem is that this is a time -// consuming operation (not good in a paint event). We solve it by keeping the buffer -// with the greatest requested size in the last N minutes. -// -// The keyword in all this thingie is "memory is cheap, processing time is not". -// We know in advance that KVIrc needs a huge double buffer anyway... -// So we basically grow instantly but we are really lazy at shrinking. -// - -class KVILIB_API KviDoubleBuffer -{ -public: - KviDoubleBuffer(unsigned int uWidth,unsigned int uHeight); - ~KviDoubleBuffer(); -public: - // This returns a pointer to the memory buffer. The buffer is at least - // of the size declared in the constructor. - QPixmap * pixmap(); - - // The stuff below is internal (used only by KviApp) - - // to be called at application initialisation and cleanup - static void init(); - static void done(); - // this has to be called at sensible intervals (like 2 minutes) - static void heartbeat(); -}; - -#endif //!_KVI_DOUBLEBUFFER_H_ |
