From 71c9dd7209370d2b4330635f028777d24d05994d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Mon, 3 Dec 2018 18:22:33 -0500 Subject: [PATCH] reduce process_tags into a simpler function It had too many responsibilities. --- src/cli.cc | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/cli.cc b/src/cli.cc index f2c2b7b..0139112 100644 --- a/src/cli.cc +++ b/src/cli.cc @@ -160,36 +160,20 @@ std::list ot::read_comments(FILE* input) return comments; } -/** - * 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) +/** Apply the modifications requested by the user to the opustags packet. */ +static ot::status edit_tags(ot::opus_tags& tags, const ot::options& opt) { - ot::opus_tags tags; - ot::status rc = ot::parse_tags(packet, tags); - if (rc != ot::st::ok) - return rc; - - if (opt.delete_all) { - tags.comments.clear(); - } else { - for (const std::string& name : opt.to_delete) - ot::delete_comments(tags, name.c_str()); - } - if (opt.set_all) tags.comments = ot::read_comments(stdin); + else if (opt.delete_all) + tags.comments.clear(); + else for (const std::string& name : opt.to_delete) + ot::delete_comments(tags, name.c_str()); + for (const std::string& comment : opt.to_add) tags.comments.emplace_back(comment); - if (writer) { - auto packet = ot::render_tags(tags); - return writer->write_packet(packet); - } else { - ot::print_comments(tags.comments, stdout); - return ot::st::ok; - } + return ot::st::ok; } /** @@ -230,13 +214,19 @@ static ot::status process(ot::ogg_reader& reader, ot::ogg_writer* writer, const if (rc != ot::st::ok) return rc; } else if (packet_count == 2) { // Comment header - rc = process_tags(reader.packet, opt, writer); - if (rc != ot::st::ok) + ot::opus_tags tags; + if ((rc = ot::parse_tags(reader.packet, tags)) != ot::st::ok) return rc; - if (!writer) - return ot::st::ok; /* nothing else to do */ - else - continue; /* process_tags wrote the new packet */ + if ((rc = edit_tags(tags, opt)) != ot::st::ok) + return rc; + if (writer) { + auto packet = ot::render_tags(tags); + if ((rc = writer->write_packet(packet)) != ot::st::ok) + return rc; + continue; + } else { + ot::print_comments(tags.comments, stdout); + } } if (writer && (rc = writer->write_packet(reader.packet)) != ot::st::ok) return rc;