Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Buffer.cc

Go to the documentation of this file.
00001 #include "Buffer.h"
00002 #include <string.h>
00003 
00004 Buffer::Buffer(int size)
00005   : data(0), capacity(0), limit(0), position(0) {
00006 
00007   if (size > 0) {
00008     data = new char[size];
00009     capacity = size;
00010   } else {
00011     capacity = 0;
00012   }
00013   limit = capacity;
00014 }
00015 
00016 Buffer::Buffer(const Buffer& rhs)
00017   : data(0), capacity(0), limit(0), position(0) {
00018     
00019   *this = rhs;
00020 }
00021 
00022 Buffer::~Buffer() {
00023   delete[] data;
00024 }
00025 
00026 Buffer& Buffer::operator=(const Buffer& rhs) {
00027     
00028   if (this != &rhs) {
00029     if (capacity != rhs.GetCapacity()) {
00030       delete[] data;
00031       data = 0;
00032       capacity = rhs.GetCapacity();
00033       data = new char[capacity];
00034     }
00035     
00036     if (capacity > 0) {     
00037       memcpy(data, rhs.GetData(), capacity);
00038     }
00039     
00040     position = rhs.GetPosition();
00041     limit = rhs.GetLimit();
00042   }
00043   return *this;
00044 }
00045 
00046 void Buffer::SetPosition(int pos) {
00047   if (pos < 0) {
00048     position = 0;
00049   } else {
00050     position = min(pos, limit);
00051   }
00052 }
00053 
00054 void Buffer::SetLimit(int lim) {
00055   if (lim < 0) {
00056     limit = 0;
00057   } else {
00058     limit = min(lim, capacity);
00059   }
00060   position = min(position, limit);
00061 }
00062 
00063 void Buffer::SetCapacity(int size) {
00064   if (size == capacity) {
00065     return;
00066   }
00067   if (size > 0) {
00068     char* newData = new char[size];
00069     if (data != 0) {
00070       memcpy(newData, data, min(capacity, size)); 
00071     }
00072     capacity = size;
00073     delete[] data;
00074     data = newData;
00075   } else {
00076     capacity = 0;
00077     delete[] data;
00078     data = 0;
00079   }  
00080   limit = min(limit, capacity);
00081   position = min(position, limit);
00082 }
00083 
00084 bool Buffer::Fill(const char*&src, int& srcLen) {
00085   if ((src == 0) || (srcLen < 1)) {
00086     return IsFull();
00087   }
00088   
00089   const int charsToRead = limit - position;
00090   if (charsToRead < 1) {
00091     return true;
00092   }
00093   
00094   if (charsToRead > srcLen) {
00095     memcpy(&data[position], src, srcLen);
00096     position += srcLen;
00097     src += srcLen;
00098     srcLen = 0;
00099     return false;
00100   } else {
00101     memcpy(&data[position], src, charsToRead);
00102     position += charsToRead;
00103     src += charsToRead;
00104     srcLen -= charsToRead;
00105     return true;
00106   }
00107 }
00108 
00109 /*! @file
00110  * @brief Describes Buffer, a general memory management data structure, featuring position, limit, and capacity marks
00111  * @author Alexander Klyubin (A.Kljubin AT herts ac uk) (Creator)
00112  * Submitted as part of "Full-duplex Audio Streaming" patch
00113  */

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:36 2016 by Doxygen 1.6.3