summaryrefslogtreecommitdiff
path: root/src/lib/alsa/AllowedFormat.cxx
blob: b346146c093e559491f6fb95c3493880ce01f24d (plain)
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
84
85
86
87
88
89
/*
 * Copyright 2003-2020 The Music Player Daemon Project
 * 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 "AllowedFormat.hxx"
#include "pcm/AudioParser.hxx"
#include "util/IterableSplitString.hxx"
#include "util/StringBuffer.hxx"
#include "config.h"

#include <stdexcept>

namespace Alsa {

AllowedFormat::AllowedFormat(StringView s)
{
#ifdef ENABLE_DSD
	const StringView dop_tail("=dop");
	if (s.EndsWith(dop_tail)) {
		dop = true;
		s.size -= dop_tail.size;
	} else
		dop = false;
#endif

	char buffer[64];
	if (s.size >= sizeof(buffer))
		throw std::invalid_argument("Failed to parse audio format");

	memcpy(buffer, s.data, s.size);
	buffer[s.size] = 0;

	format = ParseAudioFormat(buffer, true);

#ifdef ENABLE_DSD
	if (dop && format.format != SampleFormat::DSD)
		throw std::invalid_argument("DoP works only with DSD");
#endif
}

std::forward_list<AllowedFormat>
AllowedFormat::ParseList(StringView s)
{
	std::forward_list<AllowedFormat> list;
	auto tail = list.before_begin();

	for (const auto i : IterableSplitString(s, ' '))
		if (!i.empty())
			tail = list.emplace_after(tail, i);

	return list;
}

std::string
ToString(const std::forward_list<AllowedFormat> &allowed_formats) noexcept
{
	std::string result;

	for (const auto &i : allowed_formats) {
		if (!result.empty())
			result.push_back(' ');

		result += ::ToString(i.format);

#ifdef ENABLE_DSD
		if (i.dop)
			result += "=dop";
#endif
	}

	return result;
}

} // namespace Alsa