SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Toggle main menu visibility
Loading...
Searching...
No Matches
RIFF.cpp
Go to the documentation of this file.
1
#include <
sndpp/RIFF.h
>
2
3
#include <algorithm>
4
5
#include <BufferStream.h>
6
#include <
sourcepp/FS.h
>
7
8
using namespace
sndpp
;
9
using namespace
sourcepp
;
10
11
RIFF::RIFF
(uint32_t
type
, uint32_t
signature
)
12
:
signature
{
signature
}
13
,
type
{
type
} {
14
if
(this->signature !=
RIFF_SIGNATURE
&& this->signature !=
RIFX_SIGNATURE
&& this->signature !=
FFIR_SIGNATURE
) {
15
this->signature = 0;
16
}
17
}
18
19
RIFF::RIFF
(std::vector<std::byte>&& riffData, uint32_t type_)
20
:
type
{type_}
21
,
data
{std::move(riffData)} {
22
BufferStreamReadOnly stream{this->
data
};
23
24
stream >> this->
signature
;
25
if
(
signature
==
RIFX_SIGNATURE
||
signature
==
FFIR_SIGNATURE
) {
26
stream.set_big_endian(
true
);
27
}
else
if
(
signature
!=
RIFF_SIGNATURE
) {
28
this->signature = 0;
29
return
;
30
}
31
32
const
auto
fileLength = stream.read<uint32_t>();
33
if
(stream.read<uint32_t>() != this->type) {
34
this->signature = 0;
35
return;
36
}
37
38
while (stream.tell() < fileLength) {
39
auto& [chunkType, chunkData] = this->chunks.emplace_back();
40
stream.read(chunkType).read(chunkData, stream.read<uint32_t>());
41
if (stream.tell() % 2) {
42
stream.skip();
43
}
44
}
45
}
46
47
RIFF::RIFF
(std::span<const std::byte> riffData, uint32_t type_)
48
:
RIFF
{std::vector<std::byte>{riffData.begin(), riffData.end()}, type_} {}
49
50
RIFF::RIFF
(
const
std::filesystem::path& riffPath, uint32_t type_)
51
:
RIFF
{
fs
::readFileBuffer(riffPath), type_} {}
52
53
RIFF::operator bool()
const
{
54
return
this->
signature
;
55
}
56
57
uint32_t
RIFF::getSignature
()
const
{
58
return
this->
signature
;
59
}
60
61
bool
RIFF::isBigEndian
()
const
{
62
return
this->
signature
==
RIFX_SIGNATURE
|| this->
signature
==
FFIR_SIGNATURE
;
63
}
64
65
const
std::vector<std::pair<uint32_t, std::span<std::byte>>>&
RIFF::getChunks
()
const
{
66
return
this->
chunks
;
67
}
68
69
bool
RIFF::hasChunk
(uint32_t chunkType)
const
{
70
return
std::ranges::find_if(this->
chunks
, [chunkType](
const
std::pair<uint32_t, std::span<std::byte>>& chunk) {
71
return
chunk.first == chunkType;
72
}) != this->
chunks
.end();
73
}
74
75
std::vector<std::byte>
RIFF::getFirstChunk
(uint32_t chunkType)
const
{
76
if
(
const
auto
it = std::ranges::find_if(this->
chunks
, [chunkType](
const
std::pair<uint32_t, std::span<std::byte>>& chunk) {
77
return
chunk.first == chunkType;
78
}); it != this->
chunks
.end()) {
79
return
{it->second.begin(), it->second.end()};
80
}
81
return
{};
82
}
83
84
bool
RIFF::hasNthChunk
(uint32_t chunkType, uint32_t n)
const
{
85
uint32_t i = 0;
86
return
std::ranges::find_if(this->
chunks
, [chunkType, n, &i](
const
std::pair<uint32_t, std::span<std::byte>>& chunk) {
87
return
chunk.first == chunkType && i++ == n;
88
}) != this->
chunks
.end();
89
}
90
91
std::vector<std::byte>
RIFF::getNthChunk
(uint32_t chunkType, uint32_t n)
const
{
92
uint32_t i = 0;
93
if
(
const
auto
it = std::ranges::find_if(this->
chunks
, [chunkType, n, &i](
const
std::pair<uint32_t, std::span<std::byte>>& chunk) {
94
return
chunk.first == chunkType && i++ == n;
95
}); it != this->
chunks
.end()) {
96
return
{it->second.begin(), it->second.end()};
97
}
98
return
{};
99
}
FS.h
RIFF.h
sndpp::RIFF::getFirstChunk
std::vector< std::byte > getFirstChunk(uint32_t chunkType) const
Definition
RIFF.cpp:75
sndpp::RIFF::data
std::vector< std::byte > data
Definition
RIFF.h:47
sndpp::RIFF::getChunks
const std::vector< std::pair< uint32_t, std::span< std::byte > > > & getChunks() const
Definition
RIFF.cpp:65
sndpp::RIFF::getNthChunk
std::vector< std::byte > getNthChunk(uint32_t chunkType, uint32_t n) const
Definition
RIFF.cpp:91
sndpp::RIFF::signature
uint32_t signature
Definition
RIFF.h:43
sndpp::RIFF::type
uint32_t type
Definition
RIFF.h:44
sndpp::RIFF::hasChunk
bool hasChunk(uint32_t chunkType) const
Definition
RIFF.cpp:69
sndpp::RIFF::getSignature
uint32_t getSignature() const
Definition
RIFF.cpp:57
sndpp::RIFF::RIFF
RIFF(uint32_t type, uint32_t signature=RIFF_SIGNATURE)
Definition
RIFF.cpp:11
sndpp::RIFF::chunks
std::vector< std::pair< uint32_t, std::span< std::byte > > > chunks
Definition
RIFF.h:45
sndpp::RIFF::isBigEndian
bool isBigEndian() const
Definition
RIFF.cpp:61
sndpp::RIFF::hasNthChunk
bool hasNthChunk(uint32_t chunkType, uint32_t n) const
Definition
RIFF.cpp:84
sndpp
Definition
RIFF.h:10
sndpp::FFIR_SIGNATURE
constexpr auto FFIR_SIGNATURE
Definition
RIFF.h:14
sndpp::RIFX_SIGNATURE
constexpr auto RIFX_SIGNATURE
Definition
RIFF.h:13
sndpp::RIFF_SIGNATURE
constexpr auto RIFF_SIGNATURE
Definition
RIFF.h:12
sourcepp::fs
Definition
FS.h:9
sourcepp
Definition
LZMA.h:11
src
sndpp
RIFF.cpp
Generated on
for SourcePP by
1.17.0