00001 /* 00002 * IOAccessor.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Abstract accessor to I/O resource. 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/IOAccessor.h> 00025 00026 #include <stlib/ByteArray.h> 00027 #include <stlib/ExternalEncodedStreamFactory.h> 00028 #include <stlib/ExternalReadStream.h> 00029 #include <stlib/ExternalReadAppendStream.h> 00030 #include <stlib/ExternalWriteStream.h> 00031 #include <stlib/SequenceableCollection.h> 00032 #include <stlib/String.h> 00033 00034 /* Class-accessing protocol */ 00035 String *IOAccessor::className(void) const 00036 { 00037 return new String("IOAccessor"); 00038 } 00039 00040 /* Accessing protocol */ 00041 long IOAccessor::bufferSize(void) 00042 { 00043 return 4096; 00044 } 00045 00046 void IOAccessor::commit(void) 00047 { 00048 /* Does nothing */ 00049 } 00050 00051 long IOAccessor::dataSize(void) 00052 { 00053 return 0; 00054 } 00055 00056 LineEndConvention IOAccessor::lineEndConvention(void) 00057 { 00058 return LineEndTransparent; 00059 } 00060 00061 /* Data transfer protocol */ 00062 int IOAccessor::readInto(ByteArray *buffer) 00063 { 00064 return readInto(buffer, 0, buffer->size()); 00065 } 00066 00067 int IOAccessor::writeFrom(ByteArray *buffer) 00068 { 00069 return writeFrom(buffer, 0, buffer->size()); 00070 } 00071 00072 int IOAccessor::writeForSureFrom(ByteArray *buffer, long startIndex, long count) 00073 { 00074 long start = startIndex; 00075 long total = 0; 00076 long result; 00077 00078 while (total < count) { 00079 result = writeFrom(buffer, start, count - total); 00080 start += result; 00081 total += result; 00082 } 00083 return total; 00084 } 00085 00086 /* Synchronization protocol */ 00087 void IOAccessor::readWait(void) 00088 { 00089 /* Does nothing */ 00090 } 00091 00092 void IOAccessor::writeWait(void) 00093 { 00094 /* Does nothing */ 00095 } 00096 00097 /* Stream creation protocol */ 00098 ExternalReadStream *IOAccessor::readStream(void) 00099 { 00100 return new ExternalReadStream(this); 00101 } 00102 00103 ExternalReadAppendStream *IOAccessor::readAppendStream(void) 00104 { 00105 return new ExternalReadAppendStream(this); 00106 } 00107 00108 ExternalWriteStream *IOAccessor::writeStream(void) 00109 { 00110 return new ExternalWriteStream(this); 00111 } 00112 00113 ExternalEncodedStreamFactory *IOAccessor::withEncoding(String *encoding) 00114 { 00115 return new ExternalEncodedStreamFactory(this, encoding); 00116 } 00117 00118 ExternalEncodedStreamFactory *IOAccessor::withEncoding(const char *encoding) 00119 { 00120 return withEncoding(new String(encoding)); 00121 } 00122 00123 /* Stream redirection protocol */ 00124 void IOAccessor::changeDescriptorTo(int fd) 00125 { 00126 shouldNotImplement(__PRETTY_FUNCTION__); 00127 } 00128 00129 /* Testing protocol */ 00130 bool IOAccessor::isSeekable(void) 00131 { 00132 return false; 00133 }