diff --git a/etk/Buffer.h b/etk/Buffer.h index 1e6c20f..d35c799 100644 --- a/etk/Buffer.h +++ b/etk/Buffer.h @@ -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 Get(int32_t pos, int32_t nbElement) + etk::Vector Get(int32_t _pos, int32_t _nbElement) { etk::Vector 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="< " << 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="<