[DEV] change the buffer nomenclature

This commit is contained in:
Edouard DUPIN 2013-09-19 21:29:48 +02:00
parent bb085be53d
commit 0069f83679
3 changed files with 186 additions and 151 deletions

View File

@ -48,46 +48,46 @@ namespace etk
class Buffer
{
private:
int8_t* m_data; //!< pointer on the curetn table of Data
int32_t m_allocated; //!< Current allocated size
int8_t* m_data; //!< pointer on the curetn table of Data
int32_t m_allocated; //!< Current allocated size
// empty part of the buffer data
int32_t m_gapStart; //!< points to the first character of the gap
int32_t m_gapEnd; //!< points to the first char after the gap
int32_t m_gapStart; //!< points to the first character of the gap
int32_t m_gapEnd; //!< points to the first char after the gap
public:
/**
* @brief Create an empty vector
* @param[in] count Minimum request size of the Buffer
* @param[in] _count Minimum request size of the Buffer
*/
Buffer(int32_t count = 0) :
Buffer(int32_t _count = 0) :
m_data(NULL),
m_allocated(0),
m_gapStart(0),
m_gapEnd(GAP_SIZE_MIN)
{
ChangeAllocation(count+GAP_SIZE_MIN);
ChangeAllocation(_count+GAP_SIZE_MIN);
}
/**
* @brief Re-copy constructor (copy all needed data)
* @param[in] obj Buffer that might be copy
* @param[in] _obj Buffer that might be copy
*/
Buffer(const etk::Buffer & obj) :
Buffer(const etk::Buffer& _obj) :
m_data(NULL),
m_allocated(obj.m_allocated),
m_gapStart(obj.m_gapStart),
m_gapEnd(obj.m_gapEnd)
m_allocated(_obj.m_allocated),
m_gapStart(_obj.m_gapStart),
m_gapEnd(_obj.m_gapEnd)
{
// allocate all same data
m_data = (int8_t *)malloc( m_allocated * sizeof(int8_t) );
TK_ASSERT(NULL!=m_data, "Error in data allocation");
// Copy all data ...
memcpy(m_data, obj.m_data, m_allocated * sizeof(int8_t) );
memcpy(m_data, _obj.m_data, m_allocated * sizeof(int8_t) );
}
/**
* @brief Destructor of the current Class
*/
~Buffer(void)
{
if (NULL!=m_data) {
if (m_data != NULL) {
free(m_data);
}
m_data = NULL;
@ -97,26 +97,29 @@ namespace etk
};
/**
* @brief Save in the current file open
* @param[in,out] myFile pointer on the file where data might be writed
* @param[in,out] _file Pointer on the file where data might be writed
* @return true if OK / false if an error occured
*/
bool DumpIn(etk::FSNode &file)
bool DumpIn(etk::FSNode& _file)
{
bool ret = true;
// write Data
(void)file.FileWrite(m_data, sizeof(int8_t), m_gapStart);
(void)file.FileWrite(&m_data[m_gapEnd], sizeof(int8_t), m_allocated - m_gapEnd);
(void)_file.FileWrite(m_data, sizeof(int8_t), m_gapStart);
(void)_file.FileWrite(&m_data[m_gapEnd], sizeof(int8_t), m_allocated - m_gapEnd);
return ret;
}
/**
* @brief Load in the current file open
* @param[in,out] myFile pointer on the file where data might be read
* @param[in,out] _myFile Pointer on the file where data might be read
* @return true if OK / false if an error occured
*/
bool DumpFrom(etk::FSNode &file)
bool DumpFrom(etk::FSNode& _file)
{
if (false == _file.FileOpenRead()) {
return false;
}
bool ret = true;
uint32_t length = file.FileSize();
uint32_t length = _file.FileSize();
// error case ...
if (length > 2000000000) {
return false;
@ -124,7 +127,7 @@ namespace etk
// allocate the current buffer :
ChangeAllocation(length + GAP_SIZE_MIN);
// insert Data
int32_t nbReadData = file.FileRead(&m_data[GAP_SIZE_MIN], sizeof(int8_t), length);
int32_t nbReadData = _file.FileRead(&m_data[GAP_SIZE_MIN], sizeof(int8_t), length);
TK_INFO("load data : filesize=" << length << ", readData=" << nbReadData);
// check ERROR
if (nbReadData != length) {
@ -134,55 +137,70 @@ namespace etk
// set the gapsize at the end ...
m_gapStart = 0;
m_gapEnd = GAP_SIZE_MIN;
_file.FileClose();
return ret;
}
/**
* @brief Re-copy operator
* @param[in] obj Buffer that might be copy
* @param[in] _obj Buffer that might be copy
* @return reference on the curent re-copy vector
*/
etk::Buffer& operator=(const etk::Buffer & obj)
etk::Buffer& operator=(const etk::Buffer& _obj)
{
if( this != &obj ) // avoid copy to itself
if( this != &_obj ) // avoid copy to itself
{
if (NULL!=m_data) {
free(m_data);
m_data = NULL;
}
// Set the new value
m_allocated = obj.m_allocated;
m_gapStart = obj.m_gapStart;
m_gapEnd = obj.m_gapEnd;
m_allocated = _obj.m_allocated;
m_gapStart = _obj.m_gapStart;
m_gapEnd = _obj.m_gapEnd;
// allocate all same data
m_data = (int8_t *)malloc( m_allocated * sizeof(int8_t) );
TK_ASSERT(NULL!=m_data, "Error in data allocation");
// Copy all data ...
memcpy(m_data, obj.m_data, m_allocated * sizeof(int8_t) );
memcpy(m_data, _obj.m_data, m_allocated * sizeof(int8_t) );
}
// Return the curent pointer
return *this;
}
/**
* @brief Operator [] : Get the data at the requested position (gap abstraction done).
* @param[in] pos Position in the buffer.
* @param[in] _pos Position in the buffer.
* @return Element at the request pos.
*/
int8_t operator[] (int32_t pos) const
int8_t operator[] (int32_t _pos) const
{
TK_ASSERT(0 <= pos || pos < Size(), "try to read an element non existing");
if (pos < m_gapStart) {
return m_data[pos];
TK_ASSERT(0 <= _pos || _pos < Size(), "try to read an element non existing");
if (_pos < m_gapStart) {
return m_data[_pos];
}
return m_data[pos + m_gapEnd-m_gapStart];
return m_data[_pos + m_gapEnd-m_gapStart];
}
/**
* @brief Get a current element in the vector
* @param[in] pos Desired position read
* @param[in] _pos Desired position read
* @return Reference on the Element
*/
int8_t& Get(int32_t pos) const
int8_t& Get(int32_t _pos) const
{
TK_ASSERT(0 <= _pos || _pos < Size(), "try to read an element non existing");
if (_pos < m_gapStart) {
return m_data[_pos];
}
return m_data[_pos + m_gapEnd-m_gapStart];
}
#if 0
/**
* @brief Get a current element in the vector
* @param[in] _pos Desired position read
* @return Reference on the Element
*/
esize_t Get(esize_t _pos, UniChar& _value, charset_te _charset) const
{
TK_ASSERT(0 <= pos || pos < Size(), "try to read an element non existing");
if (pos < m_gapStart) {
@ -190,168 +208,169 @@ namespace etk
}
return m_data[pos + m_gapEnd-m_gapStart];
}
#endif
/**
* @brief Get elements from a specific position.
* @param[in] pos Position of the first element.
* @param[in] nbElement Number of element needed.
* @param[in] _pos Position of the first element.
* @param[in] _nbElement Number of element needed.
* @return The data requested
*/
etk::Vector<int8_t> Get(int32_t pos, int32_t nbElement)
etk::Vector<int8_t> Get(int32_t _pos, int32_t _nbElement)
{
etk::Vector<int8_t> tmpBuffer;
tmpBuffer.Clear();
if (pos < m_gapStart) {
if (pos + nbElement < m_gapStart) {
tmpBuffer.PushBack(&m_data[pos], nbElement);
if (_pos < m_gapStart) {
if (pos + _nbElement < m_gapStart) {
tmpBuffer.PushBack(&m_data[_pos], _nbElement);
} else {
tmpBuffer.PushBack(&m_data[pos], m_gapStart - pos);
tmpBuffer.PushBack(&m_data[m_gapEnd], nbElement - (m_gapStart - pos) );
tmpBuffer.PushBack(&m_data[_pos], m_gapStart - _pos);
tmpBuffer.PushBack(&m_data[m_gapEnd], _nbElement - (m_gapStart - _pos) );
}
} else {
tmpBuffer.PushBack(&m_data[pos+(m_gapEnd-m_gapStart)], nbElement);
tmpBuffer.PushBack(&m_data[_pos+(m_gapEnd-m_gapStart)], _nbElement);
}
return tmpBuffer;
}
/**
* @brief Add at the Last position of the Vector
* @param[in] item Element to add at the end of vector
* @param[in] _item Element to add at the end of vector
*/
void PushBack(const int8_t& item)
void PushBack(const int8_t& _item)
{
Insert( Size(), item);
Insert(Size(), _item);
}
/**
* @brief Insert One item at the specify position.
* @param[in] pos Position where data might be inserted
* @param[in] items Data that might be inserted.
* @param[in] _pos Position where data might be inserted
* @param[in] _items Data that might be inserted.
*/
void Insert(int32_t pos, const int8_t& item)
void Insert(int32_t _pos, const int8_t& _item)
{
if( pos > Size()
|| pos < 0 ) {
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
return;
}
if( 0 == GapSize() ) {
if (false == GapResize(pos, GAP_SIZE_MIN + 1) ) {
if (false == GapResize(_pos, GAP_SIZE_MIN + 1) ) {
return;
}
} else if( pos == m_gapStart
&& pos == m_gapEnd-1 )
} else if( _pos == m_gapStart
&& _pos == m_gapEnd-1 )
{
// mothing to do ...
} else {
if (false == GapMove(pos)) {
if (GapMove(_pos) == false) {
return;
}
}
if(pos == m_gapStart) {
m_data[m_gapStart] = item;
if(_pos == m_gapStart) {
m_data[m_gapStart] = _item;
m_gapStart++;
} else {
m_data[m_gapEnd-1] = item;
m_data[m_gapEnd-1] = _item;
m_gapEnd--;
}
}
/**
* @brief Insert data in the buffer
* @param[in] pos Position where data might be inserted
* @param[in] items Data that might be inserted.
* @param[in] _pos Position where data might be inserted
* @param[in] _items Data that might be inserted.
*/
void Insert(int32_t pos, etk::Vector<int8_t>& items)
void Insert(int32_t _pos, etk::Vector<int8_t>& _items)
{
if( pos > Size()
|| pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos=" << _pos << " bufferSize="<<Size());
return;
}
if( items.Size() > GapSize() ) {
if (false == GapResize(pos, GAP_SIZE_MIN + items.Size()) ) {
if( _items.Size() > GapSize() ) {
if (false == GapResize(_pos, GAP_SIZE_MIN + _items.Size()) ) {
return;
}
} else {
if (false == GapMove(pos) ) {
if (false == GapMove(_pos) ) {
return;
}
}
int32_t i;
for(i=0; i<items.Size(); i++) {
m_data[m_gapStart+i] = items[i];
for(esize_t iii=0; iii<_items.Size(); iii++) {
m_data[m_gapStart+iii] = _items[iii];
}
m_gapStart += items.Size();
m_gapStart += _items.Size();
}
/**
* @brief Replace one element in the buffer
* @param[in] pos The first element to remove.
* @param[in] items Data that might be inserted.
* @param[in] _pos The first element to remove.
* @param[in] _items Data that might be inserted.
*/
void Replace(int32_t pos, const int8_t& item)
void Replace(int32_t _pos, const int8_t& _item)
{
if( pos > Size()
|| pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos=" << _pos << " bufferSize="<<Size());
return;
}
// just replace the element, not update Gap position
if (pos < m_gapStart) {
m_data[pos] = item;
if (_pos < m_gapStart) {
m_data[_pos] = _item;
} else {
m_data[pos+GapSize()] = item;
m_data[_pos+GapSize()] = _item;
}
}
/**
* @brief Replace specified data.
* @param[in] pos The first element to remove.
* @param[in] nbRemoveElement number of element to remove.
* @param[in] items Data that might be inserted.
* @param[in] _pos The first element to remove.
* @param[in] _nbRemoveElement number of element to remove.
* @param[in] _items Data that might be inserted.
*/
void Replace(int32_t pos, int32_t nbRemoveElement, etk::Vector<int8_t>& items)
void Replace(int32_t _pos, int32_t _nbRemoveElement, etk::Vector<int8_t>& _items)
{
if( pos > Size()
|| pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos=" << _pos << " bufferSize="<<Size());
return;
}
if( pos+nbRemoveElement > Size() ) {
TK_ERROR("Request remove more element than expected in the buffer pos+nbRemoveElement="<<pos+nbRemoveElement<< " bufferSize="<<Size());
if( _pos+_nbRemoveElement > Size() ) {
TK_ERROR("Request remove more element than expected in the buffer pos+nbRemoveElement="
<< _pos+_nbRemoveElement << " bufferSize=" << Size());
return;
}
if (false == GapMove(pos)) {
if (false == GapMove(_pos)) {
return;
}
// Remove elements :
m_gapEnd += nbRemoveElement;
m_gapEnd += _nbRemoveElement;
//Display();
// insert elements
Insert(pos, items);
Insert(_pos, _items);
// Resize buffer if needed...
GapCheckMaxSize();
}
/**
* @brief Remove specific data in the buffer.
* @param[in] pos The first element to remove
* @param[in] nbRemoveElement number of element to remove
* @param[in] _pos The first element to remove
* @param[in] _nbRemoveElement number of element to remove
*/
void Remove(int32_t pos, int32_t nbRemoveElement = 1)
void Remove(int32_t _pos, int32_t _nbRemoveElement = 1)
{
if( pos > Size()
|| pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos=" << _pos << " bufferSize="<<Size());
return;
}
if( pos+nbRemoveElement > Size() ) {
TK_ERROR("Request remove more element than expected in the buffer pos+nbRemoveElement="<<pos+nbRemoveElement<< " bufferSize="<<Size());
if( _pos+_nbRemoveElement > Size() ) {
TK_ERROR("Request remove more element than expected in the buffer pos+nbRemoveElement="
<< _pos+_nbRemoveElement << " bufferSize=" << Size());
return;
}
if (false == GapMove(pos) ) {
if (GapMove(_pos) == false) {
return;
}
// Remove elements :
if (m_allocated==m_gapEnd) {
m_gapStart -= nbRemoveElement;
if (m_allocated == m_gapEnd) {
m_gapStart -= _nbRemoveElement;
} else {
m_gapEnd += nbRemoveElement;
m_gapEnd += _nbRemoveElement;
}
// Resize buffer if needed...
GapCheckMaxSize();
@ -374,12 +393,12 @@ namespace etk
}
/**
* @brief Get a current element in the vector (iterator system)
* @param[in] RealElementPosition Real position in the buffer (only use in the ITERATOR)
* @param[in] _realElementPosition Real position in the buffer (only use in the ITERATOR)
* @return Reference on the Element
*/
int8_t& GetDirect(int32_t RealElementPosition)
int8_t& GetDirect(int32_t _realElementPosition)
{
return m_data[RealElementPosition];
return m_data[_realElementPosition];
};
/**
* @brief Get the number of element in the vector
@ -392,104 +411,104 @@ namespace etk
private:
/**
* @brief Change the current allocation to the corect one (depend on the current size)
* @param[in] newSize Minimum number of element needed
* @param[in] _newSize Minimum number of element needed
*/
void ChangeAllocation(int32_t newSize)
void ChangeAllocation(int32_t _newSize)
{
// set the minimal size to 1
if(newSize <= 0) {
newSize = 1;
if(_newSize <= 0) {
_newSize = 1;
}
// set the size with the corect chose type :
if (newSize == m_allocated) {
if (_newSize == m_allocated) {
return;
}
TK_DEBUG("Change Allocation : " << m_allocated << " ==> " << newSize);
TK_DEBUG("Change Allocation : " << m_allocated << " ==> " << _newSize);
// check if something is allocated :
if (NULL == m_data) {
if (m_data == NULL) {
// no data allocated ==> request an allocation (might be the first)
m_data = (int8_t *)malloc( newSize * sizeof(int8_t) );
m_data = (int8_t *)malloc( _newSize * sizeof(int8_t) );
} else {
// move datas
m_data = (int8_t *)realloc( m_data, newSize* sizeof(int8_t) );
m_data = (int8_t *)realloc( m_data, _newSize* sizeof(int8_t) );
}
// Check result with assert :
TK_ASSERT(NULL!=m_data, "Error in data allocation");
TK_ASSERT(m_data != NULL, "Error in data allocation");
// set the new allocation size
m_allocated = newSize;
m_allocated = _newSize;
}
/**
* @brief Move the current gap at an other position
* @param[in] pos Position of the new Gap.
* @param[in] _pos Position of the new Gap.
* @return false The operation can not be proccesed.
* @return true The operation done correctly.
*/
bool GapMove(int32_t pos)
bool GapMove(int32_t _pos)
{
if( pos > Size()
|| pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos=" << _pos << " bufferSize=" << Size());
return false;
}
int32_t gapLen = m_gapEnd - m_gapStart;
if (pos > m_gapStart) {
memmove(&m_data[m_gapStart], &m_data[m_gapEnd], pos - m_gapStart);
if (_pos > m_gapStart) {
memmove(&m_data[m_gapStart], &m_data[m_gapEnd], _pos - m_gapStart);
} else {
memmove(&m_data[pos + gapLen], &m_data[pos], m_gapStart - pos);
memmove(&m_data[_pos + gapLen], &m_data[_pos], m_gapStart - _pos);
}
m_gapEnd += pos - m_gapStart;
m_gapStart += pos - m_gapStart;
m_gapEnd += _pos - m_gapStart;
m_gapStart += _pos - m_gapStart;
return true;
}
/**
* @brief Change The gap position and size
* @param[in] pos Position of the new Gap.
* @param[in] newGapLen Size of the new gap (can be bigger than GAP_SIZE_MAX).
* @param[in] _pos Position of the new Gap.
* @param[in] _newGapLen Size of the new gap (can be bigger than GAP_SIZE_MAX).
* @return false The operation can not be proccesed.
* @return true The operation done correctly.
*/
bool GapResize(int32_t pos, int32_t newGapLen)
bool GapResize(int32_t _pos, int32_t _newGapLen)
{
if( pos > Size()
|| pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos="<<pos<< " bufferSize="<<Size());
if( _pos > Size()
|| _pos < 0 ) {
TK_ERROR("Request higher than buffer size : pos=" << _pos << " bufferSize="<<Size());
return false;
}
int32_t previousSize = Size();
if (newGapLen == GapSize() ) {
if (_newGapLen == GapSize() ) {
// nothing to do ...
return true;
} else {
if (newGapLen > GapSize() ) {
if (_newGapLen > GapSize() ) {
// reallocation
ChangeAllocation( previousSize + newGapLen);
ChangeAllocation( previousSize + _newGapLen);
}
// move Data
if (pos <= m_gapStart) {
if (_pos <= m_gapStart) {
// just move the end of the gap
memmove(&m_data[m_gapStart + newGapLen], &m_data[m_gapEnd], previousSize - m_gapStart);
memmove(&m_data[m_gapStart + _newGapLen], &m_data[m_gapEnd], previousSize - m_gapStart);
// update gap end position
m_gapEnd = m_gapStart + newGapLen;
m_gapEnd = m_gapStart + _newGapLen;
if (pos < m_gapStart) {
if (false == GapMove(pos)) {
if (false == GapMove(_pos)) {
return false;
}
}
// no else
} else {
if (false == GapMove(pos) ) {
if (false == GapMove(_pos) ) {
return false;
}
memmove(&m_data[m_gapStart + newGapLen], &m_data[m_gapEnd], previousSize - m_gapStart);
memmove(&m_data[m_gapStart + _newGapLen], &m_data[m_gapEnd], previousSize - m_gapStart);
}
if (newGapLen < GapSize() ) {
if (_newGapLen < GapSize() ) {
// rellocation
ChangeAllocation(previousSize + newGapLen);
ChangeAllocation(previousSize + _newGapLen);
}
}
// update gap position
m_gapStart = pos;
m_gapEnd = pos + newGapLen;
m_gapStart = _pos;
m_gapEnd = _pos + _newGapLen;
return true;
}
/**

View File

@ -15,6 +15,14 @@
#include <etk/Vector.h>
#include <etk/Char.h>
const etk::UniChar etk::UniChar::Return('\n');
const etk::UniChar etk::UniChar::CarrierReturn('\r');
const etk::UniChar etk::UniChar::Tabulation('\t');
const etk::UniChar etk::UniChar::Backspace((const char)8);
const etk::UniChar etk::UniChar::Delete((const char)127);
const etk::UniChar etk::UniChar::Space(' ');
const etk::UniChar etk::UniChar::Escape((const char)27);
void etk::UniChar::Lower(void)
{
if( m_value>=(uint32_t)'A'

View File

@ -43,6 +43,14 @@ namespace etk
class UniChar
{
public: // classic unicar code :
static const UniChar Return; //!< '\n'
static const UniChar CarrierReturn; //!< '\r' CR
static const UniChar Tabulation; //!< '\t' TAB
static const UniChar Backspace; //!< BS (SUPPRESS)
static const UniChar Delete; //!< DEL
static const UniChar Space; //!< ' ' SPACE
static const UniChar Escape; //!< ESC Escape
private:
uint32_t m_value;
public: