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
ab318200
Commit
ab318200
authored
Oct 08, 2020
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config/{Data,Block}: use With() in GetUnsigned(), GetPositive()
parent
947856ca
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
49 deletions
+63
-49
Block.cxx
src/config/Block.cxx
+2
-17
Data.cxx
src/config/Data.cxx
+10
-32
Parser.cxx
src/config/Parser.cxx
+33
-0
Parser.hxx
src/config/Parser.hxx
+18
-0
No files found.
src/config/Block.cxx
View file @
ab318200
...
...
@@ -47,28 +47,13 @@ BlockParam::GetIntValue() const
unsigned
BlockParam
::
GetUnsignedValue
()
const
{
const
char
*
const
s
=
value
.
c_str
();
char
*
endptr
;
unsigned
long
value2
=
strtoul
(
s
,
&
endptr
,
0
);
if
(
endptr
==
s
||
*
endptr
!=
0
)
throw
FormatRuntimeError
(
"Not a valid number in line %i"
,
line
);
return
(
unsigned
)
value2
;
return
With
(
ParseUnsigned
);
}
unsigned
BlockParam
::
GetPositiveValue
()
const
{
const
char
*
const
s
=
value
.
c_str
();
char
*
endptr
;
unsigned
long
value2
=
strtoul
(
s
,
&
endptr
,
0
);
if
(
endptr
==
s
||
*
endptr
!=
0
)
throw
FormatRuntimeError
(
"Not a valid number in line %i"
,
line
);
if
(
value2
<=
0
)
throw
FormatRuntimeError
(
"Number in line %i must be positive"
,
line
);
return
(
unsigned
)
value2
;
return
With
(
ParsePositive
);
}
bool
...
...
src/config/Data.cxx
View file @
ab318200
...
...
@@ -84,43 +84,21 @@ ConfigData::GetPath(ConfigOption option) const
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
;
return
With
(
option
,
[
default_value
](
const
char
*
s
){
return
s
!=
nullptr
?
ParseUnsigned
(
s
)
:
default_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
;
return
With
(
option
,
[
default_value
](
const
char
*
s
){
return
s
!=
nullptr
?
ParsePositive
(
s
)
:
default_value
;
});
}
bool
...
...
src/config/Parser.cxx
View file @
ab318200
...
...
@@ -22,6 +22,8 @@
#include "util/StringStrip.hxx"
#include "util/StringUtil.hxx"
#include <cstdlib>
bool
ParseBool
(
const
char
*
value
)
{
...
...
@@ -37,6 +39,37 @@ ParseBool(const char *value)
throw
FormatRuntimeError
(
R"(Not a valid boolean ("yes" or "no"): "%s")"
,
value
);
}
long
ParseLong
(
const
char
*
s
)
{
char
*
endptr
;
long
value
=
strtol
(
s
,
&
endptr
,
10
);
if
(
endptr
==
s
||
*
endptr
!=
0
)
throw
std
::
runtime_error
(
"Failed to parse number"
);
return
value
;
}
unsigned
ParseUnsigned
(
const
char
*
s
)
{
auto
value
=
ParseLong
(
s
);
if
(
value
<
0
)
throw
std
::
runtime_error
(
"Value must not be negative"
);
return
(
unsigned
)
value
;
}
unsigned
ParsePositive
(
const
char
*
s
)
{
auto
value
=
ParseLong
(
s
);
if
(
value
<=
0
)
throw
std
::
runtime_error
(
"Value must be positive"
);
return
(
unsigned
)
value
;
}
template
<
size_t
OPERAND
>
static
size_t
Multiply
(
size_t
value
)
...
...
src/config/Parser.hxx
View file @
ab318200
...
...
@@ -29,6 +29,24 @@ bool
ParseBool
(
const
char
*
value
);
/**
* Throws on error.
*/
long
ParseLong
(
const
char
*
s
);
/**
* Throws on error.
*/
unsigned
ParseUnsigned
(
const
char
*
s
);
/**
* Throws on error.
*/
unsigned
ParsePositive
(
const
char
*
s
);
/**
* Parse a string as a byte size.
*
* Throws on error.
...
...
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