File: dump_text_file.cxx

package info (click to toggle)
mpd 0.24.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,760 kB
  • sloc: cpp: 75,041; python: 1,408; xml: 628; perl: 469; java: 289; sh: 286; ansic: 235; makefile: 105
file content (83 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "config.h"
#include "ConfigGlue.hxx"
#include "event/Thread.hxx"
#include "input/Init.hxx"
#include "input/InputStream.hxx"
#include "input/TextInputStream.hxx"
#include "util/PrintException.hxx"

#ifdef ENABLE_ARCHIVE
#include "archive/ArchiveList.hxx"
#endif

#include <stdexcept>

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

class GlobalInit {
	const ConfigData config;

	EventThread io_thread;

#ifdef ENABLE_ARCHIVE
	const ScopeArchivePluginsInit archive_plugins_init{config};
#endif

	const ScopeInputPluginsInit input_plugins_init{config, io_thread.GetEventLoop()};

public:
	explicit GlobalInit(Path config_path)
		:config(AutoLoadConfigFile(config_path))
	{
		io_thread.Start();
	}
};

static void
dump_text_file(TextInputStream &is)
{
	const char *line;
	while ((line = is.ReadLine()) != nullptr)
		printf("'%s'\n", line);
}

static int
dump_input_stream(InputStreamPtr &&is)
{
	{
		TextInputStream tis(std::move(is));
		dump_text_file(tis);
	}

	const std::scoped_lock protect{is->mutex};

	is->Check();
	return 0;
}

int main(int argc, char **argv)
try {
	if (argc != 2) {
		fprintf(stderr, "Usage: dump_text_file URI\n");
		return EXIT_FAILURE;
	}

	/* initialize MPD */

	const GlobalInit init{nullptr};

	/* open the stream and dump it */

	Mutex mutex;

	auto is = InputStream::OpenReady(argv[1], mutex);
	return dump_input_stream(std::move(is));
} catch (...) {
	PrintException(std::current_exception());
	return EXIT_FAILURE;
}