Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
fdbec694
Commit
fdbec694
authored
May 29, 2019
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config/Param: add method With()
parent
b86d8d0c
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
87 additions
and
82 deletions
+87
-82
LogInit.cxx
src/LogInit.cxx
+8
-6
Main.cxx
src/Main.cxx
+28
-34
Permission.cxx
src/Permission.cxx
+20
-25
ReplayGainGlobal.cxx
src/ReplayGainGlobal.cxx
+10
-17
Data.hxx
src/config/Data.hxx
+8
-0
Param.hxx
src/config/Param.hxx
+13
-0
No files found.
src/LogInit.cxx
View file @
fdbec694
...
@@ -93,7 +93,7 @@ log_init_file(int line)
...
@@ -93,7 +93,7 @@ log_init_file(int line)
}
}
static
inline
LogLevel
static
inline
LogLevel
parse_log_level
(
const
char
*
value
,
int
line
)
parse_log_level
(
const
char
*
value
)
{
{
if
(
0
==
strcmp
(
value
,
"default"
))
if
(
0
==
strcmp
(
value
,
"default"
))
return
LogLevel
::
DEFAULT
;
return
LogLevel
::
DEFAULT
;
...
@@ -102,8 +102,7 @@ parse_log_level(const char *value, int line)
...
@@ -102,8 +102,7 @@ parse_log_level(const char *value, int line)
else
if
(
0
==
strcmp
(
value
,
"verbose"
))
else
if
(
0
==
strcmp
(
value
,
"verbose"
))
return
LogLevel
::
DEBUG
;
return
LogLevel
::
DEBUG
;
else
else
throw
FormatRuntimeError
(
"unknown log level
\"
%s
\"
at line %d"
,
throw
FormatRuntimeError
(
"unknown log level
\"
%s
\"
"
,
value
);
value
,
line
);
}
}
#endif
#endif
...
@@ -132,9 +131,12 @@ log_init(const ConfigData &config, bool verbose, bool use_stdout)
...
@@ -132,9 +131,12 @@ log_init(const ConfigData &config, bool verbose, bool use_stdout)
#else
#else
if
(
verbose
)
if
(
verbose
)
SetLogThreshold
(
LogLevel
::
DEBUG
);
SetLogThreshold
(
LogLevel
::
DEBUG
);
else
if
(
const
auto
&
param
=
config
.
GetParam
(
ConfigOption
::
LOG_LEVEL
))
else
SetLogThreshold
(
parse_log_level
(
param
->
value
.
c_str
(),
SetLogThreshold
(
config
.
With
(
ConfigOption
::
LOG_LEVEL
,
[](
const
char
*
s
){
param
->
line
));
return
s
!=
nullptr
?
parse_log_level
(
s
)
:
LogLevel
::
DEFAULT
;
}));
if
(
use_stdout
)
{
if
(
use_stdout
)
{
out_fd
=
STDOUT_FILENO
;
out_fd
=
STDOUT_FILENO
;
...
...
src/Main.cxx
View file @
fdbec694
...
@@ -282,20 +282,23 @@ initialize_decoder_and_player(Instance &instance,
...
@@ -282,20 +282,23 @@ initialize_decoder_and_player(Instance &instance,
size_t
buffer_size
;
size_t
buffer_size
;
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_BUFFER_SIZE
);
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_BUFFER_SIZE
);
if
(
param
!=
nullptr
)
{
if
(
param
!=
nullptr
)
{
char
*
test
;
buffer_size
=
param
->
With
([](
const
char
*
s
){
long
tmp
=
strtol
(
param
->
value
.
c_str
(),
&
test
,
10
);
char
*
test
;
if
(
*
test
!=
'\0'
||
tmp
<=
0
||
tmp
==
LONG_MAX
)
long
tmp
=
strtol
(
s
,
&
test
,
10
);
throw
FormatRuntimeError
(
"buffer size
\"
%s
\"
is not a "
if
(
*
test
!=
'\0'
||
tmp
<=
0
||
tmp
==
LONG_MAX
)
"positive integer, line %i"
,
throw
FormatRuntimeError
(
"buffer size
\"
%s
\"
is not a "
param
->
value
.
c_str
(),
param
->
line
);
"positive integer"
,
s
);
buffer_size
=
tmp
*
KILOBYTE
;
size_t
result
=
tmp
*
KILOBYTE
;
if
(
buffer_size
<
MIN_BUFFER_SIZE
)
{
if
(
result
<
MIN_BUFFER_SIZE
)
{
FormatWarning
(
config_domain
,
"buffer size %lu is too small, using %lu bytes instead"
,
FormatWarning
(
config_domain
,
"buffer size %lu is too small, using %lu bytes instead"
,
(
unsigned
long
)
buffer_size
,
(
unsigned
long
)
result
,
(
unsigned
long
)
MIN_BUFFER_SIZE
);
(
unsigned
long
)
MIN_BUFFER_SIZE
);
buffer_size
=
MIN_BUFFER_SIZE
;
result
=
MIN_BUFFER_SIZE
;
}
}
return
result
;
});
}
else
}
else
buffer_size
=
DEFAULT_BUFFER_SIZE
;
buffer_size
=
DEFAULT_BUFFER_SIZE
;
...
@@ -309,17 +312,12 @@ initialize_decoder_and_player(Instance &instance,
...
@@ -309,17 +312,12 @@ initialize_decoder_and_player(Instance &instance,
config
.
GetPositive
(
ConfigOption
::
MAX_PLAYLIST_LENGTH
,
config
.
GetPositive
(
ConfigOption
::
MAX_PLAYLIST_LENGTH
,
DEFAULT_PLAYLIST_MAX_LENGTH
);
DEFAULT_PLAYLIST_MAX_LENGTH
);
AudioFormat
configured_audio_format
=
AudioFormat
::
Undefined
();
AudioFormat
configured_audio_format
=
config
.
With
(
ConfigOption
::
AUDIO_OUTPUT_FORMAT
,
[](
const
char
*
s
){
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_OUTPUT_FORMAT
);
if
(
s
==
nullptr
)
if
(
param
!=
nullptr
)
{
return
AudioFormat
::
Undefined
();
try
{
configured_audio_format
=
ParseAudioFormat
(
param
->
value
.
c_str
(),
return
ParseAudioFormat
(
s
,
true
);
true
);
});
}
catch
(...)
{
std
::
throw_with_nested
(
FormatRuntimeError
(
"error parsing line %i"
,
param
->
line
));
}
}
instance
.
partitions
.
emplace_back
(
instance
,
instance
.
partitions
.
emplace_back
(
instance
,
"default"
,
"default"
,
...
@@ -329,15 +327,11 @@ initialize_decoder_and_player(Instance &instance,
...
@@ -329,15 +327,11 @@ initialize_decoder_and_player(Instance &instance,
replay_gain_config
);
replay_gain_config
);
auto
&
partition
=
instance
.
partitions
.
back
();
auto
&
partition
=
instance
.
partitions
.
back
();
try
{
partition
.
replay_gain_mode
=
config
.
With
(
ConfigOption
::
REPLAYGAIN
,
[](
const
char
*
s
){
param
=
config
.
GetParam
(
ConfigOption
::
REPLAYGAIN
);
return
s
!=
nullptr
if
(
param
!=
nullptr
)
?
FromString
(
s
)
partition
.
replay_gain_mode
=
:
ReplayGainMode
::
OFF
;
FromString
(
param
->
value
.
c_str
());
});
}
catch
(...)
{
std
::
throw_with_nested
(
FormatRuntimeError
(
"Failed to parse line %i"
,
param
->
line
));
}
}
}
inline
void
inline
void
...
...
src/Permission.cxx
View file @
fdbec694
...
@@ -82,44 +82,39 @@ static unsigned parsePermissions(const char *string)
...
@@ -82,44 +82,39 @@ static unsigned parsePermissions(const char *string)
void
void
initPermissions
(
const
ConfigData
&
config
)
initPermissions
(
const
ConfigData
&
config
)
{
{
unsigned
permission
;
permission_default
=
PERMISSION_READ
|
PERMISSION_ADD
|
permission_default
=
PERMISSION_READ
|
PERMISSION_ADD
|
PERMISSION_CONTROL
|
PERMISSION_ADMIN
;
PERMISSION_CONTROL
|
PERMISSION_ADMIN
;
for
(
const
auto
&
param
:
config
.
GetParamList
(
ConfigOption
::
PASSWORD
))
{
for
(
const
auto
&
param
:
config
.
GetParamList
(
ConfigOption
::
PASSWORD
))
{
permission_default
=
0
;
permission_default
=
0
;
const
char
*
separator
=
strchr
(
param
.
value
.
c_str
(),
param
.
With
([](
const
char
*
value
){
PERMISSION_PASSWORD_CHAR
);
const
char
*
separator
=
strchr
(
value
,
PERMISSION_PASSWORD_CHAR
);
if
(
separator
==
NULL
)
throw
FormatRuntimeError
(
"
\"
%c
\"
not found in password string "
"
\"
%s
\"
, line %i"
,
PERMISSION_PASSWORD_CHAR
,
param
.
value
.
c_str
(),
param
.
line
);
std
::
string
password
(
param
.
value
.
c_str
(),
separator
);
if
(
separator
==
NULL
)
throw
FormatRuntimeError
(
"
\"
%c
\"
not found in password string"
,
PERMISSION_PASSWORD_CHAR
);
permission
=
parsePermissions
(
separator
+
1
);
std
::
string
password
(
value
,
separator
);
permission_passwords
.
insert
(
std
::
make_pair
(
std
::
move
(
password
),
unsigned
permission
=
parsePermissions
(
separator
+
1
);
permission
));
permission_passwords
.
insert
(
std
::
make_pair
(
std
::
move
(
password
),
permission
));
});
}
}
const
ConfigParam
*
param
;
config
.
With
(
ConfigOption
::
DEFAULT_PERMS
,
[](
const
char
*
value
){
param
=
config
.
GetParam
(
ConfigOption
::
DEFAULT_PERMS
);
if
(
value
!=
nullptr
)
permission_default
=
parsePermissions
(
value
);
if
(
param
)
});
permission_default
=
parsePermissions
(
param
->
value
.
c_str
());
#ifdef HAVE_UN
#ifdef HAVE_UN
param
=
config
.
GetParam
(
ConfigOption
::
LOCAL_PERMISSIONS
);
local_permissions
=
config
.
With
(
ConfigOption
::
LOCAL_PERMISSIONS
,
[](
const
char
*
value
){
if
(
param
!=
nullptr
)
return
value
!=
nullptr
local_permissions
=
parsePermissions
(
param
->
value
.
c_str
());
?
parsePermissions
(
value
)
else
:
permission_default
;
local_permissions
=
permission_default
;
})
;
#endif
#endif
}
}
...
...
src/ReplayGainGlobal.cxx
View file @
fdbec694
...
@@ -43,29 +43,22 @@ ParsePreamp(const char *s)
...
@@ -43,29 +43,22 @@ ParsePreamp(const char *s)
return
pow
(
10
,
f
/
20.0
);
return
pow
(
10
,
f
/
20.0
);
}
}
static
float
ParsePreamp
(
const
ConfigParam
&
p
)
{
try
{
return
ParsePreamp
(
p
.
value
.
c_str
());
}
catch
(...)
{
std
::
throw_with_nested
(
FormatRuntimeError
(
"Failed to parse line %i"
,
p
.
line
));
}
}
ReplayGainConfig
ReplayGainConfig
LoadReplayGainConfig
(
const
ConfigData
&
config
)
LoadReplayGainConfig
(
const
ConfigData
&
config
)
{
{
ReplayGainConfig
replay_gain_config
;
ReplayGainConfig
replay_gain_config
;
const
auto
*
param
=
config
.
GetParam
(
ConfigOption
::
REPLAYGAIN_PREAMP
);
replay_gain_config
.
preamp
=
config
.
With
(
ConfigOption
::
REPLAYGAIN_PREAMP
,
[](
const
char
*
s
){
if
(
param
)
return
s
!=
nullptr
replay_gain_config
.
preamp
=
ParsePreamp
(
*
param
);
?
ParsePreamp
(
s
)
:
1.0
;
});
param
=
config
.
GetParam
(
ConfigOption
::
REPLAYGAIN_MISSING_PREAMP
);
replay_gain_config
.
missing_preamp
=
config
.
With
(
ConfigOption
::
REPLAYGAIN_MISSING_PREAMP
,
[](
const
char
*
s
){
if
(
param
)
return
s
!=
nullptr
replay_gain_config
.
missing_preamp
=
ParsePreamp
(
*
param
);
?
ParsePreamp
(
s
)
:
1.0
;
});
replay_gain_config
.
limit
=
config
.
GetBool
(
ConfigOption
::
REPLAYGAIN_LIMIT
,
replay_gain_config
.
limit
=
config
.
GetBool
(
ConfigOption
::
REPLAYGAIN_LIMIT
,
ReplayGainConfig
::
DEFAULT_LIMIT
);
ReplayGainConfig
::
DEFAULT_LIMIT
);
...
...
src/config/Data.hxx
View file @
fdbec694
...
@@ -54,6 +54,14 @@ struct ConfigData {
...
@@ -54,6 +54,14 @@ struct ConfigData {
return
list
.
empty
()
?
nullptr
:
&
list
.
front
();
return
list
.
empty
()
?
nullptr
:
&
list
.
front
();
}
}
template
<
typename
F
>
auto
With
(
ConfigOption
option
,
F
&&
f
)
const
{
const
auto
*
param
=
GetParam
(
option
);
return
param
!=
nullptr
?
param
->
With
(
std
::
forward
<
F
>
(
f
))
:
f
(
nullptr
);
}
gcc_pure
gcc_pure
const
char
*
GetString
(
ConfigOption
option
,
const
char
*
GetString
(
ConfigOption
option
,
const
char
*
default_value
=
nullptr
)
const
noexcept
;
const
char
*
default_value
=
nullptr
)
const
noexcept
;
...
...
src/config/Param.hxx
View file @
fdbec694
...
@@ -66,6 +66,19 @@ struct ConfigParam {
...
@@ -66,6 +66,19 @@ struct ConfigParam {
*/
*/
[[
noreturn
]]
[[
noreturn
]]
void
ThrowWithNested
()
const
;
void
ThrowWithNested
()
const
;
/**
* Invoke a function with the configured value; if the
* function throws, call ThrowWithNested().
*/
template
<
typename
F
>
auto
With
(
F
&&
f
)
const
{
try
{
return
f
(
value
.
c_str
());
}
catch
(...)
{
ThrowWithNested
();
}
}
};
};
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment