From: Mikko L. <laz...@us...> - 2004-06-12 07:33:02
|
Update of /cvsroot/rtk/rtk/src/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2008 Modified Files: Buffer.cpp Log Message: Fixed Buffer in several cases. Index: Buffer.cpp =================================================================== RCS file: /cvsroot/rtk/rtk/src/core/Buffer.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Buffer.cpp 11 Jun 2004 23:13:17 -0000 1.6 --- Buffer.cpp 12 Jun 2004 07:32:53 -0000 1.7 *************** *** 40,108 **** ***************************************************************************/ #include <rtk/Buffer.h> ! #if _DEBUG #include <rtk/debug.h> #endif namespace Rtk { ! uchar* Buffer::Allocate(uint arg_num) { ! uint newsize = _size + arg_num; if (newsize > _allocated) { ! _allocated *= 2; if (_allocated < newsize) _allocated = newsize; uchar* newdata = (uchar*) malloc(_allocated); memcpy(newdata, _data, _size); free(_data); _data = newdata; } // if ! return _data + _size; } // Allocate() ! void Buffer::Write(const uchar* arg_byte_array, uint arg_num) { memcpy(Allocate(arg_num), arg_byte_array, arg_num); _size += arg_num; } // Write() ! int Buffer::SetSize(uchar arg_size) { ! if (arg_size) { ! uchar* newdata = (uchar*) realloc(_data, arg_size + 1); ! if (!newdata) ! return -1; ! _data = newdata; ! _allocated = arg_size; ! } // if ! _size = 0; } void Buffer::Fill(uchar arg_char) { ! memset(_data, arg_char, _allocated); ! _size = _allocated; } // Fill() ! #if _DEBUG // Code taken from eFLTK ... void Buffer::FromFile(const String& arg_fname) { ! FILE *f = fopen(fileName,"rb"); ! if (!f) return; // Some error checks should be here... ! fseek(f,0,SEEK_END); ! int size = ftell(f); ! fseek(f,0,SEEK_SET); Allocate(size+1); _data[size] = '\0'; ! _size = fread(_data, 1 , size, f); ! fclose(f); } #endif --- 40,142 ---- ***************************************************************************/ + #include <string.h> /* memcpy() */ + #include <stdlib.h> /* malloc() */ + #include <rtk/Buffer.h> ! #ifdef _DEBUG #include <rtk/debug.h> + #include <rtk/File.h> #endif namespace Rtk { ! Buffer::~Buffer() { ! Clear(); ! } ! ! uchar* Buffer::Allocate(ulong arg_num) { ! ulong newsize = _size + arg_num; if (newsize > _allocated) { ! if(_allocated) _allocated *= 2; ! else _allocated = RTK_BUFFER_SIZE; ! if (_allocated < newsize) _allocated = newsize; + + if(!_data) _data = (uchar*)malloc(_allocated); + else _data = (uchar*)realloc(_data, _allocated); + + /* uchar* newdata = (uchar*) malloc(_allocated); memcpy(newdata, _data, _size); free(_data); _data = newdata; + */ } // if ! return (_data + _size); } // Allocate() ! void Buffer::Write(const uchar* arg_byte_array, ulong arg_num) { memcpy(Allocate(arg_num), arg_byte_array, arg_num); _size += arg_num; } // Write() + + void Buffer::Clear() + { + if(_data) free(_data); + _size = 0; + _allocated = 0; + _data = 0; + } ! int Buffer::SetSize(ulong arg_size) { ! if (arg_size == 0) { ! Clear(); ! } else { ! Allocate(arg_size); ! _size = arg_size; ! } ! return 0; } void Buffer::Fill(uchar arg_char) { ! if(_data) { ! memset(_data, arg_char, _size); ! } } // Fill() ! #ifdef _DEBUG // Code taken from eFLTK ... void Buffer::FromFile(const String& arg_fname) { ! File f(arg_fname, File::READ|File::BINARY); ! if(!f.IsOpen()) return; // Some error checks should be here... ! f.Seek(0, IO::END); ! long size = f.Tell(); ! f.Seek(0, IO::BEGIN); Allocate(size+1); _data[size] = '\0'; ! _size = f.Read(_data, size); ! f.Close(); } + + void Buffer::ToFile(const String& arg_fname) const + { + if(!_size) return; + File f(arg_fname, File::WRITE|File::CREATE|File::BINARY); + if(f.IsOpen()) { + f.Write(_data, _size); + f.Close(); + } + } #endif |