mpd/test/ContainerScan.cxx

79 lines
1.8 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2016-11-22 16:03:10 +01:00
#include "song/DetachedSong.hxx"
#include "SongSave.hxx"
2018-07-17 21:31:06 +02:00
#include "config/Data.hxx"
2016-11-22 16:03:10 +01:00
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
#include "fs/Path.hxx"
#include "fs/NarrowPath.hxx"
2021-12-03 14:02:07 +01:00
#include "io/StdioOutputStream.hxx"
#include "io/BufferedOutputStream.hxx"
2018-07-17 21:56:43 +02:00
#include "util/PrintException.hxx"
2019-08-09 15:54:13 +02:00
#include "util/UriExtract.hxx"
2016-11-22 16:03:10 +01:00
#include <cassert>
2016-11-22 16:03:10 +01:00
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
static const DecoderPlugin *
FindContainerDecoderPlugin(std::string_view suffix)
2016-11-22 16:03:10 +01:00
{
return decoder_plugins_find([suffix](const DecoderPlugin &plugin){
return plugin.container_scan != nullptr &&
plugin.SupportsSuffix(suffix);
});
}
static const DecoderPlugin *
FindContainerDecoderPlugin(Path path)
{
const auto path_utf8 = path.ToUTF8Throw();
const auto suffix = uri_get_suffix(path_utf8);
if (suffix.empty())
2016-11-22 16:03:10 +01:00
return nullptr;
return FindContainerDecoderPlugin(suffix);
}
int main(int argc, char **argv)
try {
if (argc != 2) {
fprintf(stderr, "Usage: ContainerScan PATH\n");
return EXIT_FAILURE;
}
const FromNarrowPath path = argv[1];
2016-11-22 16:03:10 +01:00
2019-02-05 22:11:45 +01:00
const ScopeDecoderPluginsInit decoder_plugins_init({});
2016-11-22 16:03:10 +01:00
const auto *plugin = FindContainerDecoderPlugin(path);
if (plugin == nullptr) {
fprintf(stderr, "No decoder found for this file\n");
return EXIT_FAILURE;
}
const auto v = plugin->container_scan(path);
if (v.empty()) {
fprintf(stderr, "File is not a container\n");
return EXIT_FAILURE;
}
StdioOutputStream sos(stdout);
BufferedOutputStream bos(sos);
for (const auto &song : v)
song_save(bos, song);
2016-11-22 16:03:10 +01:00
bos.Flush();
return EXIT_SUCCESS;
2018-07-17 21:56:43 +02:00
} catch (...) {
PrintException(std::current_exception());
2016-11-22 16:03:10 +01:00
return EXIT_FAILURE;
}