dump_text_file.cxx 2.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "IOThread.hxx"
22
#include "InputInit.hxx"
23
#include "InputStream.hxx"
24
#include "ConfigGlobal.hxx"
25
#include "stdbin.h"
26
#include "TextInputStream.hxx"
27
#include "util/Error.hxx"
28
#include "thread/Cond.hxx"
29
#include "Log.hxx"
30

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

#include <glib.h>

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

static void
42
dump_text_file(TextInputStream &is)
43
{
44 45 46
	std::string line;
	while (is.ReadLine(line))
		printf("'%s'\n", line.c_str());
47 48 49
}

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

57
	is.Lock();
58

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

66
	is.Unlock();
67 68 69 70 71 72 73 74 75

	return 0;
}

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

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

	/* initialize GLib */

82
#if !GLIB_CHECK_VERSION(2,32,0)
83
	g_thread_init(NULL);
84 85
#endif

86 87 88 89 90
	/* initialize MPD */

	config_global_init();

	io_thread_init();
91
	io_thread_start();
92 93 94 95 96

#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

97 98
	Error error;
	if (!input_stream_global_init(error)) {
99
		LogError(error);
100 101 102 103 104
		return 2;
	}

	/* open the stream and dump it */

105 106
	Mutex mutex;
	Cond cond;
107

108
	InputStream *is = InputStream::OpenReady(argv[1], mutex, cond, error);
109
	if (is != NULL) {
110
		ret = dump_input_stream(*is);
111
		is->Close();
112
	} else {
113
		if (error.IsDefined())
114
			LogError(error);
115
		else
116 117
			fprintf(stderr, "input_stream::Open() failed\n");
		ret = EXIT_FAILURE;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	}

	/* deinitialize everything */

	input_stream_global_finish();

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

	io_thread_deinit();

	config_global_finish();

	return ret;
}