mirror of
https://gitlab.com/mbunkus/mkvtoolnix.git
synced 2025-01-04 01:03:33 +00:00
Exit with an error if an attachment ID was not found and don't write anything
This commit is contained in:
parent
94c383fa6b
commit
962bfd23b5
@ -33,24 +33,35 @@
|
|||||||
|
|
||||||
using namespace libmatroska;
|
using namespace libmatroska;
|
||||||
|
|
||||||
static void
|
struct attachment_t {
|
||||||
handle_attachments(KaxAttachments *atts,
|
|
||||||
std::vector<track_spec_t> &tracks) {
|
|
||||||
static int64_t attachment_ui_id = 0;
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
for (i = 0; atts->ListSize() > i; ++i) {
|
|
||||||
KaxAttached *att = dynamic_cast<KaxAttached *>((*atts)[i]);
|
|
||||||
assert(NULL != att);
|
|
||||||
|
|
||||||
std::string name, type;
|
std::string name, type;
|
||||||
int64_t size = -1;
|
int64_t size, id;
|
||||||
int64_t id = -1;
|
KaxFileData *fdata;
|
||||||
KaxFileData *fdata = NULL;
|
bool valid;
|
||||||
|
|
||||||
|
attachment_t()
|
||||||
|
: size(-1)
|
||||||
|
, id(-1)
|
||||||
|
, fdata(NULL)
|
||||||
|
, valid(false)
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
attachment_t &parse(KaxAttached &att);
|
||||||
|
static attachment_t parse_new(KaxAttached &att);
|
||||||
|
};
|
||||||
|
|
||||||
|
attachment_t
|
||||||
|
attachment_t::parse_new(KaxAttached &att) {
|
||||||
|
attachment_t attachment;
|
||||||
|
return attachment.parse(att);
|
||||||
|
}
|
||||||
|
|
||||||
|
attachment_t &
|
||||||
|
attachment_t::parse(KaxAttached &att) {
|
||||||
size_t k;
|
size_t k;
|
||||||
for (k = 0; att->ListSize() > k; ++k) {
|
for (k = 0; att.ListSize() > k; ++k) {
|
||||||
EbmlElement *e = (*att)[k];
|
EbmlElement *e = att[k];
|
||||||
|
|
||||||
if (EbmlId(*e) == EBML_ID(KaxFileName))
|
if (EbmlId(*e) == EBML_ID(KaxFileName))
|
||||||
name = UTFstring_to_cstrutf8(UTFstring(*static_cast<KaxFileName *>(e)));
|
name = UTFstring_to_cstrutf8(UTFstring(*static_cast<KaxFileName *>(e)));
|
||||||
@ -65,36 +76,49 @@ handle_attachments(KaxAttachments *atts,
|
|||||||
fdata = (KaxFileData *)e;
|
fdata = (KaxFileData *)e;
|
||||||
size = fdata->GetSize();
|
size = fdata->GetSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((-1 != id) && (-1 != size) && !type.empty()) {
|
valid = (-1 != id) && (-1 != size) && !type.empty();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_attachments(KaxAttachments *atts,
|
||||||
|
std::vector<track_spec_t> &tracks) {
|
||||||
|
int64_t attachment_ui_id = 0;
|
||||||
|
std::map<int64_t, attachment_t> attachments;
|
||||||
|
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; atts->ListSize() > i; ++i) {
|
||||||
|
KaxAttached *att = dynamic_cast<KaxAttached *>((*atts)[i]);
|
||||||
|
assert(NULL != att);
|
||||||
|
|
||||||
|
attachment_t attachment = attachment_t::parse_new(*att);
|
||||||
|
if (!attachment.valid)
|
||||||
|
continue;
|
||||||
|
|
||||||
++attachment_ui_id;
|
++attachment_ui_id;
|
||||||
|
attachments[attachment_ui_id] = attachment;
|
||||||
bool found = false;
|
|
||||||
|
|
||||||
for (k = 0; k < tracks.size(); k++)
|
|
||||||
if (tracks[k].tid == attachment_ui_id) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found && !tracks[k].done) {
|
foreach(track_spec_t &track, tracks) {
|
||||||
|
attachment_t attachment = attachments[ track.tid ];
|
||||||
|
|
||||||
|
if (!attachment.valid)
|
||||||
|
mxerror(boost::format(Y("An attachment with the ID %1% was not found.\n")) % track.tid);
|
||||||
|
|
||||||
// check for output name
|
// check for output name
|
||||||
if (tracks[k].out_name.empty())
|
if (track.out_name.empty())
|
||||||
tracks[k].out_name = name;
|
track.out_name = attachment.name;
|
||||||
|
|
||||||
mxinfo(boost::format(Y("The attachment #%1%, ID %2%, MIME type %3%, size %4%, is written to '%5%'.\n")) % attachment_ui_id % id % type % size % tracks[k].out_name);
|
mxinfo(boost::format(Y("The attachment #%1%, ID %2%, MIME type %3%, size %4%, is written to '%5%'.\n"))
|
||||||
mm_io_c *out = NULL;
|
% attachment_ui_id % attachment.id % attachment.type % attachment.size % track.out_name);
|
||||||
try {
|
try {
|
||||||
out = new mm_file_io_c(tracks[k].out_name, MODE_CREATE);
|
mm_file_io_c out(track.out_name, MODE_CREATE);
|
||||||
|
out.write(attachment.fdata->GetBuffer(), attachment.fdata->GetSize());
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
mxerror(boost::format(Y("The file '%1%' could not be opened for writing (%2%, %3%).\n")) % tracks[k].out_name % errno % strerror(errno));
|
mxerror(boost::format(Y("The file '%1%' could not be opened for writing (%2%, %3%).\n")) % track.out_name % errno % strerror(errno));
|
||||||
}
|
|
||||||
out->write(fdata->GetBuffer(), fdata->GetSize());
|
|
||||||
delete out;
|
|
||||||
tracks[k].done = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,9 +147,4 @@ extract_attachments(const std::string &file_name,
|
|||||||
handle_attachments(attachments, tracks);
|
handle_attachments(attachments, tracks);
|
||||||
delete attachments;
|
delete attachments;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t i;
|
|
||||||
for (i = 0; i < tracks.size(); i++)
|
|
||||||
if (!tracks[i].done)
|
|
||||||
mxinfo(boost::format(Y("An attachment with the ID %1% was not found.\n")) % tracks[i].tid);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user