dump_text_file.cxx 2.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "config.h"
21
#include "event/Thread.hxx"
Max Kellermann's avatar
Max Kellermann committed
22 23 24
#include "input/Init.hxx"
#include "input/InputStream.hxx"
#include "input/TextInputStream.hxx"
25
#include "config/ConfigGlobal.hxx"
26
#include "thread/Cond.hxx"
27
#include "Log.hxx"
28

29
#ifdef ENABLE_ARCHIVE
30
#include "archive/ArchiveList.hxx"
31 32
#endif

33 34
#include <stdexcept>

35 36 37 38
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

39
class GlobalInit {
40
	EventThread io_thread;
41 42 43

public:
	GlobalInit() {
44
		io_thread.Start();
45 46 47 48
		config_global_init();
#ifdef ENABLE_ARCHIVE
		archive_plugin_init_all();
#endif
49
		input_stream_global_init(io_thread.GetEventLoop());
50 51 52 53 54 55 56 57 58 59 60
	}

	~GlobalInit() {
		input_stream_global_finish();
#ifdef ENABLE_ARCHIVE
		archive_plugin_deinit_all();
#endif
		config_global_finish();
	}
};

61
static void
62
dump_text_file(TextInputStream &is)
63
{
64 65 66
	const char *line;
	while ((line = is.ReadLine()) != nullptr)
		printf("'%s'\n", line);
67 68 69
}

static int
70
dump_input_stream(InputStreamPtr &&is)
71
{
72
	{
73
		TextInputStream tis(std::move(is));
74 75
		dump_text_file(tis);
	}
76

77
	const std::lock_guard<Mutex> protect(is->mutex);
78

79
	is->Check();
80 81 82 83
	return 0;
}

int main(int argc, char **argv)
84
try {
85
	if (argc != 2) {
86 87
		fprintf(stderr, "Usage: run_input URI\n");
		return EXIT_FAILURE;
88 89 90 91
	}

	/* initialize MPD */

92
	const GlobalInit init;
93 94 95

	/* open the stream and dump it */

96 97
	Mutex mutex;
	Cond cond;
98

99 100
	auto is = InputStream::OpenReady(argv[1], mutex, cond);
	return dump_input_stream(std::move(is));
101 102 103
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
104
}