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
4eb101f0
Commit
4eb101f0
authored
May 29, 2019
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config/Parser: add ParseSize()
Supports suffixes such as "kB" and "MB".
parent
af797033
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
6 deletions
+83
-6
user.rst
doc/user.rst
+3
-2
Main.cxx
src/Main.cxx
+3
-4
Parser.cxx
src/config/Parser.cxx
+67
-0
Parser.hxx
src/config/Parser.hxx
+10
-0
No files found.
doc/user.rst
View file @
4eb101f0
...
@@ -712,8 +712,9 @@ Do not change these unless you know what you are doing.
...
@@ -712,8 +712,9 @@ Do not change these unless you know what you are doing.
* - Setting
* - Setting
- Description
- Description
* - **audio_buffer_size KBYTES**
* - **audio_buffer_size SIZE**
- Adjust the size of the internal audio buffer. Default is 4096 (4 MiB).
- Adjust the size of the internal audio buffer. Default is
:samp:`4 MB` (4 MiB).
Zeroconf
Zeroconf
~~~~~~~~
~~~~~~~~
...
...
src/Main.cxx
View file @
4eb101f0
...
@@ -58,6 +58,7 @@
...
@@ -58,6 +58,7 @@
#include "config/Defaults.hxx"
#include "config/Defaults.hxx"
#include "config/Option.hxx"
#include "config/Option.hxx"
#include "config/Domain.hxx"
#include "config/Domain.hxx"
#include "config/Parser.hxx"
#include "util/RuntimeError.hxx"
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
#include "util/ScopeExit.hxx"
...
@@ -280,12 +281,10 @@ initialize_decoder_and_player(Instance &instance,
...
@@ -280,12 +281,10 @@ initialize_decoder_and_player(Instance &instance,
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_BUFFER_SIZE
);
param
=
config
.
GetParam
(
ConfigOption
::
AUDIO_BUFFER_SIZE
);
if
(
param
!=
nullptr
)
{
if
(
param
!=
nullptr
)
{
buffer_size
=
param
->
With
([](
const
char
*
s
){
buffer_size
=
param
->
With
([](
const
char
*
s
){
char
*
test
;
size_t
result
=
ParseSize
(
s
,
KILOBYTE
);
long
tmp
=
strtol
(
s
,
&
test
,
10
);
if
(
result
<=
0
)
if
(
*
test
!=
'\0'
||
tmp
<=
0
||
tmp
==
LONG_MAX
)
throw
FormatRuntimeError
(
"buffer size
\"
%s
\"
is not a "
throw
FormatRuntimeError
(
"buffer size
\"
%s
\"
is not a "
"positive integer"
,
s
);
"positive integer"
,
s
);
size_t
result
=
tmp
*
KILOBYTE
;
if
(
result
<
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"
,
...
...
src/config/Parser.cxx
View file @
4eb101f0
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include "Parser.hxx"
#include "Parser.hxx"
#include "util/RuntimeError.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringStrip.hxx"
#include "util/StringUtil.hxx"
#include "util/StringUtil.hxx"
bool
bool
...
@@ -35,3 +36,69 @@ ParseBool(const char *value)
...
@@ -35,3 +36,69 @@ ParseBool(const char *value)
throw
FormatRuntimeError
(
"Not a valid boolean (
\"
yes
\"
or
\"
no
\"
):
\"
%s
\"
"
,
value
);
throw
FormatRuntimeError
(
"Not a valid boolean (
\"
yes
\"
or
\"
no
\"
):
\"
%s
\"
"
,
value
);
}
}
template
<
size_t
OPERAND
>
static
size_t
Multiply
(
size_t
value
)
{
static
constexpr
size_t
MAX_INPUT
=
SIZE_MAX
/
OPERAND
;
if
(
value
>
MAX_INPUT
)
throw
std
::
runtime_error
(
"Value too large"
);
return
value
*
OPERAND
;
}
size_t
ParseSize
(
const
char
*
s
,
size_t
default_factor
)
{
char
*
endptr
;
size_t
value
=
strtoul
(
s
,
&
endptr
,
10
);
if
(
endptr
==
s
)
throw
std
::
runtime_error
(
"Failed to parse integer"
);
static
constexpr
size_t
KILO
=
1024
;
static
constexpr
size_t
MEGA
=
1024
*
KILO
;
static
constexpr
size_t
GIGA
=
1024
*
MEGA
;
s
=
StripLeft
(
endptr
);
bool
apply_factor
=
false
;
switch
(
*
s
)
{
case
'k'
:
value
=
Multiply
<
KILO
>
(
value
);
++
s
;
break
;
case
'M'
:
value
=
Multiply
<
MEGA
>
(
value
);
++
s
;
break
;
case
'G'
:
value
=
Multiply
<
GIGA
>
(
value
);
++
s
;
break
;
case
'\0'
:
apply_factor
=
true
;
break
;
default
:
throw
std
::
runtime_error
(
"Unknown size suffix"
);
}
/* ignore 'B' for "byte" */
if
(
*
s
==
'B'
)
{
apply_factor
=
false
;
++
s
;
}
if
(
*
s
!=
'\0'
)
throw
std
::
runtime_error
(
"Unknown size suffix"
);
if
(
apply_factor
)
value
*=
default_factor
;
return
value
;
}
src/config/Parser.hxx
View file @
4eb101f0
...
@@ -20,10 +20,20 @@
...
@@ -20,10 +20,20 @@
#ifndef MPD_CONFIG_PARSER_HXX
#ifndef MPD_CONFIG_PARSER_HXX
#define MPD_CONFIG_PARSER_HXX
#define MPD_CONFIG_PARSER_HXX
#include <stddef.h>
/**
/**
* Throws on error.
* Throws on error.
*/
*/
bool
bool
ParseBool
(
const
char
*
value
);
ParseBool
(
const
char
*
value
);
/**
* Parse a string as a byte size.
*
* Throws on error.
*/
size_t
ParseSize
(
const
char
*
s
,
size_t
default_factor
=
1
);
#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