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
ffe68199
Commit
ffe68199
authored
Jul 17, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config/Global: move value parser code to struct ConfigData
parent
8af75c78
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
112 additions
and
59 deletions
+112
-59
Data.cxx
src/config/Data.cxx
+85
-0
Data.hxx
src/config/Data.hxx
+22
-0
Global.cxx
src/config/Global.cxx
+5
-59
No files found.
src/config/Data.cxx
View file @
ffe68199
...
...
@@ -21,9 +21,13 @@
#include "Data.hxx"
#include "Param.hxx"
#include "Block.hxx"
#include "Parser.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringAPI.hxx"
#include <stdlib.h>
void
ConfigData
::
Clear
()
{
...
...
@@ -38,6 +42,87 @@ ConfigData::Clear()
}
}
const
char
*
ConfigData
::
GetString
(
ConfigOption
option
,
const
char
*
default_value
)
const
noexcept
{
const
auto
*
param
=
GetParam
(
option
);
if
(
param
==
nullptr
)
return
default_value
;
return
param
->
value
.
c_str
();
}
AllocatedPath
ConfigData
::
GetPath
(
ConfigOption
option
)
const
{
const
auto
*
param
=
GetParam
(
option
);
if
(
param
==
nullptr
)
return
nullptr
;
return
param
->
GetPath
();
}
unsigned
ConfigData
::
GetUnsigned
(
ConfigOption
option
,
unsigned
default_value
)
const
{
const
auto
*
param
=
GetParam
(
option
);
long
value
;
char
*
endptr
;
if
(
param
==
nullptr
)
return
default_value
;
const
char
*
const
s
=
param
->
value
.
c_str
();
value
=
strtol
(
s
,
&
endptr
,
0
);
if
(
endptr
==
s
||
*
endptr
!=
0
||
value
<
0
)
throw
FormatRuntimeError
(
"Not a valid non-negative number in line %i"
,
param
->
line
);
return
(
unsigned
)
value
;
}
unsigned
ConfigData
::
GetPositive
(
ConfigOption
option
,
unsigned
default_value
)
const
{
const
auto
*
param
=
GetParam
(
option
);
long
value
;
char
*
endptr
;
if
(
param
==
nullptr
)
return
default_value
;
const
char
*
const
s
=
param
->
value
.
c_str
();
value
=
strtol
(
s
,
&
endptr
,
0
);
if
(
endptr
==
s
||
*
endptr
!=
0
)
throw
FormatRuntimeError
(
"Not a valid number in line %i"
,
param
->
line
);
if
(
value
<=
0
)
throw
FormatRuntimeError
(
"Not a positive number in line %i"
,
param
->
line
);
return
(
unsigned
)
value
;
}
bool
ConfigData
::
GetBool
(
ConfigOption
option
,
bool
default_value
)
const
{
const
auto
*
param
=
GetParam
(
option
);
bool
success
,
value
;
if
(
param
==
nullptr
)
return
default_value
;
success
=
get_bool
(
param
->
value
.
c_str
(),
&
value
);
if
(
!
success
)
throw
FormatRuntimeError
(
"Expected boolean value (yes, true, 1) or "
"(no, false, 0) on line %i
\n
"
,
param
->
line
);
return
value
;
}
const
ConfigBlock
*
ConfigData
::
FindBlock
(
ConfigBlockOption
option
,
const
char
*
key
,
const
char
*
value
)
const
...
...
src/config/Data.hxx
View file @
ffe68199
...
...
@@ -26,6 +26,7 @@
struct
ConfigParam
;
struct
ConfigBlock
;
class
AllocatedPath
;
struct
ConfigData
{
std
::
array
<
ConfigParam
*
,
std
::
size_t
(
ConfigOption
::
MAX
)
>
params
{{
nullptr
}};
...
...
@@ -39,6 +40,27 @@ struct ConfigData {
}
gcc_pure
const
char
*
GetString
(
ConfigOption
option
,
const
char
*
default_value
=
nullptr
)
const
noexcept
;
/**
* Returns an optional configuration variable which contains an
* absolute path. If there is a tilde prefix, it is expanded.
* Returns nullptr if the value is not present.
*
* Throws #std::runtime_error on error.
*/
AllocatedPath
GetPath
(
ConfigOption
option
)
const
;
unsigned
GetUnsigned
(
ConfigOption
option
,
unsigned
default_value
)
const
;
unsigned
GetPositive
(
ConfigOption
option
,
unsigned
default_value
)
const
;
bool
GetBool
(
ConfigOption
option
,
bool
default_value
)
const
;
gcc_pure
const
ConfigBlock
*
GetBlock
(
ConfigBlockOption
option
)
const
noexcept
{
return
blocks
[
size_t
(
option
)];
}
...
...
src/config/Global.cxx
View file @
ffe68199
...
...
@@ -19,7 +19,6 @@
#include "config.h"
#include "Global.hxx"
#include "Parser.hxx"
#include "Data.hxx"
#include "Param.hxx"
#include "Block.hxx"
...
...
@@ -31,8 +30,6 @@
#include "util/RuntimeError.hxx"
#include "Log.hxx"
#include <stdlib.h>
static
ConfigData
config_data
;
void
config_global_finish
(
void
)
...
...
@@ -95,80 +92,29 @@ config_get_block(ConfigBlockOption option) noexcept
const
char
*
config_get_string
(
ConfigOption
option
,
const
char
*
default_value
)
noexcept
{
const
auto
*
param
=
config_get_param
(
option
);
if
(
param
==
nullptr
)
return
default_value
;
return
param
->
value
.
c_str
();
return
config_data
.
GetString
(
option
,
default_value
);
}
AllocatedPath
config_get_path
(
ConfigOption
option
)
{
const
auto
*
param
=
config_get_param
(
option
);
if
(
param
==
nullptr
)
return
nullptr
;
return
param
->
GetPath
();
return
config_data
.
GetPath
(
option
);
}
unsigned
config_get_unsigned
(
ConfigOption
option
,
unsigned
default_value
)
{
const
auto
*
param
=
config_get_param
(
option
);
long
value
;
char
*
endptr
;
if
(
param
==
nullptr
)
return
default_value
;
const
char
*
const
s
=
param
->
value
.
c_str
();
value
=
strtol
(
s
,
&
endptr
,
0
);
if
(
endptr
==
s
||
*
endptr
!=
0
||
value
<
0
)
throw
FormatRuntimeError
(
"Not a valid non-negative number in line %i"
,
param
->
line
);
return
(
unsigned
)
value
;
return
config_data
.
GetUnsigned
(
option
,
default_value
);
}
unsigned
config_get_positive
(
ConfigOption
option
,
unsigned
default_value
)
{
const
auto
*
param
=
config_get_param
(
option
);
long
value
;
char
*
endptr
;
if
(
param
==
nullptr
)
return
default_value
;
const
char
*
const
s
=
param
->
value
.
c_str
();
value
=
strtol
(
s
,
&
endptr
,
0
);
if
(
endptr
==
s
||
*
endptr
!=
0
)
throw
FormatRuntimeError
(
"Not a valid number in line %i"
,
param
->
line
);
if
(
value
<=
0
)
throw
FormatRuntimeError
(
"Not a positive number in line %i"
,
param
->
line
);
return
(
unsigned
)
value
;
return
config_data
.
GetPositive
(
option
,
default_value
);
}
bool
config_get_bool
(
ConfigOption
option
,
bool
default_value
)
{
const
auto
*
param
=
config_get_param
(
option
);
bool
success
,
value
;
if
(
param
==
nullptr
)
return
default_value
;
success
=
get_bool
(
param
->
value
.
c_str
(),
&
value
);
if
(
!
success
)
throw
FormatRuntimeError
(
"Expected boolean value (yes, true, 1) or "
"(no, false, 0) on line %i
\n
"
,
param
->
line
);
return
value
;
return
config_data
.
GetBool
(
option
,
default_value
);
}
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