ARTS 2.5.11 (git: 6827797f)
bifstream.h
Go to the documentation of this file.
1
2// File description
4
13#ifndef BIFSTREAM_H_INCLUDED
14#define BIFSTREAM_H_INCLUDED
15
16#include <fstream>
17
18#include "binio.h"
19#include "debug.h"
20
22
26class bifstream final : public binistream, public ifstream {
27 public:
28 bifstream() : ifstream() {}
29
30 explicit bifstream(const char* name,
31 ios::openmode mode = ios::in | ios::binary)
32 : ifstream(name, mode) {
33 // Open a second file descriptor for fast array reading
34 if (!(this->mfilep = fopen(name, "rb"))) {
35 ARTS_USER_ERROR("Failed to open ", name);
36 }
37 }
38
39 ~bifstream() final {
40 if (mfilep) {
41 fclose(mfilep);
42 }
43 }
44
45 void seek(long spos, Offset offs) final;
46 streampos pos() final;
47
48 bifstream::Byte getByte() final;
49 void getRaw(char* c, streamsize n) final {
50 if (n <= 8) {
51 this->read(c, n);
52 } else {
53 fseek(mfilep, this->tellg(), SEEK_SET);
54 size_t nread = fread(c, sizeof(char), n, mfilep);
55 ARTS_USER_ERROR_IF((streamsize)nread != n,
56 "Unexpectedly reached end of binary input file.");
57 seek(nread, Add);
58 }
59 }
60
61private : FILE* mfilep{nullptr};
62};
63
64/* Overloaded input operators */
65bifstream& operator>>(bifstream& bif, double& n);
66
67bifstream& operator>>(bifstream& bif, float& n);
68
69bifstream& operator>>(bifstream& bif, std::int64_t& n);
70
71bifstream& operator>>(bifstream& bif, int& n);
72
73#endif
bifstream & operator>>(bifstream &bif, double &n)
Definition: bifstream.cc:58
Binary output file stream class.
Definition: bifstream.h:26
void seek(long spos, Offset offs) final
Definition: bifstream.cc:17
void getRaw(char *c, streamsize n) final
Definition: bifstream.h:49
streampos pos() final
Definition: bifstream.cc:36
FILE * mfilep
Definition: bifstream.h:61
bifstream::Byte getByte() final
Definition: bifstream.cc:44
bifstream(const char *name, ios::openmode mode=ios::in|ios::binary)
Definition: bifstream.h:30
~bifstream() final
Definition: bifstream.h:39
bifstream()
Definition: bifstream.h:28
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:151
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:135
#define c