dump_playlist.cxx 3.55 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "TagSave.hxx"
22
#include "DetachedSong.hxx"
23
#include "playlist/SongEnumerator.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "input/InputStream.hxx"
25
#include "config/ConfigGlobal.hxx"
26
#include "decoder/DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "input/Init.hxx"
28
#include "ScopeIOThread.hxx"
29 30
#include "playlist/PlaylistRegistry.hxx"
#include "playlist/PlaylistPlugin.hxx"
31
#include "fs/Path.hxx"
32 33
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
34
#include "util/Error.hxx"
35
#include "thread/Cond.hxx"
36
#include "Log.hxx"
37

38
#ifdef HAVE_GLIB
39
#include <glib.h>
40
#endif
41 42

#include <unistd.h>
43
#include <stdlib.h>
44

45 46 47 48 49 50 51 52 53
static void
tag_save(FILE *file, const Tag &tag)
{
	StdioOutputStream sos(file);
	BufferedOutputStream bos(sos);
	tag_save(bos, tag);
	bos.Flush();
}

54 55 56
int main(int argc, char **argv)
{
	const char *uri;
57
	InputStream *is = NULL;
58

59
	if (argc != 3) {
60 61
		fprintf(stderr, "Usage: dump_playlist CONFIG URI\n");
		return EXIT_FAILURE;
62 63
	}

64
	const Path config_path = Path::FromFS(argv[1]);
65
	uri = argv[2];
66 67 68

	/* initialize GLib */

69
#ifdef HAVE_GLIB
70
#if !GLIB_CHECK_VERSION(2,32,0)
71
	g_thread_init(NULL);
72
#endif
73 74
#endif

75 76 77
	/* initialize MPD */

	config_global_init();
78 79 80

	Error error;
	if (!ReadConfigFile(config_path, error)) {
81 82
		LogError(error);
		return EXIT_FAILURE;
83 84
	}

85
	const ScopeIOThread io_thread;
86

87
	if (!input_stream_global_init(error)) {
88
		LogError(error);
89
		return EXIT_FAILURE;
90 91
	}

92
	playlist_list_global_init();
93
	decoder_plugin_init_all();
94

95
	/* open the playlist */
96

97 98
	Mutex mutex;
	Cond cond;
99

100
	auto playlist = playlist_list_open_uri(uri, mutex, cond);
101 102
	if (playlist == NULL) {
		/* open the stream and wait until it becomes ready */
103

104
		is = InputStream::OpenReady(uri, mutex, cond, error);
105
		if (is == NULL) {
106
			if (error.IsDefined())
107
				LogError(error);
108
			else
109
				fprintf(stderr,
110
					"InputStream::Open() failed\n");
111
			return 2;
112
		}
113

114 115
		/* open the playlist */

116
		playlist = playlist_list_open_stream(*is, uri);
117
		if (playlist == NULL) {
118
			delete is;
119
			fprintf(stderr, "Failed to open playlist\n");
120 121
			return 2;
		}
122 123 124 125
	}

	/* dump the playlist */

126
	DetachedSong *song;
127
	while ((song = playlist->NextSong()) != NULL) {
128 129
		printf("%s\n", song->GetURI());

130 131
		const unsigned start_ms = song->GetStartTime().ToMS();
		const unsigned end_ms = song->GetEndTime().ToMS();
132

133
		if (end_ms > 0)
134
			printf("range: %u:%02u..%u:%02u\n",
135 136 137 138 139
			       start_ms / 60000,
			       (start_ms / 1000) % 60,
			       end_ms / 60000,
			       (end_ms / 1000) % 60);
		else if (start_ms > 0)
140
			printf("range: %u:%02u..\n",
141 142
			       start_ms / 60000,
			       (start_ms / 1000) % 60);
143

144
		tag_save(stdout, song->GetTag());
145

146
		delete song;
147 148 149 150
	}

	/* deinitialize everything */

151
	delete playlist;
152
	delete is;
153

154
	decoder_plugin_deinit_all();
155 156 157 158 159 160
	playlist_list_global_finish();
	input_stream_global_finish();
	config_global_finish();

	return 0;
}