zipheadio.h

Go to the documentation of this file.
00001 #ifndef ZIPHEADIO_H
00002 #define ZIPHEADIO_H
00003 
00004 #include "zipios-config.h"
00005 
00006 #include "meta-iostreams.h"
00007 #include <string>
00008 #include <vector>
00009 
00010 #include "ziphead.h"
00011 #include "zipios_defs.h"
00012 
00013 namespace zipios {
00014 
00015 // byte order conversion functions. 
00016 // ztohs (zip-to-host-short)
00017 #ifdef MY_BIG_ENDIAN
00018 
00019 inline uint16 ztohs ( unsigned char *buf ) {
00020   uint16 out ;
00021 //    *( reinterpret_cast<unsigned char *>( &out )     ) = *( buf  + 1 );
00022 //    *( reinterpret_cast<unsigned char *>( &out ) + 1 ) = *( buf      );
00023   out = ( static_cast< uint16 >( buf[ 0 ] ) << 8  ) + 
00024         ( static_cast< uint16 >( buf[ 1 ] )       )  ; 
00025 
00026   return out;
00027 }
00028 
00029 // ztohl (zip-to-host-long)
00030 inline uint32 ztohl ( unsigned char *buf ) {
00031   uint32 out;
00032   out = ( static_cast< uint32 >( buf[ 0 ] ) << 24 ) +  
00033         ( static_cast< uint32 >( buf[ 1 ] ) << 16 ) + 
00034         ( static_cast< uint32 >( buf[ 2 ] ) << 8  ) + 
00035         ( static_cast< uint32 >( buf[ 3 ] )       )  ; 
00036   
00037   return out;
00038 }
00039 
00040 #else
00041 
00042 inline uint16 ztohs ( unsigned char *buf ) {
00043   uint16 out ;
00044   out = ( static_cast< uint16 >( buf[ 1 ] ) << 8  ) + 
00045         ( static_cast< uint16 >( buf[ 0 ] )       )  ; 
00046   return out;
00047 }
00048 
00049 // ztohl (zip-to-host-long)
00050 inline uint32 ztohl ( unsigned char *buf ) {
00051   uint32 out;
00052   out = ( static_cast< uint32 >( buf[ 3 ] ) << 24 ) +  
00053         ( static_cast< uint32 >( buf[ 2 ] ) << 16 ) + 
00054         ( static_cast< uint32 >( buf[ 1 ] ) << 8  ) + 
00055         ( static_cast< uint32 >( buf[ 0 ] )       )  ; 
00056 //    cerr << "buf : " << static_cast< int >( buf[ 0 ] ) ;
00057 //    cerr << " "      << static_cast< int >( buf[ 1 ] ) ;
00058 //    cerr << " "      << static_cast< int >( buf[ 2 ] ) ;
00059 //    cerr << " "      << static_cast< int >( buf[ 3 ] ) << endl ;
00060 //    cerr << "uint32 " << out << endl ;
00061   return out;
00062 }
00063 
00064 
00065 #endif
00066 
00067 // htozl (host-to-zip-long)
00068 inline uint32 htozl ( unsigned char *buf ) {
00069   return ztohl( buf ) ;
00070 }
00071 
00072 // htozs (host-to-zip-short)
00073 inline uint16 htozs ( unsigned char *buf ) {
00074   return ztohs( buf ) ;
00075 }
00076 
00077 
00078 inline uint32 readUint32 ( istream &is ) {
00079   static const int buf_len = sizeof ( uint32 ) ;
00080   unsigned char buf [ buf_len ] ;
00081   int rsf = 0 ;
00082   // fix endless loop on (almost) empty streams, 20080509 wmayer
00083   int cnt = 0;
00084   while ( rsf < buf_len && (cnt++ < buf_len) ) {
00085     is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
00086     rsf += is.gcount () ;
00087   }
00088   return  ztohl ( buf ) ;
00089 }
00090 
00091 inline void writeUint32 ( uint32 host_val, ostream &os ) {
00092   uint32 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
00093   os.write( reinterpret_cast< char * >( &val ), sizeof( uint32 ) ) ;
00094 }
00095 
00096 inline uint16 readUint16 ( istream &is ) {
00097   static const int buf_len = sizeof ( uint16 ) ;
00098   unsigned char buf [ buf_len ] ;
00099   int rsf = 0 ;
00100   while ( rsf < buf_len ) {
00101     is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
00102     rsf += is.gcount () ;
00103   }
00104   return  ztohs ( buf ) ;
00105 }
00106 
00107 inline void writeUint16 ( uint16 host_val, ostream &os ) {
00108   uint16 val = (uint16)htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
00109   os.write( reinterpret_cast< char * >( &val ), sizeof( uint16 ) ) ;
00110 }
00111 
00112 inline void readByteSeq ( istream &is, string &con, int count ) {
00113   char *buf = new char [ count + 1 ] ;
00114   int rsf = 0 ;
00115   while ( rsf < count && is ) {
00116     is.read ( buf + rsf, count - rsf ) ;
00117     rsf += is.gcount() ;
00118   }
00119   buf [ count ] = '\0' ;
00120 
00121   con = buf ;
00122   delete [] buf ;
00123 }
00124 
00125 inline void writeByteSeq( ostream &os, const string &con ) {
00126   os << con ;
00127 }
00128 
00129 inline void readByteSeq ( istream &is, unsigned char *buf, int count ) {
00130   int rsf = 0 ;
00131 
00132   while ( rsf < count && is ) {
00133     is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
00134     rsf += is.gcount() ;
00135   }
00136 }
00137 
00138 inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) {
00139   os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ;
00140 }
00141 
00142 inline void readByteSeq ( istream &is, vector < unsigned char > &vec, int count ) {
00143   unsigned char *buf = new unsigned char [ count ] ;
00144   int rsf = 0 ;
00145   while ( rsf < count && is ) {
00146     is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
00147     rsf += is.gcount() ;
00148   }
00149   
00150   vec.insert ( vec.end (), buf, buf + count ) ;
00151   delete [] buf ;
00152 }
00153 
00154 inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) {
00155   if(!vec.empty())
00156     os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ;
00157 }
00158 
00159 istream& operator>> ( istream &is, ZipLocalEntry &zlh         ) ;
00160 istream& operator>> ( istream &is, DataDescriptor &dd          ) ;
00161 istream& operator>> ( istream &is, ZipCDirEntry &zcdh           ) ;
00162 //  istream& operator>> ( istream &is, EndOfCentralDirectory &eocd ) ;
00163 
00164 ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
00165 ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
00166 ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
00167 
00168 
00169 } // namespace
00170 
00171 #endif
00172 
00178 /*
00179   Zipios++ - a small C++ library that provides easy access to .zip files.
00180   Copyright (C) 2000  Thomas Søndergaard
00181   
00182   This library is free software; you can redistribute it and/or
00183   modify it under the terms of the GNU Lesser General Public
00184   License as published by the Free Software Foundation; either
00185   version 2 of the License, or (at your option) any later version.
00186   
00187   This library is distributed in the hope that it will be useful,
00188   but WITHOUT ANY WARRANTY; without even the implied warranty of
00189   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00190   Lesser General Public License for more details.
00191   
00192   You should have received a copy of the GNU Lesser General Public
00193   License along with this library; if not, write to the Free Software
00194   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00195 */

Generated on Wed Nov 23 19:01:12 2011 for FreeCAD by  doxygen 1.6.1