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
8f1ec1df
Commit
8f1ec1df
authored
Oct 21, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/NumberParser: utilities for parsing numbers from ASCII strings
parent
222dc8a2
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
93 additions
and
15 deletions
+93
-15
Makefile.am
Makefile.am
+1
-0
DirectorySave.cxx
src/DirectorySave.cxx
+2
-2
LameEncoderPlugin.cxx
src/encoder/LameEncoderPlugin.cxx
+3
-4
TwolameEncoderPlugin.cxx
src/encoder/TwolameEncoderPlugin.cxx
+3
-4
VorbisEncoderPlugin.cxx
src/encoder/VorbisEncoderPlugin.cxx
+3
-2
CurlInputPlugin.cxx
src/input/CurlInputPlugin.cxx
+3
-3
NumberParser.hxx
src/util/NumberParser.hxx
+78
-0
No files found.
Makefile.am
View file @
8f1ec1df
...
...
@@ -246,6 +246,7 @@ libutil_a_SOURCES = \
src/util/Domain.hxx
\
src/util/ReusableArray.hxx
\
src/util/ASCII.hxx
\
src/util/NumberParser.hxx
\
src/util/StringUtil.cxx src/util/StringUtil.hxx
\
src/util/FormatString.cxx src/util/FormatString.hxx
\
src/util/Tokenizer.cxx src/util/Tokenizer.hxx
\
...
...
src/DirectorySave.cxx
View file @
8f1ec1df
...
...
@@ -24,6 +24,7 @@
#include "SongSave.hxx"
#include "PlaylistDatabase.hxx"
#include "TextFile.hxx"
#include "util/NumberParser.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
...
...
@@ -95,8 +96,7 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name,
if
(
g_str_has_prefix
(
line
,
DIRECTORY_MTIME
))
{
directory
->
mtime
=
g_ascii_strtoull
(
line
+
sizeof
(
DIRECTORY_MTIME
)
-
1
,
nullptr
,
10
);
ParseUint64
(
line
+
sizeof
(
DIRECTORY_MTIME
)
-
1
);
line
=
file
.
ReadLine
();
if
(
line
==
nullptr
)
{
...
...
src/encoder/LameEncoderPlugin.cxx
View file @
8f1ec1df
...
...
@@ -22,6 +22,7 @@
#include "EncoderAPI.hxx"
#include "AudioFormat.hxx"
#include "ConfigError.hxx"
#include "util/NumberParser.hxx"
#include "util/ReusableArray.hxx"
#include "util/Manual.hxx"
#include "util/Error.hxx"
...
...
@@ -29,8 +30,6 @@
#include <lame/lame.h>
#include <glib.h>
#include <assert.h>
#include <string.h>
...
...
@@ -63,7 +62,7 @@ LameEncoder::Configure(const config_param ¶m, Error &error)
if
(
value
!=
nullptr
)
{
/* a quality was configured (VBR) */
quality
=
g_ascii_strtod
(
value
,
&
endptr
);
quality
=
ParseDouble
(
value
,
&
endptr
);
if
(
*
endptr
!=
'\0'
||
quality
<
-
1.0
||
quality
>
10.0
)
{
error
.
Format
(
config_domain
,
...
...
@@ -89,7 +88,7 @@ LameEncoder::Configure(const config_param ¶m, Error &error)
}
quality
=
-
2.0
;
bitrate
=
g_ascii_strtoll
(
value
,
&
endptr
,
10
);
bitrate
=
ParseInt
(
value
,
&
endptr
);
if
(
*
endptr
!=
'\0'
||
bitrate
<=
0
)
{
error
.
Set
(
config_domain
,
...
...
src/encoder/TwolameEncoderPlugin.cxx
View file @
8f1ec1df
...
...
@@ -22,14 +22,13 @@
#include "EncoderAPI.hxx"
#include "AudioFormat.hxx"
#include "ConfigError.hxx"
#include "util/NumberParser.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
#include <twolame.h>
#include <glib.h>
#include <assert.h>
#include <string.h>
...
...
@@ -69,7 +68,7 @@ TwolameEncoder::Configure(const config_param ¶m, Error &error)
if
(
value
!=
nullptr
)
{
/* a quality was configured (VBR) */
quality
=
g_ascii_strtod
(
value
,
&
endptr
);
quality
=
ParseDouble
(
value
,
&
endptr
);
if
(
*
endptr
!=
'\0'
||
quality
<
-
1.0
||
quality
>
10.0
)
{
error
.
Format
(
config_domain
,
...
...
@@ -95,7 +94,7 @@ TwolameEncoder::Configure(const config_param ¶m, Error &error)
}
quality
=
-
2.0
;
bitrate
=
g_ascii_strtoll
(
value
,
&
endptr
,
10
);
bitrate
=
ParseInt
(
value
,
&
endptr
);
if
(
*
endptr
!=
'\0'
||
bitrate
<=
0
)
{
error
.
Set
(
config_domain
,
...
...
src/encoder/VorbisEncoderPlugin.cxx
View file @
8f1ec1df
...
...
@@ -25,6 +25,7 @@
#include "tag/Tag.hxx"
#include "AudioFormat.hxx"
#include "ConfigError.hxx"
#include "util/NumberParser.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
...
...
@@ -67,7 +68,7 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder,
/* a quality was configured (VBR) */
char
*
endptr
;
encoder
->
quality
=
g_ascii_strtod
(
value
,
&
endptr
);
encoder
->
quality
=
ParseDouble
(
value
,
&
endptr
);
if
(
*
endptr
!=
'\0'
||
encoder
->
quality
<
-
1.0
||
encoder
->
quality
>
10.0
)
{
...
...
@@ -96,7 +97,7 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder,
encoder
->
quality
=
-
2.0
;
char
*
endptr
;
encoder
->
bitrate
=
g_ascii_strtoll
(
value
,
&
endptr
,
10
);
encoder
->
bitrate
=
ParseInt
(
value
,
&
endptr
);
if
(
*
endptr
!=
'\0'
||
encoder
->
bitrate
<=
0
)
{
error
.
Set
(
config_domain
,
"bitrate should be a positive integer"
);
...
...
src/input/CurlInputPlugin.cxx
View file @
8f1ec1df
...
...
@@ -31,6 +31,7 @@
#include "IOThread.hxx"
#include "util/ASCII.hxx"
#include "util/CharUtil.hxx"
#include "util/NumberParser.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
...
...
@@ -50,7 +51,6 @@
#include <forward_list>
#include <curl/curl.h>
#include <glib.h>
#if LIBCURL_VERSION_NUM < 0x071200
#error libcurl is too old
...
...
@@ -854,7 +854,7 @@ input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream)
memcpy
(
buffer
,
value
,
end
-
value
);
buffer
[
end
-
value
]
=
0
;
c
->
base
.
size
=
c
->
base
.
offset
+
g_ascii_strtoull
(
buffer
,
nullptr
,
10
);
c
->
base
.
size
=
c
->
base
.
offset
+
ParseUint64
(
buffer
);
}
else
if
(
StringEqualsCaseASCII
(
name
,
"content-type"
))
{
c
->
base
.
mime
.
assign
(
value
,
end
);
}
else
if
(
StringEqualsCaseASCII
(
name
,
"icy-name"
)
||
...
...
@@ -877,7 +877,7 @@ input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream)
memcpy
(
buffer
,
value
,
end
-
value
);
buffer
[
end
-
value
]
=
0
;
icy_metaint
=
g_ascii_strtoull
(
buffer
,
nullptr
,
10
);
icy_metaint
=
ParseUint64
(
buffer
);
FormatDebug
(
curl_domain
,
"icy-metaint=%zu"
,
icy_metaint
);
if
(
icy_metaint
>
0
)
{
...
...
src/util/NumberParser.hxx
0 → 100644
View file @
8f1ec1df
/*
* Copyright (C) 2009-2013 Max Kellermann <max@duempel.org>
* http://www.musicpd.org
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef NUMBER_PARSER_HXX
#define NUMBER_PARSER_HXX
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
static
inline
unsigned
ParseUnsigned
(
const
char
*
p
,
char
**
endptr
=
nullptr
,
int
base
=
10
)
{
assert
(
p
!=
nullptr
);
return
(
unsigned
)
strtoul
(
p
,
endptr
,
base
);
}
static
inline
int
ParseInt
(
const
char
*
p
,
char
**
endptr
=
nullptr
,
int
base
=
10
)
{
assert
(
p
!=
nullptr
);
return
(
int
)
strtol
(
p
,
endptr
,
base
);
}
static
inline
uint64_t
ParseUint64
(
const
char
*
p
,
char
**
endptr
=
nullptr
,
int
base
=
10
)
{
assert
(
p
!=
nullptr
);
return
strtoull
(
p
,
endptr
,
base
);
}
static
inline
int64_t
ParseInt64
(
const
char
*
p
,
char
**
endptr
=
nullptr
,
int
base
=
10
)
{
assert
(
p
!=
nullptr
);
return
strtoll
(
p
,
endptr
,
base
);
}
static
inline
double
ParseDouble
(
const
char
*
p
,
char
**
endptr
=
nullptr
)
{
assert
(
p
!=
nullptr
);
return
(
double
)
strtod
(
p
,
endptr
);
}
#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