Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JFileStreamIO.hh
Go to the documentation of this file.
1#ifndef __JIO__JFILESTREAMIO__
2#define __JIO__JFILESTREAMIO__
3
4#include <fstream>
5
6#include "JIO/JStreamIO.hh"
7#include "JIO/JBufferedIO.hh"
8
9
10/**
11 * \author mdejong
12 */
13
14namespace JIO {}
15namespace JPP { using namespace JIO; }
16
17namespace JIO {
18
19 /**
20 * Binary buffered file input.
21 */
23 public std::ifstream,
24 public JBufferedReader
25 {
26 public:
27
28 using JAbstractObjectStatus::operator bool;
29 using JAbstractObjectStatus::operator!;
30 using JReader::operator>>;
31
32
33 /**
34 * Default constructor.
35 */
37 std::ifstream(),
38 JBufferedReader(new JStreamReader(static_cast<std::ifstream&>(*this)))
39 {}
40
41
42 /**
43 * Constructor.
44 *
45 * \param file_name file name
46 * \param size size of internal buffer
47 */
48 JFileStreamReader(const char* file_name,
49 const int size = 1048576) :
50 std::ifstream (),
51 JBufferedReader(new JStreamReader(static_cast<std::ifstream&>(*this)), size)
52 {
53 open(file_name);
54 }
55
56
57 /**
58 * Open file.
59 *
60 * \param file_name file name
61 */
62 void open(const char* file_name)
63 {
64 static_cast<std::ifstream*>(this)->open(file_name, std::ios::binary);
65 }
66
67
68 /**
69 * Clear status of reader.
70 */
71 virtual void clear() override
72 {
73 std::ifstream ::clear();
75 }
76
77
78 /**
79 * Rewind.
80 */
81 void rewind()
82 {
83 seekg(0); // rewind file stream
84
85 reset(); // reset buffer
86 }
87 };
88
89
90 /**
91 * Binary buffered file output.
92 */
94 public std::ofstream,
95 public JBufferedWriter
96 {
97 public:
98
99 using JAbstractObjectStatus::operator bool;
100 using JAbstractObjectStatus::operator!;
101 using JWriter::operator<<;
102
103
104 /**
105 * Default constructor.
106 */
108 std::ofstream(),
109 JBufferedWriter(new JStreamWriter(static_cast<std::ofstream&>(*this)))
110 {}
111
112
113 /**
114 * Constructor.
115 *
116 * \param file_name file name
117 * \param size size of internal buffer
118 */
119 JFileStreamWriter(const char* file_name,
120 const int size = 1048576) :
121 std::ofstream(),
122 JBufferedWriter(new JStreamWriter(static_cast<std::ofstream&>(*this)), size)
123 {
124 open(file_name);
125 }
126
127
128 /**
129 * Open file.
130 *
131 * \param file_name file name
132 */
133 void open(const char* file_name)
134 {
135 static_cast<std::ofstream*>(this)->open(file_name, std::ios::binary);
136 }
137
138
139 /**
140 * Close file.
141 */
142 void close()
143 {
144 static_cast<JBufferedWriter*>(this)->flush();
145 static_cast<std::ofstream*> (this)->close();
146 }
147 };
148}
149
150#endif
Buffered binary input.
virtual void clear() override
Clear status of reader.
int size
size of internal buffer
Buffered binary output.
int size
size of internal buffer
void flush()
Write internal data to output.
Binary buffered file input.
JFileStreamReader()
Default constructor.
virtual void clear() override
Clear status of reader.
JFileStreamReader(const char *file_name, const int size=1048576)
Constructor.
void open(const char *file_name)
Open file.
Binary buffered file output.
void close()
Close file.
JFileStreamWriter()
Default constructor.
void open(const char *file_name)
Open file.
JFileStreamWriter(const char *file_name, const int size=1048576)
Constructor.
Binary input based on std::istream.
Definition JStreamIO.hh:26
Binary output based on std::ostream.
Definition JStreamIO.hh:83
Auxiliary classes and methods for binary I/O.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).