BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnIFileReader.h
Go to the documentation of this file.
1
#pragma once
2
#ifndef BmnIFileReader_h
3
#define BmnIFileReader_h
4
5
// headers from this target
6
#include "
BmnFileProcessingFlags.h
"
7
// std:: headers
8
#include <filesystem>
9
// FairLogger headers
10
#include <fairlogger/Logger.h>
11
12
// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
13
// ================== BmnIFileReader class ==================
14
// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
15
16
/*
17
* An abstract interface class for reading files. The primary goal of this class is to provide some
18
* basic-reading functionality while children classes will implement various ways of extracting data
19
* from files (mostly from compressed files as this is the goal).
20
*/
21
class
BmnIFileReader
22
{
23
public
:
24
/*
25
* A constructor that's called from one of derived classes.
26
* @param fileType A type of files that the derived-caller class can process
27
* @param fileName A name of the file that's expected to be in the currently-working director
28
* @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
29
*/
30
explicit
BmnIFileReader
(
const
BmnFileType
fileType,
const
std::string& fileName =
""
);
31
32
/*
33
* A constructor that's called from one of derived classes.
34
* @param fileType A type of files that the derived-caller class can process
35
* @param fileName A name of the file that's expected to be in the currently-working director
36
* @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
37
*/
38
explicit
BmnIFileReader
(
const
BmnFileType
fileType, std::string&& fileName)
noexcept
;
39
40
/*
41
* A constructor that's called from one of derived classes.
42
* @param fileType A type of files that the derived-caller class can process
43
* @param filePath A path to the file that's expected to be read
44
* @warning If you provide a relative path, be very careful that it's read relative to the currently-working
45
* directory!
46
*/
47
explicit
BmnIFileReader
(
const
BmnFileType
fileType,
const
std::filesystem::path& filePath);
48
49
/*
50
* A constructor that's called from one of derived classes.
51
* @param fileType A type of files that the derived-caller class can process
52
* @param filePath A path to the file that's expected to be read
53
* @warning If you provide a relative path, be very careful that it's read relative to the currently-working
54
* directory!
55
*/
56
explicit
BmnIFileReader
(
const
BmnFileType
fileType, std::filesystem::path&& filePath)
noexcept
;
57
58
virtual
~BmnIFileReader
() =
default
;
59
BmnIFileReader
(
const
BmnIFileReader
&) =
delete
;
60
BmnIFileReader
&
operator=
(
const
BmnIFileReader
&) =
delete
;
61
BmnIFileReader
(
BmnIFileReader
&&) noexcept = delete;
62
BmnIFileReader
& operator=(
BmnIFileReader
&&) noexcept = delete;
63
64
/*
65
* A method that opens a binary file for reading according to the previously provided file path
66
* @param processingFlags Flags that should be used for reading the file. Currently, only `ReadBinary` is supported
67
*/
68
virtual
void
Open
(
69
const
BmnBinaryFileProcessingOptions
processingFlags =
BmnBinaryFileProcessingOptions
::
ReadBinary
) = 0;
70
71
/*
72
* A method that opens a binary file for reading
73
* @param fileName A name of the file that's expected to be in the currently-working director
74
* @param processingFlags Flags that should be used for reading the file. Currently, only `ReadBinary` is supported
75
* @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
76
*/
77
virtual
void
Open
(
78
const
std
::
string
& fileName,
79
const
BmnBinaryFileProcessingOptions
processingFlags =
BmnBinaryFileProcessingOptions
::
ReadBinary
);
80
81
/*
82
* A method that opens a binary file for reading
83
* @param fileName A name of the file that's expected to be in the currently-working director
84
* @param processingFlags Flags that should be used for reading the file. Currently, only `ReadBinary` is supported
85
* @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
86
*/
87
virtual
void
Open
(
88
std
::
string
&& fileName,
89
const
BmnBinaryFileProcessingOptions
processingFlags =
BmnBinaryFileProcessingOptions
::
ReadBinary
);
90
91
/*
92
* A method that opens a binary file for reading
93
* @param filePath A path to the file that's expected to be read
94
* @param processingFlags Flags that should be used for reading the file. Currently, only `ReadBinary` is supported
95
* @warning If you provide a relative path, be very careful that it's read relative to the currently-working
96
* directory!
97
*/
98
virtual
void
Open
(
99
const
std
::filesystem::path& filePath,
100
const
BmnBinaryFileProcessingOptions
processingFlags =
BmnBinaryFileProcessingOptions
::
ReadBinary
);
101
102
/*
103
* A method that opens a binary file for reading
104
* @param filePath A path to the file that's expected to be read
105
* @param processingFlags Flags that should be used for reading the file. Currently, only `ReadBinary` is supported
106
* @warning If you provide a relative path, be very careful that it's read relative to the currently-working
107
* directory!
108
*/
109
virtual
void
Open
(
110
std
::filesystem::path&& filePath,
111
const
BmnBinaryFileProcessingOptions
processingFlags =
BmnBinaryFileProcessingOptions
::
ReadBinary
);
112
113
/*
114
* A method that closes an open file stream and clears/removes all buffers.
115
* This method should be used before reopening a file.
116
*/
117
virtual
void
Close
() noexcept = 0;
118
119
/*
120
* A method that extracts a requested number of bytes and puts them into the provided array
121
* @param dataPointer A buffer for inserting new data
122
* @param numberOfBytesToRead A number of bytes that should be copied into the provided buffer
123
* @return The number of bytes that have actually been copied. If less than `numberOfBytesToRead`, it means we've
124
* hit EOF or an error
125
*/
126
virtual int64_t
Read
(
void
* dataPointer, int64_t numberOfBytesToRead) = 0;
127
128
/*
129
* @return A next byte ID that's to be read
130
*/
131
[[nodiscard]] virtual int64_t
Tell
() const noexcept = 0;
132
133
/*
134
* Move the file reading ID to another position
135
* @param aNewReadingPosition A next position to read
136
* @warning If compressed files are used, this might still read and uncomress the data. The method might not have
137
* high performance
138
*/
139
virtual
void
Seek
(int64_t aNewReadingPosition) = 0;
140
141
/*
142
* @return 'true' if no error has been detected while processing an input stream, otherwise 'false'
143
*/
144
[[nodiscard]] virtual
bool
IsGood
() const noexcept = 0;
145
146
/*
147
* @return 'true' if we have reached EOF, otherwise 'false'
148
*/
149
[[nodiscard]] virtual
bool
IsEOF
() const noexcept = 0;
150
151
/*
152
* A method that clears bad flags after an error has occurred. Just calling this error will most likely
153
* not solve the problem, and you will get an error again in the next read. That is, before calling `Read`,
154
* remove the previous error source
155
*/
156
virtual
void
ClearStateFlags
() = 0;
157
/*
158
* @return 'true' if the input file is open, otherwise 'false'
159
*/
160
[[nodiscard]] virtual
bool
IsOpen
() const noexcept = 0;
161
162
public:
163
// inline methods
164
165
// This will set the path only if the file is NOT already open!
166
inline
void
SetFilePath
(const
std
::
string
& fileName);
167
// This will set the path only if the file is NOT already open!
168
inline
void
SetFilePath
(
std
::
string
&& fileName) noexcept;
169
// This will set the path only if the file is NOT already open!
170
inline
void
SetFilePath
(const
std
::filesystem::path& filePath);
171
// This will set the path only if the file is NOT already open!
172
inline
void
SetFilePath
(
std
::filesystem::path&& filePath) noexcept;
173
[[nodiscard]] inline
std
::filesystem::path&
GetFilePath
() noexcept;
174
[[nodiscard]] inline const
std
::filesystem::path&
GetFilePath
() const noexcept;
175
// @warning This method may throw - e.g., permission denied
176
[[nodiscard]] inline int64_t
GetInputFileSize
() const;
177
178
// Setting flags, i.e., old flags are lost!
179
inline
void
SetProcessingFlags
(const
BmnBinaryFileProcessingOptions
) noexcept;
180
// Adding new flags, i.e., old flags are preserved!
181
inline
void
AddProcessingFlags
(const
BmnBinaryFileProcessingOptions
) noexcept;
182
// Remove specific flags if they are valid
183
inline
void
RemoveProcessingFlags
(const
BmnBinaryFileProcessingOptions
) noexcept;
184
// Get current processing flags
185
[[nodiscard]] inline
BmnBinaryFileProcessingOptions
GetProcessingFlags
() const noexcept;
186
187
[[nodiscard]] inline
BmnFileType
GetFileType
() const noexcept;
188
189
protected:
190
/*
191
* Check the path and input flags that are always necessary for opening a file.
192
* @param processingFlags Flags that are passed into an open function
193
* @return 'true' if all conditions are satisfied, 'false' otherwise
194
*/
195
[[nodiscard]]
bool
PerformStandardChecksBeforeOpening
(const
BmnBinaryFileProcessingOptions
processingFlags);
196
197
protected:
198
// after opening a file, we don't need this path at all, but still, keeping it here for sanity and debugging reasons
199
std
::filesystem::path
fFilePath
;
200
// Flags are settable before the file is opened
201
BmnBinaryFileProcessingOptions
fProcessingFlags
;
202
// The underlying type that's managing reading - we don't need this one here, but we will have 4 padding bytes
203
// anyway so....
204
BmnFileType
fFileType
;
205
// 3 padding bytes
206
};
207
208
// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
209
// ================== BmnIFileReader inline methods ==================
210
// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
211
212
void
BmnIFileReader
::
SetFilePath
(const
std
::
string
& fileName)
213
{
214
if
(
IsOpen
()) {
215
LOG(error) <<
"You may not change file name of BmnIFileReader when it's already open!\n"
;
216
return
;
217
}
218
fFilePath
= fileName;
219
}
220
221
void
BmnIFileReader::SetFilePath
(std::string&& fileName)
noexcept
222
{
223
if
(
IsOpen
()) {
224
LOG(error) <<
"You may not change file name of BmnIFileReader when it's already open!\n"
;
225
return
;
226
}
227
fFilePath
= std::move(fileName);
228
}
229
230
void
BmnIFileReader::SetFilePath
(
const
std::filesystem::path& filePath)
231
{
232
if
(
IsOpen
()) {
233
LOG(error) <<
"You may not change file path of BmnIFileReader when it's already open!\n"
;
234
return
;
235
}
236
fFilePath
= filePath;
237
}
238
239
void
BmnIFileReader::SetFilePath
(std::filesystem::path&& filePath)
noexcept
240
{
241
if
(
IsOpen
()) {
242
LOG(error) <<
"You may not change file path of BmnIFileReader when it's already open!\n"
;
243
return
;
244
}
245
fFilePath
= std::move(filePath);
246
}
247
248
std::filesystem::path&
BmnIFileReader::GetFilePath
() noexcept
249
{
250
return
fFilePath
;
251
}
252
253
const
std::filesystem::path&
BmnIFileReader::GetFilePath
() const noexcept
254
{
255
return
fFilePath
;
256
}
257
258
int64_t
BmnIFileReader::GetInputFileSize
()
const
259
{
260
if
(
fFilePath
.empty())
261
return
0ll;
262
return
std::filesystem::file_size(
fFilePath
);
263
}
264
265
void
BmnIFileReader::SetProcessingFlags
(
const
BmnBinaryFileProcessingOptions
newFlags)
noexcept
266
{
267
if
(
IsOpen
()) {
268
LOG(error) <<
"You may not modify flags of BmnIFileReader when a file is already open!\n"
;
269
return
;
270
}
271
fProcessingFlags
= newFlags;
272
}
273
274
void
BmnIFileReader::AddProcessingFlags
(
const
BmnBinaryFileProcessingOptions
newFlags)
noexcept
275
{
276
if
(
IsOpen
()) {
277
LOG(error) <<
"You may not modify flags of BmnIFileReader when a file is already open!\n"
;
278
return
;
279
}
280
fProcessingFlags
|= newFlags;
281
}
282
283
void
BmnIFileReader::RemoveProcessingFlags
(
const
BmnBinaryFileProcessingOptions
badFlags)
noexcept
284
{
285
if
(
IsOpen
()) {
286
LOG(error) <<
"You may not modify flags of BmnIFileReader when a file is already open!\n"
;
287
return
;
288
}
289
fProcessingFlags
&= (~badFlags);
290
}
291
292
BmnBinaryFileProcessingOptions
BmnIFileReader::GetProcessingFlags
() const noexcept
293
{
294
return
fProcessingFlags
;
295
}
296
297
BmnFileType
BmnIFileReader::GetFileType
() const noexcept
298
{
299
return
fFileType
;
300
}
301
302
#endif
// BmnIFileReader_h
BmnIFileReader::~BmnIFileReader
virtual ~BmnIFileReader()=default
BmnIFileReader::BmnIFileReader
BmnIFileReader(const BmnFileType fileType, const std::string &fileName="")
Definition
BmnIFileReader.cxx:8
BmnIFileReader::Close
virtual void Close() noexcept=0
BmnIFileReader::fFilePath
std::filesystem::path fFilePath
Definition
BmnIFileReader.h:199
BmnIFileReader::PerformStandardChecksBeforeOpening
bool PerformStandardChecksBeforeOpening(const BmnBinaryFileProcessingOptions processingFlags)
Definition
BmnIFileReader.cxx:72
BmnIFileReader::SetFilePath
void SetFilePath(const std::string &fileName)
Definition
BmnIFileReader.h:212
BmnIFileReader::RemoveProcessingFlags
void RemoveProcessingFlags(const BmnBinaryFileProcessingOptions) noexcept
Definition
BmnIFileReader.h:283
BmnIFileReader::fProcessingFlags
BmnBinaryFileProcessingOptions fProcessingFlags
Definition
BmnIFileReader.h:201
BmnIFileReader::BmnIFileReader
BmnIFileReader(BmnIFileReader &&) noexcept=delete
BmnIFileReader::operator=
BmnIFileReader & operator=(const BmnIFileReader &)=delete
BmnIFileReader::GetFilePath
std::filesystem::path & GetFilePath() noexcept
Definition
BmnIFileReader.h:248
BmnIFileReader::GetProcessingFlags
BmnBinaryFileProcessingOptions GetProcessingFlags() const noexcept
Definition
BmnIFileReader.h:292
BmnIFileReader::IsOpen
virtual bool IsOpen() const noexcept=0
BmnIFileReader::Open
virtual void Open(const BmnBinaryFileProcessingOptions processingFlags=BmnBinaryFileProcessingOptions::ReadBinary)=0
BmnIFileReader::Seek
virtual void Seek(int64_t aNewReadingPosition)=0
BmnIFileReader::GetFileType
BmnFileType GetFileType() const noexcept
Definition
BmnIFileReader.h:297
BmnIFileReader::Tell
virtual int64_t Tell() const noexcept=0
BmnIFileReader::fFileType
BmnFileType fFileType
Definition
BmnIFileReader.h:204
BmnIFileReader::IsEOF
virtual bool IsEOF() const noexcept=0
BmnIFileReader::GetInputFileSize
int64_t GetInputFileSize() const
Definition
BmnIFileReader.h:258
BmnIFileReader::BmnIFileReader
BmnIFileReader(const BmnIFileReader &)=delete
BmnIFileReader::SetProcessingFlags
void SetProcessingFlags(const BmnBinaryFileProcessingOptions) noexcept
Definition
BmnIFileReader.h:265
BmnIFileReader::AddProcessingFlags
void AddProcessingFlags(const BmnBinaryFileProcessingOptions) noexcept
Definition
BmnIFileReader.h:274
BmnIFileReader::Read
virtual int64_t Read(void *dataPointer, int64_t numberOfBytesToRead)=0
BmnIFileReader::IsGood
virtual bool IsGood() const noexcept=0
BmnIFileReader::ClearStateFlags
virtual void ClearStateFlags()=0
BmnFileProcessingFlags.h
BmnFileType
BmnFileType
Definition
BmnFileProcessingFlags.h:16
BmnBinaryFileProcessingOptions
BmnBinaryFileProcessingOptions
Definition
BmnFileProcessingFlags.h:95
BmnBinaryFileProcessingOptions::ReadBinary
@ ReadBinary
Definition
BmnFileProcessingFlags.h:103
std
Definition
json.hpp:4446
compression
include
BmnIFileReader.h
Generated on
for BmnRoot by
1.17.0