ARTS  2.4.0(git:4fb77825)
bofstream.cc
Go to the documentation of this file.
1 /* Copyright (C) 2003-2012 Oliver Lemke <olemke@core-dump.info>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
19 // File description
21 
30 #include "bofstream.h"
31 #include <fstream>
32 #include <stdexcept>
33 
34 void bofstream::seek(long spos, Offset offs) {
35  if (!in) {
36  err = NotOpen;
37  return;
38  }
39 
40  switch (offs) {
41  case Set:
42  this->seekp(spos, ios::beg);
43  break;
44  case Add:
45  this->seekp(spos, ios::cur);
46  break;
47  case End:
48  this->seekp(spos, ios::end);
49  break;
50  }
51 }
52 
53 streampos bofstream::pos() {
54  if (!in) {
55  err = NotOpen;
56  return 0;
57  }
58  return streamoff(this->tellp());
59 }
60 
62  if (!this->good()) {
63  err |= NotOpen;
64  throw runtime_error("Cannot open binary file for writing");
65  return;
66  }
67 
68  this->put(b);
69  if (this->bad()) {
70  err |= Fatal;
71  throw runtime_error("Writing to binary file failed");
72  }
73 }
74 
75 /* Overloaded output operators */
76 bofstream& operator<<(bofstream& bof, double n) {
77  bof.writeFloat(n, binio::Double);
78  return (bof);
79 }
80 
81 bofstream& operator<<(bofstream& bof, float n) {
82  bof.writeFloat(n, binio::Double);
83  return (bof);
84 }
85 
86 bofstream& operator<<(bofstream& bof, long n) {
87  bof.writeInt(n, 4);
88  return (bof);
89 }
90 
92  bof.writeInt(n, 4);
93  return (bof);
94 }
binio::Set
@ Set
Definition: binio.h:80
binio::err
Error err
Definition: binio.h:105
operator<<
bofstream & operator<<(bofstream &bof, double n)
Definition: bofstream.cc:76
binio::Fatal
@ Fatal
Definition: binio.h:72
binio::Add
@ Add
Definition: binio.h:80
binostream::writeFloat
void writeFloat(Float f, FType ft)
Definition: binio.cc:420
binostream::writeInt
void writeInt(Int val, unsigned int size)
Definition: binio.cc:399
bofstream::seek
void seek(long spos, Offset offs) override final
Definition: bofstream.cc:34
binio::End
@ End
Definition: binio.h:80
bofstream.h
This file contains the class declaration of bofstream.
Zeeman::end
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:108
bofstream::pos
streampos pos() override final
Definition: bofstream.cc:53
binio::Offset
Offset
Definition: binio.h:80
binio::NotOpen
@ NotOpen
Definition: binio.h:74
bofstream::putByte
void putByte(bofstream::Byte b) override final
Definition: bofstream.cc:61
binio::Byte
unsigned char Byte
Definition: binio.h:99
bofstream
Binary output file stream class.
Definition: bofstream.h:42
binio::Double
@ Double
Definition: binio.h:81