ARTS 2.5.4 (git: 31ce4f0e)
bifstream.h
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#ifndef BIFSTREAM_H_INCLUDED
31#define BIFSTREAM_H_INCLUDED
32
33#include <fstream>
34
35#include "binio.h"
36#include "debug.h"
37
39
43class bifstream final : public binistream, public ifstream {
44 public:
45 bifstream() : ifstream() {}
46
47 explicit bifstream(const char* name,
48 ios::openmode mode = ios::in | ios::binary)
49 : ifstream(name, mode) {
50 // Open a second file descriptor for fast array reading
51 if (!(this->mfilep = fopen(name, "rb"))) {
52 ARTS_USER_ERROR("Failed to open ", name);
53 }
54 }
55
56 ~bifstream() final {
57 if (mfilep) {
58 fclose(mfilep);
59 }
60 }
61
62 void seek(long spos, Offset offs) final;
63 streampos pos() final;
64
65 bifstream::Byte getByte() final;
66 void getRaw(char* c, streamsize n) final {
67 if (n <= 8) {
68 this->read(c, n);
69 } else {
70 fseek(mfilep, this->tellg(), SEEK_SET);
71 size_t nread = fread(c, sizeof(char), n, mfilep);
72 ARTS_USER_ERROR_IF((streamsize)nread != n,
73 "Unexpectedly reached end of binary input file.");
74 seek(nread, Add);
75 }
76 }
77
78private : FILE* mfilep{nullptr};
79};
80
81/* Overloaded input operators */
82bifstream& operator>>(bifstream& bif, double& n);
83
84bifstream& operator>>(bifstream& bif, float& n);
85
86bifstream& operator>>(bifstream& bif, std::int64_t& n);
87
88bifstream& operator>>(bifstream& bif, int& n);
89
90#endif
bifstream & operator>>(bifstream &bif, double &n)
Definition: bifstream.cc:75
Binary output file stream class.
Definition: bifstream.h:43
void seek(long spos, Offset offs) final
Definition: bifstream.cc:34
void getRaw(char *c, streamsize n) final
Definition: bifstream.h:66
streampos pos() final
Definition: bifstream.cc:53
FILE * mfilep
Definition: bifstream.h:78
bifstream::Byte getByte() final
Definition: bifstream.cc:61
bifstream(const char *name, ios::openmode mode=ios::in|ios::binary)
Definition: bifstream.h:47
~bifstream() final
Definition: bifstream.h:56
bifstream()
Definition: bifstream.h:45
enum { Set, Add, End } Offset
Definition: binio.h:64
unsigned char Byte
Definition: binio.h:82
Helper macros for debugging.
#define ARTS_USER_ERROR(...)
Definition: debug.h:150
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:134
void read(HitranRelaxationMatrixData &hitran, ArrayOfAbsorptionLines &bands, const SpeciesIsotopologueRatios &isotopologue_ratio, const String &basedir, const Numeric linemixinglimit, const Numeric fmin, const Numeric fmax, const Numeric stot, const ModeOfLineMixing mode)
Read from HITRAN online line mixing file.
#define c