From: Mikko L. <laz...@us...> - 2004-06-13 10:28:33
|
Update of /cvsroot/rtk/rtk/src/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14291 Modified Files: Buffer.cpp error.cpp Log Message: - Some IO changes - Use ERROR things in File - Buffer is now IO Index: error.cpp =================================================================== RCS file: /cvsroot/rtk/rtk/src/core/error.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** error.cpp 28 Feb 2004 07:39:44 -0000 1.3 --- error.cpp 13 Jun 2004 10:27:58 -0000 1.4 *************** *** 4,25 **** static const RCHAR * error_names[] = { ! _R("No Error"), ! #if 1 ! ! # define INC_ERROR_CODES 1 ! ! # define ERROR_CODE0(name) _R(#name), ! # define ERROR_CODE1(name,type) _R(#type) _R(":") _R(#name), ! # include <rtk/error_codes.h> ! # undef ERROR_CODE0 ! # undef ERROR_CODE1 ! # undef INC_ERROR_CODES ! #else ! _R("Out of memory"), ! _R("File not found"), ! #endif }; --- 4,16 ---- static const RCHAR * error_names[] = { ! _R("Operation Success"), ! #define INC_ERROR_CODES 1 ! #define ERROR_CODE(name,type) _R(#type) _R(":") _R(#name), ! #include <rtk/error_codes.h> ! #undef ERROR_CODE ! #undef INC_ERROR_CODES }; Index: Buffer.cpp =================================================================== RCS file: /cvsroot/rtk/rtk/src/core/Buffer.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Buffer.cpp 13 Jun 2004 07:51:56 -0000 1.9 --- Buffer.cpp 13 Jun 2004 10:27:58 -0000 1.10 *************** *** 50,127 **** 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); ! } // 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() ! void Buffer::From(IO* arg_io) { ! if(!arg_io->CanRead() && arg_io->IsOpen()) ! return; // Some error checks should be here... ! while(!arg_io->IsEos()) ! { ! uchar buf[1024]; ! int bytes = arg_io->Read(buf, sizeof(buf)); this->Write(buf, bytes); ! } ! } // From() ! void Buffer::To(IO* arg_io) ! { ! if (!_size) return; ! if (arg_io->CanWrite() && arg_io->IsOpen()) ! { ! arg_io->Write(_data, _size); ! } ! } // To() }; // Rtk --- 50,184 ---- namespace Rtk { ! Buffer::~Buffer() { ! Clear(); ! } ! uchar* Buffer::Allocate(ulong arg_num) ! { ! ulong newsize = _pos + 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); ! } // if ! return (_data + _pos); ! } // Allocate() ! long Buffer::Read(void *buffer, long buffer_len) ! { ! if(IsEos()) return 0; ! ! ulong end = _pos+buffer_len; ! if(end > _size) ! end = _size; ! ulong len = end-_pos; ! memcpy(buffer, _data+_pos, len); ! _pos += len; ! return len; ! } ! ! long Buffer::Write(void *buffer, long buffer_len) ! { ! if(buffer_len <= 0) return buffer_len; ! memcpy(Allocate(buffer_len), buffer, buffer_len); ! _pos += buffer_len; ! if(_pos > _size) { ! _size = _pos; } + return buffer_len; + } + + long Buffer::Seek(long pos, int method) + { + long offset = 0; + switch(method) { + case IO::END: offset = _size; break; + case IO::CURRENT: offset = _pos; break; + default: break; + }; + + long after = offset + pos; + + if(after > (long)_size || after < 0) { + ERROR_RETURN(OPERATION_FAILED); + } + + return (_pos = after); + } + + long Buffer::Tell() + { + return _pos; + } + + + void Buffer::Clear() + { + if(_data) free(_data); + _size = 0; + _allocated = 0; + _data = 0; + _pos = 0; + } ! int Buffer::SetSize(ulong arg_size) ! { ! if (arg_size == 0) { ! Clear(); ! } else { ! _allocated = _size = arg_size; ! ! if(!_data) _data = (uchar*)malloc(_allocated); ! else _data = (uchar*)realloc(_data, _allocated); ! if(_pos > _size) ! _pos = _size; ! } ! RETURN_SUCCESS; ! } ! ! void Buffer::Fill(uchar arg_char) ! { ! if(_data) { ! memset(_data, arg_char, _size); ! } ! } // Fill() ! void Buffer::From(IO* arg_io) ! { ! if(!arg_io->CanRead() && arg_io->IsOpen()) ! return; // Some error checks should be here... ! ! Clear(); ! ! while(!arg_io->IsEos()) { ! uchar buf[1024]; ! int bytes = arg_io->Read(buf, sizeof(buf)); ! if(bytes < 0) { ! DBG(1, D_IO, _R("Buffer::From() IO::Read failed: %s [%d]\n"), error2str(bytes), bytes); ! break; ! } else if(bytes>0) { this->Write(buf, bytes); ! } ! } ! this->Seek(0); ! } // From() ! void Buffer::To(IO* arg_io) ! { ! if (!_size) return; ! if (arg_io->CanWrite() && arg_io->IsOpen()) ! { ! arg_io->Write(_data, _size); ! } ! } // To() }; // Rtk |