-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriff.cpp
276 lines (220 loc) · 7.05 KB
/
riff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#ifndef __RIFF_CPP__
#define __RIFF_CPP__
#include "riff.hpp"
namespace RIFF {
#pragma region condes
void * try_calloc(size_t nmemb, size_t size, const char * const objName) {
int cnt = 16;
void * ptr = nullptr;
while (cnt != 0 && ptr == nullptr) {
ptr = calloc(nmemb, size);
}
if (ptr == nullptr) {
fprintf(stderr, "Could not allocate %s\n", objName);
}
return ptr;
}
RIFFFile::RIFFFile() {
rh = riff_handleAllocate();
#if !RIFF_CXX_PRINT_ERRORS
rh->fp_printf = NULL;
#endif
}
// copy assignment
RIFFFile & RIFFFile::operator = (const RIFFFile &rhs) {
if (&rhs == this)
return *this;
// Copy the riff_handle
auto newrh = (riff_handle *)try_calloc(1, sizeof(riff_handle), "riff_handle, aborting copy assignment of RIFFFile");
if (newrh == nullptr) return *this;
memcpy(rh, rhs.rh, sizeof(riff_handle));
if (newrh->ls) {
newrh->ls = (struct riff_levelStackE *)try_calloc(rh->ls_size, sizeof(struct riff_levelStackE), "riff level stack, aborting copy assignment of RIFFFile");
if (newrh->ls == nullptr) return *this;
memcpy(newrh->ls, rhs.rh->ls, rh->ls_size * sizeof(struct riff_levelStackE));
}
if (rh) die();
// Copy the data
memcpy(this, &rhs, sizeof(RIFFFile));
rh = newrh;
return *this;
}
// copy constructor
RIFFFile::RIFFFile(const RIFFFile &rhs) {
// Copy the data
memcpy(this, &rhs, sizeof(RIFFFile));
// Copy the riff_handle
rh = (riff_handle *)try_calloc(1, sizeof(riff_handle), "riff_handle, aborting copy assignment of RIFFFile");
if (rh == nullptr) return;
memcpy(rh, rhs.rh, sizeof(riff_handle));
if (rh->ls) {
rh->ls = (struct riff_levelStackE *)try_calloc(rh->ls_size, sizeof(struct riff_levelStackE), "riff level stack, aborting copy assignment of RIFFFile");
if (rh->ls == nullptr) return;
memcpy(rh->ls, rhs.rh->ls, rh->ls_size * sizeof(struct riff_levelStackE));
}
}
// move assignment
RIFFFile & RIFFFile::operator = (RIFFFile &&rhs) noexcept {
if (&rhs == this)
return *this;
if (rh) die();
memcpy(this, &rhs, sizeof(RIFFFile));
rhs.reset();
return *this;
}
// move constructor
RIFFFile::RIFFFile (RIFFFile &&rhs) noexcept {
memcpy(this, &rhs, sizeof(RIFFFile));
rhs.reset();
}
RIFFFile::~RIFFFile() {
die();
}
void RIFFFile::die() {
riff_handleFree(rh);
close();
}
void RIFFFile::reset() {
// Reset the 4 internal variables
file = nullptr;
rh = nullptr;
type = CLOSED;
__latestError = RIFF_ERROR_NONE;
}
#pragma endregion
#pragma region openCfile
int RIFFFile::openCFILE (const char* __filename, bool __detectSize) {
file = std::fopen(__filename, "rb");
FILE * __file = (FILE *)file;
// Detect file size
size_t __size = 0;
if (__detectSize) {
std::fseek(__file, 0, SEEK_END);
__size = std::ftell(__file);
std::fseek(__file, 0, SEEK_SET);
}
type = C_FILE;
return riff_open_file(rh, (std::FILE *)file, __size);
}
int RIFFFile::openCFILE (std::FILE & __file, size_t __size) {
file = &__file;
type = C_FILE|MANUAL;
return riff_open_file(rh, &__file, __size);
}
#pragma endregion
#pragma region openMem
int RIFFFile::openMemory (const void * __mem_ptr, size_t __size) {
file = nullptr;
type = MEM_PTR;
return riff_open_mem(rh, __mem_ptr, __size);
}
#pragma endregion
#pragma region fstreamHandling
size_t read_fstream(riff_handle *rh, void *ptr, size_t size){
auto stream = ((std::fstream *)rh->fh);
size_t oldg = stream->tellg();
stream->read((char *)ptr, size);
size_t newg = stream->tellg();
return newg-oldg;
}
size_t seek_fstream(riff_handle *rh, size_t pos){
auto stream = ((std::ifstream *)rh->fh);
stream->seekg(pos);
return stream->tellg();
}
int RIFFFile::openFstream(const char * __filename, bool __detectSize) {
// Set type
setAutomaticFstream();
auto & stream = *(std::fstream*)file;
stream.open(__filename, std::ios_base::in|std::ios_base::binary);
return openFstreamCommon(detectFstreamSize(__detectSize));
}
int RIFFFile::openFstream(const std::string & __filename, bool __detectSize) {
// Set type
setAutomaticFstream();
auto & stream = *(std::fstream*)file;
stream.open(__filename, std::ios_base::in|std::ios_base::binary);
return openFstreamCommon(detectFstreamSize(__detectSize));
}
#if RIFF_CXX17_SUPPORT
int RIFFFile::openFstream(const std::filesystem::path & __filename, bool __detectSize) {
// Set type
setAutomaticFstream();
auto & stream = *(std::fstream*)file;
stream.open(__filename, std::ios_base::in|std::ios_base::binary);
return openFstreamCommon(detectFstreamSize(__detectSize));
}
#endif
size_t RIFFFile::detectFstreamSize(bool __detectSize) {
if (!__detectSize) return 0;
auto & stream = *(std::fstream*)file;
stream.seekg(0, std::ios_base::end);
size_t output = stream.tellg();
stream.seekg(0, std::ios_base::beg);
return output;
}
void RIFFFile::setAutomaticFstream(){
type = FSTREAM;
file = new std::fstream;
}
int RIFFFile::openFstream(std::fstream & __file, size_t __size){
type = FSTREAM|MANUAL;
file = &__file;
return openFstreamCommon(__size);
}
int RIFFFile::openFstreamCommon(size_t __size){
auto stream = (std::fstream*)file;
// My own open function lmfao
if(rh == NULL)
return RIFF_ERROR_INVALID_HANDLE;
rh->fh = file;
rh->size = __size;
rh->pos_start = stream->tellg(); //current file offset of stream considered as start of RIFF file
rh->fp_read = &read_fstream;
rh->fp_seek = &seek_fstream;
return riff_readHeader(rh);
}
#pragma endregion
void RIFFFile::close () {
if (!(type & MANUAL)) { // Must be automatically allocated to close
if (type == C_FILE) {
std::fclose((std::FILE *)file);
free (file);
} else if (type == FSTREAM) {
((std::fstream *)file)->close();
free (file);
}
}
type = CLOSED;
}
std::string RIFFFile::latestErrorToString () {
#define posStrSize 1+2+1+3+1+2+(2*sizeof(size_t))+1
if (__latestError == RIFF_ERROR_NONE) return "";
std::string outString(riff_errorToString(__latestError));
char buffer[posStrSize];
std::snprintf(buffer, posStrSize, " at pos 0x%zX", rh->pos);
std::string posString (buffer);
outString += posString;
return outString;
#undef posStrSize
}
std::vector<uint8_t> RIFFFile::readChunkData() {
__latestError = seekChunkStart();
if (__latestError || rh->c_size == 0) {
return std::vector<uint8_t>(0);
}
auto outVec = std::vector<uint8_t>(rh->c_size);
size_t totalSize = 0, succSize;
do {
succSize = readInChunk(outVec.data()+totalSize, rh->c_size);
totalSize += succSize;
} while (succSize != 0);
#if RIFF_CXX_PRINT_ERRORS
if (totalSize != rh->c_size && rh->fp_printf) {
rh->fp_printf("Couldn't read the entire chunk for some reason. Successfully read %zu bytes out of %zu\n", totalSize, rh->c_size);
}
#endif
return outVec;
}
} // namespace RIFF
#endif // __RIFF_CPP__