Go to the documentation of this file.00001 #include "MagneticField/Interpolation/src/binary_ofstream.h"
00002
00003 #include <cstdio>
00004 #include <iostream>
00005
00006 struct binary_ofstream_error {};
00007
00008 binary_ofstream::binary_ofstream( const char* name) : file_(0)
00009 {
00010 init (name);
00011 }
00012
00013 binary_ofstream::binary_ofstream( const std::string& name) : file_(0)
00014 {
00015 init (name.c_str());
00016 }
00017
00018 void binary_ofstream::init( const char* name)
00019 {
00020 file_ = fopen( name, "wb");
00021 if (file_ == 0) {
00022 std::cout << "file " << name << " cannot be opened for writing"
00023 << std::endl;
00024 throw binary_ofstream_error();
00025 }
00026 }
00027
00028 binary_ofstream::~binary_ofstream()
00029 {
00030 close();
00031 }
00032 void binary_ofstream::close()
00033 {
00034 if (file_ != 0) fclose( file_);
00035 file_ = 0;
00036 }
00037
00038 binary_ofstream& binary_ofstream::operator<<( char n) {
00039 fputc( n, file_); return *this;
00040 }
00041 binary_ofstream& binary_ofstream::operator<<( unsigned char n) {
00042 fputc( n, file_); return *this;
00043 }
00044
00045 binary_ofstream& binary_ofstream::operator<<( short n) {
00046 fwrite( &n, sizeof(n), 1, file_); return *this;}
00047 binary_ofstream& binary_ofstream::operator<<( unsigned short n) {
00048 fwrite( &n, sizeof(n), 1, file_); return *this;}
00049 binary_ofstream& binary_ofstream::operator<<( int n) {
00050 fwrite( &n, sizeof(n), 1, file_); return *this;}
00051 binary_ofstream& binary_ofstream::operator<<( unsigned int n) {
00052 fwrite( &n, sizeof(n), 1, file_); return *this;}
00053 binary_ofstream& binary_ofstream::operator<<( long n) {
00054 fwrite( &n, sizeof(n), 1, file_); return *this;}
00055 binary_ofstream& binary_ofstream::operator<<( unsigned long n) {
00056 fwrite( &n, sizeof(n), 1, file_); return *this;}
00057 binary_ofstream& binary_ofstream::operator<<( float n) {
00058 fwrite( &n, sizeof(n), 1, file_); return *this;}
00059 binary_ofstream& binary_ofstream::operator<<( double n) {
00060 fwrite( &n, sizeof(n), 1, file_); return *this;}
00061
00062 binary_ofstream& binary_ofstream::operator<<( bool n) {
00063 return operator<<( static_cast<char>(n));
00064 }
00065
00066 binary_ofstream& binary_ofstream::operator<<( const std::string& s) {
00067 (*this) << s.size();
00068 fwrite( s.data(), 1, s.size(), file_);
00069 return *this;
00070 }