IBSimu 1.0.4
file.hpp
Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2005-2010 Taneli Kalvas. All rights reserved.
00006  *
00007  * You can redistribute this software and/or modify it under the terms
00008  * of the GNU General Public License as published by the Free Software
00009  * Foundation; either version 2 of the License, or (at your option)
00010  * any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this library (file "COPYING" included in the package);
00019  * if not, write to the Free Software Foundation, Inc., 51 Franklin
00020  * Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  * 
00022  * If you have questions about your rights to use or distribute this
00023  * software, please contact Berkeley Lab's Technology Transfer
00024  * Department at TTD@lbl.gov. Other questions, comments and bug
00025  * reports should be sent directly to the author via email at
00026  * taneli.kalvas@jyu.fi.
00027  * 
00028  * NOTICE. This software was developed under partial funding from the
00029  * U.S.  Department of Energy.  As such, the U.S. Government has been
00030  * granted for itself and others acting on its behalf a paid-up,
00031  * nonexclusive, irrevocable, worldwide license in the Software to
00032  * reproduce, prepare derivative works, and perform publicly and
00033  * display publicly.  Beginning five (5) years after the date
00034  * permission to assert copyright is obtained from the U.S. Department
00035  * of Energy, and subject to any subsequent five (5) year renewals,
00036  * the U.S. Government is granted for itself and others acting on its
00037  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
00038  * the Software to reproduce, prepare derivative works, distribute
00039  * copies to the public, perform publicly and display publicly, and to
00040  * permit others to do so.
00041  */
00042 
00043 #ifndef FILE_HPP
00044 #define FILE_HPP 1
00045 
00046 
00047 #include <iostream>
00048 #include <zlib.h>
00049 #include "error.hpp"
00050 
00051 
00052 /* **************** *
00053  * Write            *
00054  * **************** */
00055 
00056 
00059 inline void write_int8( std::ostream &s, int8_t value )
00060 {
00061     s.write( (const char *)(&value), sizeof(int8_t) );
00062 }
00063 
00064 
00067 inline void write_int16( std::ostream &s, int16_t value )
00068 {
00069     s.write( (const char *)(&value), sizeof(int16_t) );
00070 }
00071 
00072 
00075 inline void write_int32( std::ostream &s, int32_t value )
00076 {
00077     s.write( (const char *)(&value), sizeof(int32_t) );
00078 }
00079 
00080 
00083 inline void write_uint32( std::ostream &s, uint32_t value )
00084 {
00085     s.write( (const char *)(&value), sizeof(uint32_t) );
00086 }
00087 
00088 
00091 inline void write_double( std::ostream &s, double value )
00092 {
00093     s.write( (const char *)(&value), sizeof(double) );
00094 }
00095 
00096 
00100 inline void write_compressed_block( std::ostream &s, uint32_t len, const int8_t *data )
00101 {
00102     uLongf  buflen = uLongf(len*1.1)+12 ;
00103     Bytef  *buf = new Bytef [buflen];
00104     compress( buf, &buflen, (Bytef *)data, (uLong)len );
00105     write_uint32( s, len );
00106     write_uint32( s, buflen );
00107     s.write( (const char *)buf, buflen );
00108     delete [] buf;
00109 }
00110 
00111 
00112 
00113 /* **************** *
00114  * Read             *
00115  * **************** */
00116 
00117 
00118 inline int8_t read_int8( std::istream &s )
00119 {
00120     int8_t value;
00121     s.read( (char *)(&value), sizeof(int8_t) );
00122     return( value );
00123 }
00124 
00125 
00126 inline int16_t read_int16( std::istream &s )
00127 {
00128     int16_t value;
00129     s.read( (char *)(&value), sizeof(int16_t) );
00130     return( value );
00131 }
00132 
00133 
00134 inline int32_t read_int32( std::istream &s )
00135 {
00136     int32_t value;
00137     s.read( (char *)(&value), sizeof(int32_t) );
00138     return( value );
00139 }
00140 
00141 
00142 inline uint32_t read_uint32( std::istream &s )
00143 {
00144     uint32_t value;
00145     s.read( (char *)(&value), sizeof(uint32_t) );
00146     return( value );
00147 }
00148 
00149 
00150 inline double read_double( std::istream &s )
00151 {
00152     double value;
00153     s.read( (char *)(&value), sizeof(double) );
00154     return( value );
00155 }
00156 
00157 
00158 inline uint32_t read_compressed_block( std::istream &s, uint32_t len, int8_t *dest )
00159 {
00160     uint32_t datalen;
00161     uint32_t compressedlen;
00162     datalen = read_uint32( s );
00163     compressedlen = read_uint32( s );
00164 
00165     if( datalen > len )
00166         throw( Error( ERROR_LOCATION, "compressed data length longer than expected (" + 
00167                       to_string(datalen) + " > " + to_string(len) ) );
00168 
00169     Bytef  *buf = new Bytef [compressedlen];
00170     uLongf  tmp = datalen;
00171     s.read( (char *)buf, compressedlen );
00172     uncompress( (Bytef *)dest, &tmp, buf, compressedlen );
00173     delete [] buf;
00174 
00175     return( datalen );
00176 }
00177 
00178 
00179 #endif
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198