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)
}
static
inline
LogLevel
parse_log_level
(
const
char
*
value
,
int
line
)
parse_log_level
(
const
char
*
value
)
{
if
(
0
==
strcmp
(
value
,
"default"
))
return
LogLevel
::
DEFAULT
;
...
...
@@ -102,8 +102,7 @@ parse_log_level(const char *value, int line)
else
if
(
0
==
strcmp
(
value
,
"verbose"
))
return
LogLevel
::
DEBUG
;
else
throw
FormatRuntimeError
(
"unknown log level
\"
%s
\"
at line %d"
,
value
,
line
);
throw
FormatRuntimeError
(
"unknown log level
\"
%s
\"
"
,
value
);
}
#endif
...
...
@@ -132,9 +131,12 @@ log_init(const ConfigData &config, bool verbose, bool use_stdout)
#else
if
(
verbose
)
SetLogThreshold
(
LogLevel
::
DEBUG
);
else
if
(
const
auto
&
param
=
config
.
GetParam
(
ConfigOption
::
LOG_LEVEL
))
SetLogThreshold
(
parse_log_level
(
param
->
value
.
c_str
(),
param
->
line
));
else
SetLogThreshold
(
config
.
With
(
ConfigOption
::
LOG_LEVEL
,
[](
const
char
*
s
){
return
s
!=
nullptr
?
parse_log_level
(
s
)
:
LogLevel
::
DEFAULT
;
}));
if
(
use_stdout
)
{
out_fd
=
STDOUT_FILENO
;
...
...
src/Main.cxx
View file @
fdbec694
...
...
@@ -282,20 +282,23 @@ initialize_decoder_and_player(Instance &instance,
size_t
buffer_size
;
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_BUFFER_SIZE
);
if
(
param
!=
nullptr
)
{
char
*
test
;
long
tmp
=
strtol
(
param
->
value
.
c_str
(),
&
test
,
10
);
if
(
*
test
!=
'\0'
||
tmp
<=
0
||
tmp
==
LONG_MAX
)
throw
FormatRuntimeError
(
"buffer size
\"
%s
\"
is not a "
"positive integer, line %i"
,
param
->
value
.
c_str
(),
param
->
line
);
buffer_size
=
tmp
*
KILOBYTE
;
if
(
buffer_size
<
MIN_BUFFER_SIZE
)
{
FormatWarning
(
config_domain
,
"buffer size %lu is too small, using %lu bytes instead"
,
(
unsigned
long
)
buffer_size
,
(
unsigned
long
)
MIN_BUFFER_SIZE
);
buffer_size
=
MIN_BUFFER_SIZE
;
}
buffer_size
=
param
->
With
([](
const
char
*
s
){
char
*
test
;
long
tmp
=
strtol
(
s
,
&
test
,
10
);
if
(
*
test
!=
'\0'
||
tmp
<=
0
||
tmp
==
LONG_MAX
)
throw
FormatRuntimeError
(
"buffer size
\"
%s
\"
is not a "
"positive integer"
,
s
);
size_t
result
=
tmp
*
KILOBYTE
;
if
(
result
<
MIN_BUFFER_SIZE
)
{
FormatWarning
(
config_domain
,
"buffer size %lu is too small, using %lu bytes instead"
,
(
unsigned
long
)
result
,
(
unsigned
long
)
MIN_BUFFER_SIZE
);
result
=
MIN_BUFFER_SIZE
;
}
return
result
;
});
}
else
buffer_size
=
DEFAULT_BUFFER_SIZE
;
...
...
@@ -309,17 +312,12 @@ initialize_decoder_and_player(Instance &instance,
config
.
GetPositive
(
ConfigOption
::
MAX_PLAYLIST_LENGTH
,
DEFAULT_PLAYLIST_MAX_LENGTH
);
AudioFormat
configured_audio_format
=
AudioFormat
::
Undefined
();
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_OUTPUT_FORMAT
);
if
(
param
!=
nullptr
)
{
try
{
configured_audio_format
=
ParseAudioFormat
(
param
->
value
.
c_str
(),
true
);
}
catch
(...)
{
std
::
throw_with_nested
(
FormatRuntimeError
(
"error parsing line %i"
,
param
->
line
));
}
}
AudioFormat
configured_audio_format
=
config
.
With
(
ConfigOption
::
AUDIO_OUTPUT_FORMAT
,
[](
const
char
*
s
){
if
(
s
==
nullptr
)
return
AudioFormat
::
Undefined
();
return
ParseAudioFormat
(
s
,
true
);
});
instance
.
partitions
.
emplace_back
(
instance
,
"default"
,
...
...
@@ -329,15 +327,11 @@ initialize_decoder_and_player(Instance &instance,
replay_gain_config
);
auto
&
partition
=
instance
.
partitions
.
back
();
try
{
param
=
config
.
GetParam
(
ConfigOption
::
REPLAYGAIN
);
if
(
param
!=
nullptr
)
partition
.
replay_gain_mode
=
FromString
(
param
->
value
.
c_str
());
}
catch
(...)
{
std
::
throw_with_nested
(
FormatRuntimeError
(
"Failed to parse line %i"
,
param
->
line
));
}
partition
.
replay_gain_mode
=
config
.
With
(
ConfigOption
::
REPLAYGAIN
,
[](
const
char
*
s
){
return
s
!=
nullptr
?
FromString
(
s
)
:
ReplayGainMode
::
OFF
;
});
}
inline
void
...
...
src/Permission.cxx
View file @
fdbec694
...
...
@@ -82,44 +82,39 @@ static unsigned parsePermissions(const char *string)
void
initPermissions
(
const
ConfigData
&
config
)
{
unsigned
permission
;
permission_default
=
PERMISSION_READ
|
PERMISSION_ADD
|
PERMISSION_CONTROL
|
PERMISSION_ADMIN
;
for
(
const
auto
&
param
:
config
.
GetParamList
(
ConfigOption
::
PASSWORD
))
{
permission_default
=
0
;
const
char
*
separator
=
strchr
(
param
.
value
.
c_str
(),
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
);
param
.
With
([](
const
char
*
value
){
const
char
*
separator
=
strchr
(
value
,
PERMISSION_PASSWORD_CHAR
);
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
),
permission
));
unsigned
permission
=
parsePermissions
(
separator
+
1
);
permission_passwords
.
insert
(
std
::
make_pair
(
std
::
move
(
password
),
permission
));
});
}
const
ConfigParam
*
param
;
param
=
config
.
GetParam
(
ConfigOption
::
DEFAULT_PERMS
);
if
(
param
)
permission_default
=
parsePermissions
(
param
->
value
.
c_str
());
config
.
With
(
ConfigOption
::
DEFAULT_PERMS
,
[](
const
char
*
value
){
if
(
value
!=
nullptr
)
permission_default
=
parsePermissions
(
value
);
});
#ifdef HAVE_UN
param
=
config
.
GetParam
(
ConfigOption
::
LOCAL_PERMISSIONS
);
if
(
param
!=
nullptr
)
local_permissions
=
parsePermissions
(
param
->
value
.
c_str
());
else
local_permissions
=
permission_default
;
local_permissions
=
config
.
With
(
ConfigOption
::
LOCAL_PERMISSIONS
,
[](
const
char
*
value
){
return
value
!=
nullptr
?
parsePermissions
(
value
)
:
permission_default
;
})
;
#endif
}
...
...
src/ReplayGainGlobal.cxx
View file @
fdbec694
...
...
@@ -43,29 +43,22 @@ ParsePreamp(const char *s)
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
LoadReplayGainConfig
(
const
ConfigData
&
config
)
{
ReplayGainConfig
replay_gain_config
;
const
auto
*
param
=
config
.
GetParam
(
ConfigOption
::
REPLAYGAIN_PREAMP
);
if
(
param
)
replay_gain_config
.
preamp
=
ParsePreamp
(
*
param
);
replay_gain_config
.
preamp
=
config
.
With
(
ConfigOption
::
REPLAYGAIN_PREAMP
,
[](
const
char
*
s
){
return
s
!=
nullptr
?
ParsePreamp
(
s
)
:
1.0
;
});
param
=
config
.
GetParam
(
ConfigOption
::
REPLAYGAIN_MISSING_PREAMP
);
if
(
param
)
replay_gain_config
.
missing_preamp
=
ParsePreamp
(
*
param
);
replay_gain_config
.
missing_preamp
=
config
.
With
(
ConfigOption
::
REPLAYGAIN_MISSING_PREAMP
,
[](
const
char
*
s
){
return
s
!=
nullptr
?
ParsePreamp
(
s
)
:
1.0
;
});
replay_gain_config
.
limit
=
config
.
GetBool
(
ConfigOption
::
REPLAYGAIN_LIMIT
,
ReplayGainConfig
::
DEFAULT_LIMIT
);
...
...
src/config/Data.hxx
View file @
fdbec694
...
...
@@ -54,6 +54,14 @@ struct ConfigData {
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
const
char
*
GetString
(
ConfigOption
option
,
const
char
*
default_value
=
nullptr
)
const
noexcept
;
...
...
src/config/Param.hxx
View file @
fdbec694
...
...
@@ -66,6 +66,19 @@ struct ConfigParam {
*/
[[
noreturn
]]
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
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