dump_text_file.cxx 2.41 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "stdbin.h"
27
#include "util/Error.hxx"
28
#include "thread/Cond.hxx"
29
#include "Log.hxx"
30

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

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

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

static int
48
dump_input_stream(InputStream &is)
49
{
50 51 52 53
	{
		TextInputStream tis(is);
		dump_text_file(tis);
	}
54

55
	is.Lock();
56

57
	Error error;
58
	if (!is.Check(error)) {
59
		LogError(error);
60
		is.Unlock();
61 62 63
		return EXIT_FAILURE;
	}

64
	is.Unlock();
65 66 67 68 69 70 71 72 73

	return 0;
}

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

	if (argc != 2) {
74 75
		fprintf(stderr, "Usage: run_input URI\n");
		return EXIT_FAILURE;
76 77 78 79 80 81
	}

	/* initialize MPD */

	config_global_init();

82
	const ScopeIOThread io_thread;
83 84 85 86 87

#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

88 89
	Error error;
	if (!input_stream_global_init(error)) {
90
		LogError(error);
91 92 93 94 95
		return 2;
	}

	/* open the stream and dump it */

96 97
	Mutex mutex;
	Cond cond;
98

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

	/* deinitialize everything */

	input_stream_global_finish();

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

	config_global_finish();

	return ret;
}