library of assembled shared sources

http://lass.cocamware.com

binary_i_stream.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #include "io_common.h"
00044 #include "binary_i_stream.h"
00045 #include "../num/basic_types.h"
00046 
00047 // static_cast from TintPtr to pointer gives warning on MSVC, yet both are identical in size [Bramz]
00048 // 
00049 #if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
00050 #   pragma warning(push)
00051 #   pragma warning(disable: 4312) // 'reinterpret_cast' : conversion from 'lass::num::TintPtr' to 'void *' of greater size
00052 #endif
00053 
00054 namespace lass
00055 {
00056 
00057 namespace io
00058 {
00059 
00060 BinaryIStream::BinaryIStream():
00061     BinaryStreamBase()
00062 {
00063 }
00064 
00065 
00066 
00067 BinaryIStream::~BinaryIStream()
00068 {
00069 }
00070 
00071 
00072 
00073 long BinaryIStream::tellg() const
00074 {
00075     return doTellg();
00076 }
00077 
00078 
00079 
00080 BinaryIStream& BinaryIStream::seekg(long position)
00081 {
00082     doSeekg(position, std::ios_base::beg);
00083     return *this;
00084 }
00085 
00086 
00087 
00088 BinaryIStream& BinaryIStream::seekg(long offset, std::ios_base::seekdir directioin)
00089 {
00090     doSeekg(offset, directioin);
00091     return *this;
00092 }
00093 
00094 
00095 
00096 BinaryIStream& BinaryIStream::operator>>( char& x )
00097 {
00098     return readValue(x);
00099 }
00100 
00101 
00102 
00103 BinaryIStream& BinaryIStream::operator>>( num::Tint8& x )
00104 {
00105     return readValue(x);
00106 }
00107 
00108 
00109 
00110 BinaryIStream& BinaryIStream::operator>>( num::Tuint8& x )
00111 {
00112     return readValue(x);
00113 }
00114 
00115 
00116 
00117 BinaryIStream& BinaryIStream::operator>>( num::Tint16& x )
00118 {
00119     return readValue(x);
00120 }
00121 
00122 
00123 
00124 BinaryIStream& BinaryIStream::operator>>( num::Tuint16& x )
00125 {
00126     return readValue(x);
00127 }
00128 
00129 
00130 
00131 BinaryIStream& BinaryIStream::operator>>( num::Tint32& x )
00132 {
00133     return readValue(x);
00134 }
00135 
00136 
00137 
00138 BinaryIStream& BinaryIStream::operator>>( num::Tuint32& x )
00139 {
00140     return readValue(x);
00141 }
00142 
00143 
00144 
00145 BinaryIStream& BinaryIStream::operator>>( num::Tint64& x )
00146 {
00147     return readValue(x);
00148 }
00149 
00150 
00151 
00152 BinaryIStream& BinaryIStream::operator>>( num::Tuint64& x )
00153 {
00154     return readValue(x);
00155 }
00156 
00157 
00158 
00159 BinaryIStream& BinaryIStream::operator>>( num::Tfloat32& x )
00160 {
00161     return readValue(x);
00162 }
00163 
00164 
00165 
00166 BinaryIStream& BinaryIStream::operator>>( num::Tfloat64& x )
00167 {
00168     return readValue(x);
00169 }
00170 
00171 
00172 
00173 BinaryIStream& BinaryIStream::operator>>(bool& x)
00174 {
00175     num::Tuint8 temp;
00176     *this >> temp;
00177     if (good())
00178     {
00179         x = temp ? true : false;
00180     }
00181     return *this;
00182 }
00183 
00184 
00185 
00186 BinaryIStream& BinaryIStream::operator>>(void*& x)
00187 {
00188     LASS_META_ASSERT(sizeof(num::TintPtr) == sizeof(const void*), TintPtr_should_be_of_pointer_size);
00189     num::Tint64 temp;
00190     *this >> temp;
00191     if (good())
00192     {
00193 #pragma LASS_FIXME("do something special here if cast causes truncation [Bramz]")
00194         x = reinterpret_cast<void*>(static_cast<num::TintPtr>(temp));
00195     }
00196     return *this;
00197 }
00198 
00199 
00200 
00201 BinaryIStream& BinaryIStream::operator>>( std::string& x )
00202 {
00203     num::Tuint64 n;
00204     *this >> n;
00205     if (good())
00206     {
00207         size_t length = static_cast<size_t>(n);
00208 #pragma LASS_FIXME("do something special if there's an overflow [Bramz]")
00209         LASS_ASSERT(n == static_cast<num::Tuint64>(n));
00210         std::vector<char> buffer(length + 2, '\0'); // [TDM] null character storage + safety :o)
00211         doRead(&buffer[0], length);
00212         if (good())
00213         {
00214             x = std::string(&buffer[0]);
00215         }
00216     }
00217     return *this;
00218 }
00219 
00220 
00221 
00222 /** read a number of bytes from stream to buffer
00223  *
00224  *  @param iBytes 
00225  *      pointer to buffer.  Must be able to contain at least @a numBytes bytes.
00226  *  @param numBytes 
00227  *      number of bytes to be read
00228  */
00229 void BinaryIStream::read(void* output, size_t numBytes)
00230 {
00231     doRead(output, numBytes);
00232 }
00233 
00234 
00235 
00236 // --- private -------------------------------------------------------------------------------------
00237 
00238 template <typename T>
00239 BinaryIStream& BinaryIStream::readValue(T& x)
00240 {
00241     if (good())
00242     {
00243         T temp;
00244         doRead(&temp, sizeof(T));
00245         if (good())
00246         {
00247             x = num::fixEndianness(temp, endianness());
00248         }
00249     }
00250     return *this;
00251 }
00252 
00253 
00254 }
00255 
00256 }
00257 

Generated on Mon Nov 10 14:20:00 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo