-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathabstractcontainer.cpp
552 lines (508 loc) · 15 KB
/
abstractcontainer.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include "./abstractcontainer.h"
#include "./diagnostics.h"
using namespace std;
using namespace CppUtilities;
namespace TagParser {
/// \brief The AbstractContainerPrivate struct contains private fields of the AbstractContainer class.
struct AbstractContainerPrivate {
std::vector<std::string> muxingApps, writingApps;
};
/*!
* \class TagParser::AbstractContainer
* \brief The AbstractContainer class provides an interface and common functionality to parse and make a certain container format.
*/
/*!
* \brief Constructs a new container for the specified file \a stream at the specified \a startOffset.
*/
AbstractContainer::AbstractContainer(iostream &stream, std::uint64_t startOffset)
: m_version(0)
, m_readVersion(0)
, m_doctypeVersion(0)
, m_doctypeReadVersion(0)
, m_timeScale(0)
, m_headerParsed(false)
, m_tagsParsed(false)
, m_tracksParsed(false)
, m_tracksAltered(false)
, m_chaptersParsed(false)
, m_attachmentsParsed(false)
, m_startOffset(startOffset)
, m_stream(&stream)
, m_reader(BinaryReader(m_stream))
, m_writer(BinaryWriter(m_stream))
{
}
/*!
* \brief Destroys the container.
*
* Destroys the reader, the writer and track, tag, chapter and attachment objects as well.
* Does NOT destroy the stream which has been specified when constructing the object.
*/
AbstractContainer::~AbstractContainer()
{
}
/*!
* \brief Parses the header if not parsed yet.
*
* The information will be read from the associated stream. The stream and the start offset
* have been specified when constructing the object.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Failure or a derived class when an parsing error occurs.
*/
void AbstractContainer::parseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
{
if (!isHeaderParsed()) {
removeAllTags();
removeAllTracks();
internalParseHeader(diag, progress);
m_headerParsed = true;
}
}
/*!
* \brief Parses the tag information if not parsed yet.
*
* The header will be parsed before if not parsed yet.
*
* The information will be read from the associated stream. The stream and the start offset
* have been specified when constructing the object.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Failure or a derived class when an parsing error occurs.
*
* \sa parseHeader()
* \sa parseTracks()
* \sa parseAttachments()
* \sa parseChapters()
* \sa tags()
*/
void AbstractContainer::parseTags(Diagnostics &diag, AbortableProgressFeedback &progress)
{
if (!areTagsParsed()) {
parseHeader(diag, progress);
internalParseTags(diag, progress);
m_tagsParsed = true;
}
}
/*!
* \brief Parses the tracks of the file if not parsed yet.
*
* The header will be parsed before if not parsed yet.
*
* The information will be read from the associated stream. The stream and the start offset
* have been specified when constructing the object.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Failure or a derived class when an parsing error occurs.
*
* \sa parseHeader()
* \sa parseTags()
* \sa tracks()
*/
void AbstractContainer::parseTracks(Diagnostics &diag, AbortableProgressFeedback &progress)
{
if (!areTracksParsed()) {
parseHeader(diag, progress);
internalParseTracks(diag, progress);
m_tracksParsed = true;
m_tracksAltered = false;
}
}
/*!
* \brief Parses the chapters of the file if not parsed yet.
*
* The information will be read from the associated stream. The stream and the start offset
* have been specified when constructing the object.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws TagParser::Failure or a derived exception when a parsing
* error occurs.
*/
void AbstractContainer::parseChapters(Diagnostics &diag, AbortableProgressFeedback &progress)
{
if (!areChaptersParsed()) {
parseHeader(diag, progress);
internalParseChapters(diag, progress);
m_chaptersParsed = true;
}
}
/*!
* \brief Parses the attachments of the file if not parsed yet.
*
* The information will be read from the associated stream. The stream and the start offset
* have been specified when constructing the object.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws TagParser::Failure or a derived exception when a parsing
* error occurs.
*/
void AbstractContainer::parseAttachments(Diagnostics &diag, AbortableProgressFeedback &progress)
{
if (!areAttachmentsParsed()) {
parseHeader(diag, progress);
internalParseAttachments(diag, progress);
m_attachmentsParsed = true;
}
}
/*!
* \brief Rewrites the file to apply changed tag information.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws TagParser::Failure or a derived exception when a making
* error occurs.
*/
void AbstractContainer::makeFile(Diagnostics &diag, AbortableProgressFeedback &progress)
{
internalMakeFile(diag, progress);
}
/*!
* \brief Returns whether the implementation supports adding or removing of tracks.
*/
bool AbstractContainer::supportsTrackModifications() const
{
return false;
}
/*!
* \brief Determines the position of the index.
* \returns Returns ElementPosition::BeforeData or ElementPosition::AfterData if the position could
* be determined; otherwise returns ElementPosition::Keep.
* \remarks
* - It might be required to parse tracks before the index position can be determined.
* - Not be applicable for files composed of multiple segments.
* \sa MediaFileInfo::indexPosition()
*/
ElementPosition AbstractContainer::determineIndexPosition(Diagnostics &diag) const
{
CPP_UTILITIES_UNUSED(diag);
return ElementPosition::Keep;
}
/*!
* \brief Internally called to parse the header.
*
* Must be implemented when subclassing to provide this feature.
*
* \throws Throws Failure or a derived class when a parsing error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void AbstractContainer::internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
{
CPP_UTILITIES_UNUSED(diag);
CPP_UTILITIES_UNUSED(progress);
throw NotImplementedException();
}
/*!
* \brief Internally called to parse the tags.
*
* Must be implemented when subclassing to provide this feature.
*
* \throws Throws Failure or a derived class when a parsing error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void AbstractContainer::internalParseTags(Diagnostics &diag, AbortableProgressFeedback &progress)
{
CPP_UTILITIES_UNUSED(diag);
CPP_UTILITIES_UNUSED(progress);
throw NotImplementedException();
}
/*!
* \brief Internally called to parse the tracks.
*
* Must be implemented when subclassing to provide this feature.
*
* \throws Throws Failure or a derived class when a parsing error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void AbstractContainer::internalParseTracks(Diagnostics &diag, AbortableProgressFeedback &progress)
{
CPP_UTILITIES_UNUSED(diag);
CPP_UTILITIES_UNUSED(progress);
throw NotImplementedException();
}
/*!
* \brief Internally called to parse the chapters.
*
* Must be implemented when subclassing to provide this feature.
*
* \throws Throws Failure or a derived class when a parsing error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void AbstractContainer::internalParseChapters(Diagnostics &diag, AbortableProgressFeedback &progress)
{
CPP_UTILITIES_UNUSED(diag);
CPP_UTILITIES_UNUSED(progress);
throw NotImplementedException();
}
/*!
* \brief Internally called to parse the attachments.
*
* Must be implemented when subclassing to provide this feature.
*
* \throws Throws Failure or a derived class when a parsing error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void AbstractContainer::internalParseAttachments(Diagnostics &diag, AbortableProgressFeedback &progress)
{
CPP_UTILITIES_UNUSED(diag);
CPP_UTILITIES_UNUSED(progress);
throw NotImplementedException();
}
/*!
* \brief Internally called to make the file.
*
* Must be implemented when subclassing.
*
* \throws Throws Failure or a derived class when a parsing error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void AbstractContainer::internalMakeFile(Diagnostics &diag, AbortableProgressFeedback &progress)
{
CPP_UTILITIES_UNUSED(diag);
CPP_UTILITIES_UNUSED(progress);
throw NotImplementedException();
}
/*!
* \brief Creates and returns a tag for the specified \a target.
* \remarks
* - If there is already a tag (for the specified \a target) present,
* no new tag will be created. The present tag will be returned instead.
* - If an empty \a target is specified it will be ignored.
* - If targets aren't supported the specified \a target will be ignored.
* - If no tag could be created, nullptr is returned.
* - The container keeps the ownership over the created tag.
*/
Tag *AbstractContainer::createTag(const TagTarget &)
{
return nullptr;
}
/*!
* \brief Returns the tag with the specified \a index.
*
* \a index must be less than tagCount().
*/
Tag *AbstractContainer::tag(size_t index)
{
CPP_UTILITIES_UNUSED(index);
return nullptr;
}
/*!
* \brief Returns the number of tags attached to the container.
*
* This method returns zero if the tags have not been parsed yet.
*/
size_t AbstractContainer::tagCount() const
{
return 0;
}
/*!
* \brief Removes the specified \a tag from the container.
*
* Does nothing if the tag is not attached to the container.
*
* The tags need to be parsed before a removal is possible.
* \sa areTagsParsed()
* \sa parseTags()
*
* \remarks The \a tag is not destroyed. The ownership is transferred to the caller.
*
* \returns Returns whether the \a tag could be removed.
*/
bool AbstractContainer::removeTag(Tag *tag)
{
CPP_UTILITIES_UNUSED(tag);
return false;
}
/*!
* \brief Removes all tags attached to the container.
*
* The tags need to be parsed before they can be removed.
* \sa areTagsParsed()
* \sa parseTags()
*
* \remarks The current tag objects are destroyed. All pointers obtained
* by calling tag() are invalidated.
*/
void AbstractContainer::removeAllTags()
{
}
/*!
* \brief Determines the position of the tags inside the file.
* \returns Returns ElementPosition::BeforeData or ElementPosition::AfterData if the position could
* be determined; otherwise returns ElementPosition::Keep.
* \remarks
* - It might be required to parse tags before the tag position can be determined.
* - Not be applicable for files composed of multiple segments.
* \sa MediaFileInfo::tagPosition()
*/
ElementPosition AbstractContainer::determineTagPosition(Diagnostics &diag) const
{
CPP_UTILITIES_UNUSED(diag);
return ElementPosition::Keep;
}
/*!
* \brief Returns the track with the specified \a index.
*
* \a index must be less than trackCount().
*/
AbstractTrack *AbstractContainer::track(size_t index)
{
CPP_UTILITIES_UNUSED(index);
return nullptr;
}
/*!
* \brief Returns the number of tracks the container holds.
*/
size_t AbstractContainer::trackCount() const
{
return 0;
}
/*!
* \brief Removes the specified \a track to the container.
*
* Removal of tracks might be not supported by the implementation.
* \sa supportsTrackModifications()
*
* The tracks need to be parsed before a removal is possible.
* \sa areTracksParsed()
* \sa parseTracks()
*
* \remarks The \a track is not destroyed. The ownership is transferred to the caller.
*
* \returns Returns whether the \a track could be removed.
*/
bool AbstractContainer::removeTrack(AbstractTrack *track)
{
CPP_UTILITIES_UNUSED(track);
return false;
}
/*!
* \brief Removes all tracks from the container.
*
* Modifying tracks might be not supported by the implementation.
* \sa supportsTrackModifications()
*
* The tracks need to be parsed before they can be removed.
* \sa areTracksParsed()
* \sa parseTracks()
*
* \remarks The current track objects are destroyed. All pointers obtained
* by calling track() are invalidated.
*/
void AbstractContainer::removeAllTracks()
{
}
/*!
* \brief Returns the chapter with the specified \a index.
*
* \a index must be less than chapterCount().
*/
AbstractChapter *AbstractContainer::chapter(size_t index)
{
CPP_UTILITIES_UNUSED(index);
return nullptr;
}
/*!
* \brief Returns the number of chapters the container holds.
*/
size_t AbstractContainer::chapterCount() const
{
return 0;
}
/*!
* \brief Creates and returns a new attachment.
* \returns Returns the created attachment.
* \remarks The container keeps the ownership over the created attachment.
*/
AbstractAttachment *AbstractContainer::createAttachment()
{
return nullptr;
}
/*!
* \brief Returns the attachment with the specified \a index.
*
* \a index must be less than attachmentCount().
*/
AbstractAttachment *AbstractContainer::attachment(size_t index)
{
CPP_UTILITIES_UNUSED(index);
return nullptr;
}
/*!
* \brief Returns the number of attachments the container holds.
*/
size_t AbstractContainer::attachmentCount() const
{
return 0;
}
/*!
* \brief Returns whether the title property is supported.
*/
bool AbstractContainer::supportsTitle() const
{
return false;
}
/*!
* \brief Returns the muxing applications specified as meta-data.
*/
const std::vector<std::string> &AbstractContainer::muxingApplications() const
{
static const auto empty = std::vector<std::string>();
return m_p ? m_p->muxingApps : empty;
}
/*!
* \brief Returns the muxing applications specified as meta-data.
*/
std::vector<std::string> &AbstractContainer::muxingApplications()
{
return p()->muxingApps;
}
/*!
* \brief Returns the writing applications specified as meta-data.
*/
const std::vector<std::string> &AbstractContainer::writingApplications() const
{
static const auto empty = std::vector<std::string>();
return m_p ? m_p->writingApps : empty;
}
/*!
* \brief Returns the writing applications specified as meta-data.
*/
std::vector<std::string> &AbstractContainer::writingApplications()
{
return p()->writingApps;
}
/*!
* \brief Returns the number of segments.
*/
size_t AbstractContainer::segmentCount() const
{
return 1;
}
/*!
* \brief Discards all parsing results.
*/
void AbstractContainer::reset()
{
m_headerParsed = false;
m_tagsParsed = false;
m_tracksParsed = false;
m_tracksAltered = false;
m_chaptersParsed = false;
m_attachmentsParsed = false;
m_version = 0;
m_readVersion = 0;
m_doctypeVersion = 0;
m_doctypeReadVersion = 0;
m_timeScale = 0;
m_titles.clear();
}
/*!
* \brief Returns the private data for the container.
*/
std::unique_ptr<AbstractContainerPrivate> &AbstractContainer::p()
{
if (!m_p) {
m_p = std::make_unique<AbstractContainerPrivate>();
}
return m_p;
}
} // namespace TagParser