00001 /* 00002 * IOBuffer.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Buffer for I/O accessors. 00006 * 00007 * Copyright (c) 2003 Milan Cermak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #include <stlib/IOBuffer.h> 00025 00026 #include <stlib/IOAccessor.h> 00027 #include <stlib/SequenceableCollection.h> 00028 #include <stlib/ByteArray.h> 00029 #include <stlib/String.h> 00030 00031 IOBuffer::IOBuffer(IOAccessor *accessor) 00032 { 00033 io_accessor = accessor; 00034 _buffer = new ByteArray(io_accessor->bufferSize()); 00035 buffer_amount = 0; 00036 } 00037 00038 /* Class-accessing protocol */ 00039 String *IOBuffer::className(void) const 00040 { 00041 return new String("IOBuffer"); 00042 } 00043 00044 /* Buffer accessing protocol */ 00045 ByteArray *IOBuffer::buffer(void) 00046 { 00047 return _buffer; 00048 } 00049 00050 /* Buffer state protocol */ 00051 void IOBuffer::bufferChanged(void) 00052 { 00053 /* Does nothing */ 00054 } 00055 00056 /* Position accessing protocol */ 00057 long IOBuffer::currentBufferPosition(void) 00058 { 00059 return 0; 00060 } 00061 00062 long IOBuffer::firstDataPosition(void) 00063 { 00064 return 0; 00065 } 00066 00067 long IOBuffer::lastDataPosition(void) 00068 { 00069 return buffer_amount; 00070 } 00071 00072 long IOBuffer::lastWriteablePosition(void) 00073 { 00074 return _buffer->size() - 1; 00075 } 00076 00077 /* Positioning protocol */ 00078 bool IOBuffer::isSeekable(void) 00079 { 00080 return io_accessor->isSeekable(); 00081 } 00082 00083 ByteArray *IOBuffer::readPositionAndSetOffset(long position, long &offset) 00084 { 00085 io_accessor->seekTo(position); 00086 buffer_amount = readBufferStartingAt(1); 00087 offset = 1; 00088 return _buffer; 00089 } 00090 00091 /* Reading protocol */ 00092 ByteArray *IOBuffer::nextAndSetOffset(long &offset) 00093 { 00094 long amt, start; 00095 00096 stepToNextBuffer(); 00097 start = buffer_amount % _buffer->size(); 00098 amt = readBufferStartingAt(start); 00099 offset = start; 00100 buffer_amount = start + amt; 00101 return _buffer; 00102 } 00103 00104 /* Status protocol */ 00105 void IOBuffer::commit(void) 00106 { 00107 io_accessor->commit(); 00108 } 00109 00110 /* Writing protocol */ 00111 void IOBuffer::flushBufferUpTo(long position) 00112 { 00113 if (position > 0) 00114 io_accessor->writeForSureFrom(_buffer, 0, position); 00115 } 00116 00117 /* Private protocol */ 00118 long IOBuffer::readBufferStartingAt(long position) 00119 { 00120 long amt; 00121 amt = _buffer->size() - position; 00122 amt = io_accessor->readInto(_buffer, position, amt); 00123 /* Tady by se mely osetrit chyby cteni. */ 00124 return amt; 00125 } 00126 00127 void IOBuffer::stepToNextBuffer(void) 00128 { 00129 /* Does nothing */ 00130 }