1// Settings.h
2//
3// Copyright (c) 2003, Ingo Weinhold (bonefish@cs.tu-berlin.de)
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18//
19// You can alternatively use *this file* under the terms of the the MIT
20// license included in this package.
21
22#ifndef SETTINGS_H
23#define SETTINGS_H
24
25#include <driver_settings.h>
26
27#include "List.h"
28#include "String.h"
29
30class Settings {
31public:
32	Settings();
33	~Settings();
34
35	status_t SetTo(const char *volumeName = NULL);
36	status_t SetTo(off_t volumeOffset, off_t volumeSize);
37	void Unset();
38
39	const char *GetDefaultVolumeName() const;
40	const char *GetVolumeName() const;
41	bool GetHideEsoteric() const;
42	const char *HiddenEntryAt(int32 index) const;
43
44	void Dump();
45
46private:
47	status_t _Init(const driver_settings *settings,
48				   const driver_parameter *volume);
49	static const driver_parameter *_FindVolumeParameter(
50		const driver_settings *settings, const char *name);
51	static const driver_parameter *_FindVolumeParameter(
52		const driver_settings *settings, off_t offset, off_t size);
53
54	template<typename container_t>
55	static const driver_parameter *_FindNextParameter(
56		const container_t *container, const char *name, int32 &cookie);
57
58	template<typename container_t>
59	static const char *_GetParameterValue(const container_t *container,
60		const char *name, const char *unknownValue, const char *noArgValue);
61
62	template<typename container_t>
63	static bool _GetParameterValue(const container_t *container,
64		const char *name, bool unknownValue, bool noArgValue);
65
66	template<typename container_t>
67	static int64 _GetParameterValue(const container_t *container,
68		const char *name, int64 unknownValue, int64 noArgValue);
69
70	static void _CheckVolumeName(String &name);
71	static bool _CheckEntryName(const char *name);
72
73private:
74	String			fDefaultVolumeName;
75	String			fVolumeName;
76	bool			fHideEsoteric;
77	List<String>	fHiddenEntries;
78};
79
80// _FindNextParameter
81template<typename container_t>
82const driver_parameter *
83Settings::_FindNextParameter(const container_t *container,
84							 const char *name, int32 &cookie)
85{
86	const driver_parameter *parameter = NULL;
87	if (container) {
88		for (; !parameter && cookie < container->parameter_count; cookie++) {
89			const driver_parameter &param = container->parameters[cookie];
90			if (!strcmp(param.name, name))
91				parameter = &param;
92		}
93	}
94	return parameter;
95}
96
97// _GetParameterValue
98template<typename container_t>
99const char *
100Settings::_GetParameterValue(const container_t *container, const char *name,
101							 const char *unknownValue, const char *noArgValue)
102{
103	if (container) {
104		for (int32 i = container->parameter_count - 1; i >= 0; i--) {
105			const driver_parameter &param = container->parameters[i];
106			if (!strcmp(param.name, name)) {
107				if (param.value_count > 0)
108					return param.values[0];
109				return noArgValue;
110			}
111		}
112	}
113	return unknownValue;
114}
115
116// contains
117static inline
118bool
119contains(const char **array, size_t size, const char *value)
120{
121	for (int32 i = 0; i < (int32)size; i++) {
122		if (!strcmp(array[i], value))
123			return true;
124	}
125	return false;
126}
127
128// _GetParameterValue
129template<typename container_t>
130bool
131Settings::_GetParameterValue(const container_t *container, const char *name,
132							 bool unknownValue, bool noArgValue)
133{
134	// note: container may be NULL
135	const char unknown = 0;
136	const char noArg = 0;
137	const char *value = _GetParameterValue(container, name, &unknown, &noArg);
138	if (value == &unknown)
139		return unknownValue;
140	if (value == &noArg)
141		return noArgValue;
142	const char *trueStrings[]
143		= { "1", "true", "yes", "on", "enable", "enabled" };
144	const char *falseStrings[]
145		= { "0", "false", "no", "off", "disable", "disabled" };
146	if (contains(trueStrings, sizeof(trueStrings) / sizeof(const char*),
147				 value)) {
148		return true;
149	}
150	if (contains(falseStrings, sizeof(falseStrings) / sizeof(const char*),
151				 value)) {
152		return false;
153	}
154	return unknownValue;
155}
156
157// _GetParameterValue
158template<typename container_t>
159int64
160Settings::_GetParameterValue(const container_t *container, const char *name,
161							 int64 unknownValue, int64 noArgValue)
162{
163	// note: container may be NULL
164	const char unknown = 0;
165	const char noArg = 0;
166	const char *value = _GetParameterValue(container, name, &unknown, &noArg);
167	if (value == &unknown)
168		return unknownValue;
169	if (value == &noArg)
170		return noArgValue;
171	return atoll(value);
172}
173
174#endif	// SETTINGS_H
175