lib/xiph/{FlacStreamMetadata,VorbisComments}: merge redundant code

This commit is contained in:
Max Kellermann 2019-08-14 19:57:12 +02:00
parent 01f86e1c25
commit 545af857ba
5 changed files with 98 additions and 78 deletions

View File

@ -19,14 +19,12 @@
#include "FlacStreamMetadata.hxx"
#include "FlacAudioFormat.hxx"
#include "XiphTags.hxx"
#include "ScanVorbisComment.hxx"
#include "CheckAudioFormat.hxx"
#include "MixRampInfo.hxx"
#include "tag/Handler.hxx"
#include "tag/Table.hxx"
#include "tag/Builder.hxx"
#include "tag/Tag.hxx"
#include "tag/VorbisComment.hxx"
#include "tag/ReplayGain.hxx"
#include "tag/MixRamp.hxx"
#include "ReplayGainInfo.hxx"
@ -68,50 +66,12 @@ flac_parse_mixramp(const FLAC__StreamMetadata_VorbisComment &vc)
return mix_ramp;
}
/**
* Check if the comment's name equals the passed name, and if so, copy
* the comment value into the tag.
*/
static bool
flac_copy_comment(StringView comment,
StringView name, TagType tag_type,
TagHandler &handler) noexcept
{
const auto value = GetVorbisCommentValue(comment, name);
if (!value.IsNull()) {
handler.OnTag(tag_type, value);
return true;
}
return false;
}
static void
flac_scan_comment(StringView comment, TagHandler &handler) noexcept
{
if (handler.WantPair()) {
const auto split = comment.Split('=');
if (!split.first.empty() && !split.second.IsNull())
handler.OnPair(split.first, split.second);
}
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
if (flac_copy_comment(comment, i->name, i->type, handler))
return;
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
if (flac_copy_comment(comment,
tag_item_names[i], (TagType)i,
handler))
return;
}
static void
flac_scan_comments(const FLAC__StreamMetadata_VorbisComment *comment,
TagHandler &handler) noexcept
{
for (unsigned i = 0; i < comment->num_comments; ++i)
flac_scan_comment(ToStringView(comment->comments[i]), handler);
ScanVorbisComment(ToStringView(comment->comments[i]), handler);
}
gcc_pure

View File

@ -0,0 +1,64 @@
/*
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ScanVorbisComment.hxx"
#include "XiphTags.hxx"
#include "tag/Table.hxx"
#include "tag/Handler.hxx"
#include "tag/VorbisComment.hxx"
#include "util/StringView.hxx"
/**
* Check if the comment's name equals the passed name, and if so, copy
* the comment value into the tag.
*/
static bool
vorbis_copy_comment(StringView comment,
StringView name, TagType tag_type,
TagHandler &handler) noexcept
{
const auto value = GetVorbisCommentValue(comment, name);
if (!value.IsNull()) {
handler.OnTag(tag_type, value);
return true;
}
return false;
}
void
ScanVorbisComment(StringView comment, TagHandler &handler) noexcept
{
if (handler.WantPair()) {
const auto split = comment.Split('=');
if (!split.first.empty() && !split.second.IsNull())
handler.OnPair(split.first, split.second);
}
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
if (vorbis_copy_comment(comment, i->name, i->type,
handler))
return;
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
if (vorbis_copy_comment(comment,
tag_item_names[i], TagType(i),
handler))
return;
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_SCAN_VORBIS_COMMENT_HXX
#define MPD_SCAN_VORBIS_COMMENT_HXX
struct StringView;
class TagHandler;
void
ScanVorbisComment(StringView comment, TagHandler &handler) noexcept;
#endif

View File

@ -19,8 +19,7 @@
#include "VorbisComments.hxx"
#include "VorbisPicture.hxx"
#include "XiphTags.hxx"
#include "tag/Table.hxx"
#include "ScanVorbisComment.hxx"
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
#include "tag/Tag.hxx"
@ -64,24 +63,6 @@ VorbisCommentToReplayGain(ReplayGainInfo &rgi,
return found;
}
/**
* Check if the comment's name equals the passed name, and if so, copy
* the comment value into the tag.
*/
static bool
vorbis_copy_comment(StringView comment,
StringView name, TagType tag_type,
TagHandler &handler) noexcept
{
const auto value = GetVorbisCommentValue(comment, name);
if (!value.IsNull()) {
handler.OnTag(tag_type, value);
return true;
}
return false;
}
static void
vorbis_scan_comment(StringView comment, TagHandler &handler) noexcept
{
@ -91,22 +72,7 @@ vorbis_scan_comment(StringView comment, TagHandler &handler) noexcept
if (!picture_b64.IsNull())
return ScanVorbisPicture(picture_b64, handler);
if (handler.WantPair()) {
const auto split = comment.Split('=');
if (!split.first.empty() && !split.second.IsNull())
handler.OnPair(split.first, split.second);
}
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
if (vorbis_copy_comment(comment, i->name, i->type,
handler))
return;
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
if (vorbis_copy_comment(comment,
tag_item_names[i], TagType(i),
handler))
return;
ScanVorbisComment(comment, handler);
}
void

View File

@ -46,6 +46,7 @@ endif
xiph = static_library(
'xiph',
'ScanVorbisComment.cxx',
'VorbisComments.cxx',
'VorbisPicture.cxx',
'XiphTags.cxx',