dump_text_file.cxx 2.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "ScopeIOThread.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 "util/Error.hxx"
27
#include "thread/Cond.hxx"
28
#include "Log.hxx"
29

30
#ifdef ENABLE_ARCHIVE
31
#include "archive/ArchiveList.hxx"
32 33 34 35 36 37 38
#endif

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

static void
39
dump_text_file(TextInputStream &is)
40
{
41 42 43
	const char *line;
	while ((line = is.ReadLine()) != nullptr)
		printf("'%s'\n", line);
44 45 46
}

static int
47
dump_input_stream(InputStreamPtr &&is)
48
{
49
	{
50
		TextInputStream tis(std::move(is));
51 52
		dump_text_file(tis);
	}
53

54
	const ScopeLock protect(is->mutex);
55

56
	Error error;
57
	if (!is->Check(error)) {
58
		LogError(error);
59 60 61 62 63 64 65 66 67 68 69
		return EXIT_FAILURE;
	}

	return 0;
}

int main(int argc, char **argv)
{
	int ret;

	if (argc != 2) {
70 71
		fprintf(stderr, "Usage: run_input URI\n");
		return EXIT_FAILURE;
72 73 74 75 76 77
	}

	/* initialize MPD */

	config_global_init();

78
	const ScopeIOThread io_thread;
79 80 81 82 83

#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

84 85
	Error error;
	if (!input_stream_global_init(error)) {
86
		LogError(error);
87 88 89 90 91
		return 2;
	}

	/* open the stream and dump it */

92 93 94 95 96 97
	{
		Mutex mutex;
		Cond cond;

		auto is = InputStream::OpenReady(argv[1], mutex, cond, error);
		if (is) {
98
			ret = dump_input_stream(std::move(is));
99 100 101 102 103 104 105
		} else {
			if (error.IsDefined())
				LogError(error);
			else
				fprintf(stderr, "input_stream::Open() failed\n");
			ret = EXIT_FAILURE;
		}
106 107 108 109 110 111 112 113 114 115 116 117 118 119
	}

	/* deinitialize everything */

	input_stream_global_finish();

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

	config_global_finish();

	return ret;
}