123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #ifndef __C_DATAPUMP_H__
- #define __C_DATAPUMP_H__
- #include <stdlib.h>
- #include <memory.h>
- #include <stddef.h>
- #pragma intrinsic(memcpy,memset)
- template<class T> class C_DATAPUMP {
- private:
- protected:
- T *BufferBottom;
- T *BufferTop;
- T *BufferStart;
- T *BufferEnd;
- virtual void addItems(T *inputBuffer, size_t inputSize) {
- if(inputBuffer && inputSize) {
- memcpy(BufferEnd,inputBuffer,inputSize*sizeof(T));
- BufferEnd += inputSize;
- if(BufferEnd >= BufferTop) BufferEnd = BufferBottom + (BufferEnd-BufferTop);
- }
- }
- virtual void delItems(int where, size_t numItems) {
- if(numItems > 0) {
- if(numItems > size()) {
- BufferEnd = BufferStart;
- } else {
- if(where == 0) {
- BufferStart += numItems;
- if(BufferStart >= BufferTop) BufferStart = BufferBottom + (BufferTop-BufferStart);
- } else if(where == 1) {
- BufferEnd -= numItems;
- if(BufferEnd < BufferBottom) BufferEnd = BufferTop - (BufferBottom-BufferEnd);
- }
- }
- }
- }
- virtual void getItems(T *outputBuffer, size_t outputSize) {
- if(outputBuffer && outputSize) {
- memcpy(outputBuffer,BufferStart,outputSize*sizeof(T));
- }
- }
- public:
- C_DATAPUMP(int bufferSize) {
- BufferBottom = NULL;
- BufferTop = NULL;
- BufferStart = NULL;
- BufferEnd = NULL;
- resizeBuffer(bufferSize);
- }
- virtual ~C_DATAPUMP() {
- if(getBufferSize() && BufferBottom) {
- free(BufferBottom);
- BufferBottom = NULL;
- }
- }
- virtual void resizeBuffer(size_t bufferSize) {
-
- if(bufferSize) {
- if(getBufferSize() != bufferSize) {
- if(BufferBottom && BufferTop && getBufferSize()) {
- if(getBufferSize() > bufferSize) {
- BufferTop -= getBufferSize()-bufferSize;
- invalidate();
- } else {
- T *newBuffer = (T *)malloc(bufferSize * sizeof(T));
-
- BufferEnd = newBuffer + get(newBuffer,bufferSize);
- free(BufferBottom);
- BufferBottom = newBuffer;
- BufferTop = BufferBottom + bufferSize;
- BufferStart = BufferBottom;
-
- }
- } else {
- BufferBottom = (T *)malloc(bufferSize * sizeof(T));
- BufferTop = BufferBottom + bufferSize;
- invalidate();
- }
- }
- }
- }
- virtual size_t size() {
- return BufferEnd >= BufferStart ? BufferEnd-BufferStart : (BufferTop-BufferStart)+(BufferEnd-BufferBottom);
- }
- virtual size_t put(T *inputBuffer, size_t inputSize) {
-
- size_t retval = 0;
- if(inputBuffer && inputSize) {
- size_t fitting = ((BufferTop-BufferBottom)-1) - size();
- if(fitting > inputSize) fitting = inputSize;
- retval = fitting;
- if(fitting > 0) {
- T *bufptr = inputBuffer;
- size_t top = BufferEnd >= BufferStart ? BufferTop-BufferEnd : 0;
- size_t bottom = BufferEnd >= BufferStart ? BufferStart-BufferBottom : (BufferStart-BufferEnd);
- if(top > 0) {
- if(top > fitting) top = fitting;
- addItems(bufptr,top);
- fitting -= top;
- bufptr += top;
- }
- if(bottom > 0 && fitting > 0) {
- if(bottom > fitting) bottom = fitting;
- addItems(bufptr,bottom);
- }
- }
- }
- return retval;
- }
- virtual size_t get(T *outputBuffer, size_t outputSize) {
-
- size_t retval = 0;
- if(outputBuffer && outputSize) {
- size_t fitting = size();
- if(fitting > outputSize) fitting = outputSize;
- retval = fitting;
- if(fitting > 0) {
- T *bufptr = outputBuffer;
- size_t top = BufferEnd >= BufferStart ? BufferEnd-BufferStart : BufferTop-BufferStart;
- size_t bottom = BufferEnd >= BufferStart ? 0 : BufferEnd-BufferBottom;
- if(top > 0) {
- if(top > fitting) top = fitting;
- getItems(bufptr,top);
- delItems(0,top);
- fitting -= top;
- bufptr += top;
- }
- if(bottom > 0 && fitting > 0) {
- if(bottom > fitting) bottom = fitting;
- getItems(bufptr,bottom);
- delItems(0,bottom);
- }
- }
- }
- return retval;
- }
- virtual size_t getBufferSize() {
- return BufferTop-BufferBottom;
- }
- virtual void invalidate() {
- BufferStart = BufferEnd = BufferBottom;
- }
- };
- #endif
|