2012-02-14 19:13:04 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2012-02-14 19:13:04 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "ArgParser.hxx"
|
2015-12-18 09:50:48 +01:00
|
|
|
#include "Ack.hxx"
|
2014-08-27 19:11:55 +02:00
|
|
|
#include "Chrono.hxx"
|
2018-02-09 20:27:00 +01:00
|
|
|
#include "util/NumberParser.hxx"
|
2012-02-14 19:13:04 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
uint32_t
|
|
|
|
ParseCommandArgU32(const char *s)
|
2012-02-14 19:13:04 +01:00
|
|
|
{
|
|
|
|
char *test;
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = strtoul(s, &test, 10);
|
|
|
|
if (test == s || *test != '\0')
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Integer expected: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return value;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
int
|
|
|
|
ParseCommandArgInt(const char *s, int min_value, int max_value)
|
2012-02-14 19:13:04 +01:00
|
|
|
{
|
|
|
|
char *test;
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = strtol(s, &test, 10);
|
|
|
|
if (test == s || *test != '\0')
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Integer expected: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
if (value < min_value || value > max_value)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Number too large: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return (int)value;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
int
|
|
|
|
ParseCommandArgInt(const char *s)
|
2015-08-11 22:57:06 +02:00
|
|
|
{
|
2015-12-18 09:50:48 +01:00
|
|
|
return ParseCommandArgInt(s,
|
|
|
|
std::numeric_limits<int>::min(),
|
|
|
|
std::numeric_limits<int>::max());
|
2015-08-11 22:57:06 +02:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
RangeArg
|
|
|
|
ParseCommandArgRange(const char *s)
|
2012-02-14 19:13:04 +01:00
|
|
|
{
|
|
|
|
char *test, *test2;
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = strtol(s, &test, 10);
|
|
|
|
if (test == s || (*test != '\0' && *test != ':'))
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Integer or range expected: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
if (value == -1 && *test == 0)
|
2012-02-14 19:13:04 +01:00
|
|
|
/* compatibility with older MPD versions: specifying
|
|
|
|
"-1" makes MPD display the whole list */
|
2015-12-18 09:50:48 +01:00
|
|
|
return RangeArg::All();
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
if (value < 0)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Number is negative: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2016-03-07 14:30:43 +01:00
|
|
|
if (value > std::numeric_limits<int>::max())
|
2015-12-18 09:50:48 +01:00
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Number too large: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
RangeArg range;
|
|
|
|
range.start = (unsigned)value;
|
2012-02-14 19:13:04 +01:00
|
|
|
|
|
|
|
if (*test == ':') {
|
|
|
|
value = strtol(++test, &test2, 10);
|
2015-12-18 09:50:48 +01:00
|
|
|
if (*test2 != '\0')
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Integer or range expected: %s",
|
|
|
|
s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
|
|
|
if (test == test2)
|
2014-09-04 14:38:55 +02:00
|
|
|
value = std::numeric_limits<int>::max();
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
if (value < 0)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Number is negative: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
|
|
|
|
2016-03-07 14:30:43 +01:00
|
|
|
if (value > std::numeric_limits<int>::max())
|
2015-12-18 09:50:48 +01:00
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Number too large: %s", s);
|
2013-10-17 21:53:19 +02:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
range.end = (unsigned)value;
|
2012-02-14 19:13:04 +01:00
|
|
|
} else {
|
2015-12-18 09:50:48 +01:00
|
|
|
range.end = (unsigned)value + 1;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return range;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
unsigned
|
|
|
|
ParseCommandArgUnsigned(const char *s, unsigned max_value)
|
2012-02-14 19:13:04 +01:00
|
|
|
{
|
|
|
|
char *endptr;
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = strtoul(s, &endptr, 10);
|
|
|
|
if (endptr == s || *endptr != 0)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Integer expected: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
if (value > max_value)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Number too large: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return (unsigned)value;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
unsigned
|
|
|
|
ParseCommandArgUnsigned(const char *s)
|
2015-08-11 22:57:06 +02:00
|
|
|
{
|
2015-12-18 09:50:48 +01:00
|
|
|
return ParseCommandArgUnsigned(s,
|
|
|
|
std::numeric_limits<unsigned>::max());
|
2015-08-11 22:57:06 +02:00
|
|
|
}
|
|
|
|
|
2012-02-14 19:13:04 +01:00
|
|
|
bool
|
2015-12-18 09:50:48 +01:00
|
|
|
ParseCommandArgBool(const char *s)
|
2012-02-14 19:13:04 +01:00
|
|
|
{
|
|
|
|
char *endptr;
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = strtol(s, &endptr, 10);
|
|
|
|
if (endptr == s || *endptr != 0 || (value != 0 && value != 1))
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Boolean (0/1) expected: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return !!value;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
float
|
|
|
|
ParseCommandArgFloat(const char *s)
|
2012-02-14 19:13:04 +01:00
|
|
|
{
|
|
|
|
char *endptr;
|
2018-02-09 20:27:00 +01:00
|
|
|
auto value = ParseFloat(s, &endptr);
|
2015-12-18 09:50:48 +01:00
|
|
|
if (endptr == s || *endptr != 0)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Float expected: %s", s);
|
2012-02-14 19:13:04 +01:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return value;
|
2012-02-14 19:13:04 +01:00
|
|
|
}
|
2014-08-27 19:11:55 +02:00
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
SongTime
|
|
|
|
ParseCommandArgSongTime(const char *s)
|
2014-08-27 19:11:55 +02:00
|
|
|
{
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = ParseCommandArgFloat(s);
|
2018-03-04 11:46:11 +01:00
|
|
|
if (value < 0)
|
|
|
|
throw FormatProtocolError(ACK_ERROR_ARG,
|
|
|
|
"Negative value not allowed: %s", s);
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
return SongTime::FromS(value);
|
2014-08-27 19:11:55 +02:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:50:48 +01:00
|
|
|
SignedSongTime
|
|
|
|
ParseCommandArgSignedSongTime(const char *s)
|
2014-08-27 19:11:55 +02:00
|
|
|
{
|
2015-12-18 09:50:48 +01:00
|
|
|
auto value = ParseCommandArgFloat(s);
|
|
|
|
return SongTime::FromS(value);
|
2014-08-27 19:11:55 +02:00
|
|
|
}
|