From 8a5b80e075b9954101c4a3b1346dbed01190e568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Wed, 14 Nov 2018 18:51:04 -0500 Subject: [PATCH] process_tags function in the main module --- src/error.cc | 1 + src/opustags.cc | 65 ++++++++++++++++++++++++++++++------------------- src/opustags.h | 1 + 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/error.cc b/src/error.cc index 17abbcc..436fb77 100644 --- a/src/error.cc +++ b/src/error.cc @@ -9,6 +9,7 @@ static const char* messages[] = { "End of file", "libogg error", "Invalid identification header, not an Opus stream", + "Invalid comment header", "Bad magic number", "Overflowing magic number", "Overflowing vendor length", diff --git a/src/opustags.cc b/src/opustags.cc index 02fa52b..ca7fa8a 100644 --- a/src/opustags.cc +++ b/src/opustags.cc @@ -25,6 +25,39 @@ static bool same_file(const std::string& path_in, const std::string& path_out) return false; } +/** + * Parse the packet as an OpusTags comment header, apply the user's modifications, and write the new + * packet to the writer. + */ +static ot::status process_tags(const ogg_packet& packet, const ot::options& opt, ot::ogg_writer& writer) +{ + ot::opus_tags tags; + if(ot::parse_tags((char*) packet.packet, packet.bytes, &tags) != ot::status::ok) + return ot::status::bad_comment_header; + + if (opt.delete_all) { + tags.comments.clear(); + } else { + for (const std::string& name : opt.to_delete) + ot::delete_tags(&tags, name.c_str()); + } + + if (opt.set_all) + tags.comments = ot::read_comments(stdin); + for (const std::string& comment : opt.to_add) + tags.comments.emplace_back(comment); + + if (writer.file) { + auto packet = ot::render_tags(tags); + if(ogg_stream_packetin(&writer.stream, &packet) == -1) + return ot::status::libogg_error; + } else { + ot::print_comments(tags.comments, stdout); + } + + return ot::status::ok; +} + static int run(ot::options& opt) { if (!opt.path_out.empty() && same_file(opt.path_in, opt.path_out)) { @@ -64,7 +97,6 @@ static int run(ot::options& opt) } } } - ot::opus_tags tags; const char *error = NULL; int packet_count = -1; while(error == NULL){ @@ -114,33 +146,16 @@ static int run(ot::options& opt) error = ot::error_message(rc); break; } - } - else if(packet_count == 2){ // Comment header - if(ot::parse_tags((char*) reader.packet.packet, reader.packet.bytes, &tags) != ot::status::ok){ - error = "opustags: invalid comment header"; + } else if (packet_count == 2) { // Comment header + rc = process_tags(reader.packet, opt, writer); + if (rc != ot::status::ok) { + error = ot::error_message(rc); break; } - if (opt.delete_all) { - tags.comments.clear(); - } else { - for (const std::string& name : opt.to_delete) - ot::delete_tags(&tags, name.c_str()); - } - if (opt.set_all) - tags.comments = ot::read_comments(stdin); - for (const std::string& comment : opt.to_add) - tags.comments.emplace_back(comment); - if(writer.file){ - auto packet = ot::render_tags(tags); - if(ogg_stream_packetin(&writer.stream, &packet) == -1) - error = "ogg_stream_packetin: internal error"; - } + if (!writer.file) + break; /* nothing else to do */ else - ot::print_comments(tags.comments, stdout); - if(error || !writer.file) - break; - else - continue; + continue; /* process_tags wrote the new packet */ } if(writer.file){ if(ogg_stream_packetin(&writer.stream, &reader.packet) == -1){ diff --git a/src/opustags.h b/src/opustags.h index 431849e..fabbd77 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -36,6 +36,7 @@ enum class status { end_of_file, libogg_error, bad_identification_header, + bad_comment_header, /* OpusTags parsing errors */ bad_magic_number, overflowing_magic_number,