BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnCompressionGlobalFunctions.cxx
Go to the documentation of this file.
1
// headers from this target
2
#include "BmnCompressionGlobalFunctions.h"
3
4
#include "BmnDefinitions.h"
5
#include "BmnFileReaderRaw.h"
6
#include "BmnFileReaderXzLzma.h"
7
#include "BmnFileReaderZLib.h"
8
#include "BmnFileReaderZLibNG.h"
9
#include "BmnFileReaderZStd.h"
10
// std:: headers
11
#include <algorithm>
12
#include <array>
13
#include <cctype>
14
#include <iterator>
15
#include <string>
16
#ifdef BMNCOMPRESSION_WITH_ZSTD
17
#include <zstd.h>
18
#endif
// BMNCOMPRESSION_WITH_ZSTD
19
20
// 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
21
// ================== static function declaration ==================
22
// 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
23
24
/*
25
* A translation-unit function that will be updated as we add new reading libraries. This function
26
* opens a stream for the provided path (if one exists) and based on magic bytes, it returns what kind
27
* of file reader should be created
28
* @param aFilePath A path to an existing actual and accessible file that should be read
29
* @param aUserSelectedAlgorithm A preferred algorithm selected by a user and passed into
30
* `BmnCompressionIsArchiveLibAvailable`
31
* @return A type of the file that should be opened
32
*/
33
[[nodiscard]]
static
BmnFileType
DoCommonProcessingOfFileFinding
(
const
std::filesystem::path& aFilePath,
34
const
BmnFileType
aUserSelectedAlgorithm);
35
36
// 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
37
// ================== Global functions ==================
38
// 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
39
40
bool
BmnCompressionIsArchiveLibAvailable
(
const
BmnFileType
aLibraryType)
noexcept
41
{
42
switch
(aLibraryType) {
43
case
BmnFileType::ZStd
:
44
#ifdef BMNCOMPRESSION_WITH_ZSTD
45
return
true
;
46
#else
47
return
false
;
48
#endif
// BMNCOMPRESSION_WITH_ZSTD
49
case
BmnFileType::ZLib
:
50
#ifdef BMNCOMPRESSION_WITH_ZLIB
51
return
true
;
52
#else
53
return
false
;
54
#endif
// BMNCOMPRESSION_WITH_ZLIB
55
case
BmnFileType::ZLib_ng
:
56
#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
57
return
true
;
58
#else
59
return
false
;
60
#endif
// BMNCOMPRESSION_WITH_ZLIB_NG
61
case
BmnFileType::XZ_LZMA
:
62
#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
63
return
true
;
64
#else
65
return
false
;
66
#endif
// BMNCOMPRESSION_WITH_XZ_LZMA
67
case
BmnFileType::Raw
:
68
return
true
;
69
case
BmnFileType::Unknown
:
70
default
:
71
return
false
;
72
}
73
}
74
75
BmnIFileReader
*
BmnCompressionCreateNewSuitableReaderForAFile
(
const
std::filesystem::path& aFilePath,
76
const
BmnFileType
aPreferredTypeForThisAlgorithm)
77
{
78
BmnFileType
aNeededType =
DoCommonProcessingOfFileFinding
(aFilePath, aPreferredTypeForThisAlgorithm);
79
switch
(aNeededType) {
80
#ifdef BMNCOMPRESSION_WITH_ZSTD
81
case
BmnFileType::ZStd
:
82
return
new
BmnFileReaderZStd
{aFilePath};
83
#endif
// BMNCOMPRESSION_WITH_ZSTD
84
#ifdef BMNCOMPRESSION_WITH_ZLIB
85
case
BmnFileType::ZLib
:
86
return
new
BmnFileReaderZLib
{aFilePath};
87
#endif
// BMNCOMPRESSION_WITH_ZLIB
88
#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
89
case
BmnFileType::ZLib_ng
:
90
return
new
BmnFileReaderZLibNG
{aFilePath};
91
#endif
// BMNCOMPRESSION_WITH_ZLIB_NG
92
#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
93
case
BmnFileType::XZ_LZMA
:
94
return
new
BmnFileReaderXzLzma
{aFilePath};
95
#endif
// BMNCOMPRESSION_WITH_XZ_LZMA
96
case
BmnFileType::Raw
:
97
return
new
BmnFileReaderRaw
{aFilePath};
98
case
BmnFileType::Unknown
:
99
default
:
100
return
nullptr
;
101
}
102
}
103
104
BmnIFileReader
*
BmnCompressionCreateNewSuitableReaderForAFile
(std::filesystem::path&& aFilePath,
105
const
BmnFileType
aPreferredTypeForThisAlgorithm)
106
{
107
BmnFileType
aNeededType =
DoCommonProcessingOfFileFinding
(aFilePath, aPreferredTypeForThisAlgorithm);
108
switch
(aNeededType) {
109
case
BmnFileType::ZStd
:
110
#ifdef BMNCOMPRESSION_WITH_ZSTD
111
return
new
BmnFileReaderZStd
{std::move(aFilePath)};
112
#endif
// BMNCOMPRESSION_WITH_ZSTD
113
#ifdef BMNCOMPRESSION_WITH_ZLIB
114
case
BmnFileType::ZLib
:
115
return
new
BmnFileReaderZLib
{std::move(aFilePath)};
116
#endif
// BMNCOMPRESSION_WITH_ZLIB
117
#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
118
case
BmnFileType::ZLib_ng
:
119
return
new
BmnFileReaderZLibNG
{std::move(aFilePath)};
120
#endif
// BMNCOMPRESSION_WITH_ZLIB_NG
121
#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
122
case
BmnFileType::XZ_LZMA
:
123
return
new
BmnFileReaderXzLzma
{std::move(aFilePath)};
124
#endif
// BMNCOMPRESSION_WITH_XZ_LZMA
125
case
BmnFileType::Raw
:
126
return
new
BmnFileReaderRaw
{std::move(aFilePath)};
127
case
BmnFileType::Unknown
:
128
default
:
129
return
nullptr
;
130
}
131
}
132
133
std::filesystem::path
BmnCompressionRemoveArchiveExtension
(std::filesystem::path aFilePath)
134
{
135
std::string fileExtension = aFilePath.extension().string();
136
// '.ZST' names the very same format as '.zst', so the extensions are compared in the lower case
137
std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(),
138
[](
unsigned
char
aSymbol) { return static_cast<char>(std::tolower(aSymbol)); });
139
140
// the list of the archive extensions is declared in BmnCompressionGlobalFunctions.h
141
if
(std::find(std::cbegin(
BmnCompressionArchiveExtensions
), std::cend(
BmnCompressionArchiveExtensions
),
142
fileExtension)
143
== std::cend(
BmnCompressionArchiveExtensions
))
144
return
aFilePath;
145
146
return
aFilePath.replace_extension();
147
}
148
149
// 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
150
// ================== static function definition ==================
151
// 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
152
153
DISABLE_UNUSED_PARAM_WARNING
154
static
BmnFileType
DoCommonProcessingOfFileFinding
(
const
std::filesystem::path& aFilePath,
155
const
BmnFileType
aUserSelectedAlgorithm)
156
{
157
if
(aFilePath.empty()) {
158
LOG(error) <<
"You did not provide a path for opening a file reader!\n"
;
159
return
BmnFileType::Unknown
;
160
}
161
try
{
162
if
(!std::filesystem::exists(aFilePath) || !std::filesystem::is_regular_file(aFilePath)) {
163
LOG(error) <<
"You should provide an existing path to an actual file for opening a file reader!\n"
;
164
return
BmnFileType::Unknown
;
165
}
166
}
catch
(
const
std::bad_alloc& errorDescription) {
167
LOG(error) <<
"Filesystem failure! Reason: "
<< errorDescription.what() <<
'\n'
;
168
return
BmnFileType::Unknown
;
169
}
catch
(
const
std::filesystem::filesystem_error& errorDescription) {
170
LOG(error) <<
"Filesystem failure! Reason: "
<< errorDescription.what() <<
'\n'
;
171
return
BmnFileType::Unknown
;
172
}
173
std::ifstream inStream{};
174
inStream.open(aFilePath, std::ios::in | std::ios::binary);
175
if
(!inStream.is_open()) {
176
LOG(error) <<
"Failed to open a file for checking a magic byte for opening a file reader!\n"
;
177
return
BmnFileType::Unknown
;
178
}
179
std::array<char, 15> beginningOfTheStream{};
180
inStream.read(beginningOfTheStream.data(), beginningOfTheStream.size());
181
// ================================================================================================================
182
#ifdef BMNCOMPRESSION_WITH_ZSTD
183
if
(*
reinterpret_cast<
uint32_t*
>
(beginningOfTheStream.data()) == ZSTD_MAGICNUMBER)
184
return
BmnFileType::ZStd
;
185
#endif
// BMNCOMPRESSION_WITH_ZSTD
186
// ================================================================================================================
187
#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
188
// I don't see this information in the XZ_LZMA API. Still, I found it on the internet and seems to work
189
constexpr
char
xzMagicByte[7] = {
"\xFD\x37\x7A\x58\x5A\x00"
};
190
for
(
size_t
i
= 0ull;
i
< 6; ++
i
) {
191
if
(beginningOfTheStream[
i
] != xzMagicByte[
i
])
192
break
;
193
// if we are here in the last pass, it means we have a match
194
if
(
i
== 5)
195
return
BmnFileType::XZ_LZMA
;
196
}
197
#endif
// BMNCOMPRESSION_WITH_XZ_LZMA
198
// ================================================================================================================
199
#if defined(BMNCOMPRESSION_WITH_ZLIB) || defined(BMNCOMPRESSION_WITH_ZLIB_NG)
200
// for now we expect GZip compression while later we might add additional support for files compressed directly by
201
// us. the deflate format itself doesn't have a magic byte, while zlib has various headers, but we will not be
202
// writing a direct ZLIB stream anyway
203
if
(beginningOfTheStream[0] ==
'\x1F'
&& beginningOfTheStream[1] ==
'\x8B'
) {
204
#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
205
#ifdef BMNCOMPRESSION_WITH_ZLIB
206
if
(aUserSelectedAlgorithm ==
BmnFileType::ZLib
)
207
return
BmnFileType::ZLib
;
208
#endif
// BMNCOMPRESSION_WITH_ZLIB
209
return
BmnFileType::ZLib_ng
;
210
#else
// defined(BMNCOMPRESSION_WITH_ZLIB) && !defined(BMNCOMPRESSION_WITH_ZLIB_NG)
211
return
BmnFileType::ZLib
;
212
#endif
// BMNCOMPRESSION_WITH_ZLIB_NG
213
}
214
#endif
// defined(BMNCOMPRESSION_WITH_ZLIB) || defined(BMNCOMPRESSION_WITH_ZLIB_NG)
215
// ================================================================================================================
216
return
BmnFileType::Raw
;
217
}
218
RESTORE_UNUSED_PARAM_WARNING
BmnCompressionIsArchiveLibAvailable
bool BmnCompressionIsArchiveLibAvailable(const BmnFileType aLibraryType) noexcept
Definition
BmnCompressionGlobalFunctions.cxx:40
DoCommonProcessingOfFileFinding
static BmnFileType DoCommonProcessingOfFileFinding(const std::filesystem::path &aFilePath, const BmnFileType aUserSelectedAlgorithm)
Definition
BmnCompressionGlobalFunctions.cxx:154
BmnCompressionRemoveArchiveExtension
std::filesystem::path BmnCompressionRemoveArchiveExtension(std::filesystem::path aFilePath)
Definition
BmnCompressionGlobalFunctions.cxx:133
BmnCompressionCreateNewSuitableReaderForAFile
BmnIFileReader * BmnCompressionCreateNewSuitableReaderForAFile(const std::filesystem::path &aFilePath, const BmnFileType aPreferredTypeForThisAlgorithm)
Definition
BmnCompressionGlobalFunctions.cxx:75
i
int i
Definition
P4_F32vec4.h:22
RESTORE_UNUSED_PARAM_WARNING
#define RESTORE_UNUSED_PARAM_WARNING
Definition
BmnDefinitions.h:23
DISABLE_UNUSED_PARAM_WARNING
#define DISABLE_UNUSED_PARAM_WARNING
Definition
BmnDefinitions.h:22
BmnFileReaderRaw
Definition
BmnFileReaderRaw.h:16
BmnFileReaderXzLzma
Definition
BmnFileReaderXzLzma.h:24
BmnFileReaderZLibNG
Definition
BmnFileReaderZLibNG.h:26
BmnFileReaderZLib
Definition
BmnFileReaderZLib.h:25
BmnFileReaderZStd
Definition
BmnFileReaderZStd.h:28
BmnIFileReader
Definition
BmnIFileReader.h:22
BmnCompressionArchiveExtensions
constexpr std::string_view BmnCompressionArchiveExtensions[]
Definition
BmnCompressionGlobalFunctions.h:16
BmnFileType
BmnFileType
Definition
BmnFileProcessingFlags.h:16
BmnFileType::ZLib
@ ZLib
Definition
BmnFileProcessingFlags.h:20
BmnFileType::Raw
@ Raw
Definition
BmnFileProcessingFlags.h:18
BmnFileType::Unknown
@ Unknown
Definition
BmnFileProcessingFlags.h:17
BmnFileType::XZ_LZMA
@ XZ_LZMA
Definition
BmnFileProcessingFlags.h:22
BmnFileType::ZStd
@ ZStd
Definition
BmnFileProcessingFlags.h:19
BmnFileType::ZLib_ng
@ ZLib_ng
Definition
BmnFileProcessingFlags.h:21
compression
sources
BmnCompressionGlobalFunctions.cxx
Generated on
for BmnRoot by
1.17.0