BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
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
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 */
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;
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(
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,
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,
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,
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,
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!
180 // Adding new flags, i.e., old flags are preserved!
182 // Remove specific flags if they are valid
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 */
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
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....
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
212void 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
221void 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
230void 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
239void 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
248std::filesystem::path& BmnIFileReader::GetFilePath() noexcept
249{
250 return fFilePath;
251}
252
253const std::filesystem::path& BmnIFileReader::GetFilePath() const noexcept
254{
255 return fFilePath;
256}
257
259{
260 if (fFilePath.empty())
261 return 0ll;
262 return std::filesystem::file_size(fFilePath);
263}
264
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
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
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
293{
294 return fProcessingFlags;
295}
296
298{
299 return fFileType;
300}
301
302#endif // BmnIFileReader_h
virtual ~BmnIFileReader()=default
BmnIFileReader(const BmnFileType fileType, const std::string &fileName="")
virtual void Close() noexcept=0
std::filesystem::path fFilePath
bool PerformStandardChecksBeforeOpening(const BmnBinaryFileProcessingOptions processingFlags)
void SetFilePath(const std::string &fileName)
void RemoveProcessingFlags(const BmnBinaryFileProcessingOptions) noexcept
BmnBinaryFileProcessingOptions fProcessingFlags
BmnIFileReader(const BmnFileType fileType, const std::filesystem::path &filePath)
BmnIFileReader(BmnIFileReader &&) noexcept=delete
BmnIFileReader & operator=(const BmnIFileReader &)=delete
std::filesystem::path & GetFilePath() noexcept
BmnBinaryFileProcessingOptions GetProcessingFlags() const noexcept
virtual bool IsOpen() const noexcept=0
virtual void Open(const BmnBinaryFileProcessingOptions processingFlags=BmnBinaryFileProcessingOptions::ReadBinary)=0
virtual void Seek(int64_t aNewReadingPosition)=0
BmnFileType GetFileType() const noexcept
virtual int64_t Tell() const noexcept=0
BmnFileType fFileType
virtual bool IsEOF() const noexcept=0
BmnIFileReader(const BmnFileType fileType, std::string &&fileName) noexcept
BmnIFileReader(const BmnFileType fileType, std::filesystem::path &&filePath) noexcept
int64_t GetInputFileSize() const
BmnIFileReader(const BmnIFileReader &)=delete
void SetProcessingFlags(const BmnBinaryFileProcessingOptions) noexcept
void AddProcessingFlags(const BmnBinaryFileProcessingOptions) noexcept
virtual int64_t Read(void *dataPointer, int64_t numberOfBytesToRead)=0
virtual bool IsGood() const noexcept=0
virtual void ClearStateFlags()=0
BmnBinaryFileProcessingOptions
Definition json.hpp:4446