Commit 79241138 authored by Max Kellermann's avatar Max Kellermann

protocol/ArgParser: add function MakeArgError()

Replaces FormatProtocolError().
parent 78b0ff83
......@@ -25,6 +25,7 @@
#include "client/Response.hxx"
#include "fs/Traits.hxx"
#include "time/ChronoUtil.hxx"
#include "util/StringBuffer.hxx"
#include "util/UriUtil.hxx"
#include <fmt/format.h>
......
......@@ -20,6 +20,7 @@
#include "TimePrint.hxx"
#include "client/Response.hxx"
#include "time/ISO8601.hxx"
#include "util/StringBuffer.hxx"
#include <fmt/format.h>
......
......@@ -20,8 +20,6 @@
#ifndef MPD_ACK_H
#define MPD_ACK_H
#include "util/StringFormat.hxx"
#include <stdexcept>
#include <utility>
......@@ -54,13 +52,4 @@ public:
}
};
template<typename... Args>
static inline ProtocolError
FormatProtocolError(enum ack code, const char *fmt, Args&&... args) noexcept
{
return ProtocolError(code,
StringFormat<256>(fmt,
std::forward<Args>(args)...));
}
#endif
......@@ -25,14 +25,21 @@
#include <stdlib.h>
static inline ProtocolError
MakeArgError(const char *msg, const char *value) noexcept
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%s: %s", msg, value);
return ProtocolError(ACK_ERROR_ARG, buffer);
}
uint32_t
ParseCommandArgU32(const char *s)
{
char *test;
auto value = strtoul(s, &test, 10);
if (test == s || *test != '\0')
throw FormatProtocolError(ACK_ERROR_ARG,
"Integer expected: %s", s);
throw MakeArgError("Integer expected", s);
return value;
}
......@@ -43,12 +50,10 @@ ParseCommandArgInt(const char *s, int min_value, int max_value)
char *test;
auto value = strtol(s, &test, 10);
if (test == s || *test != '\0')
throw FormatProtocolError(ACK_ERROR_ARG,
"Integer expected: %s", s);
throw MakeArgError("Integer expected", s);
if (value < min_value || value > max_value)
throw FormatProtocolError(ACK_ERROR_ARG,
"Number too large: %s", s);
throw MakeArgError("Number too large", s);
return (int)value;
}
......@@ -67,8 +72,7 @@ ParseCommandArgRange(const char *s)
char *test, *test2;
auto value = strtol(s, &test, 10);
if (test == s || (*test != '\0' && *test != ':'))
throw FormatProtocolError(ACK_ERROR_ARG,
"Integer or range expected: %s", s);
throw MakeArgError("Integer or range expected", s);
if (value == -1 && *test == 0)
/* compatibility with older MPD versions: specifying
......@@ -76,12 +80,10 @@ ParseCommandArgRange(const char *s)
return RangeArg::All();
if (value < 0)
throw FormatProtocolError(ACK_ERROR_ARG,
"Number is negative: %s", s);
throw MakeArgError("Number is negative", s);
if (value > std::numeric_limits<int>::max())
throw FormatProtocolError(ACK_ERROR_ARG,
"Number too large: %s", s);
throw MakeArgError("Number too large", s);
RangeArg range;
range.start = (unsigned)value;
......@@ -89,21 +91,17 @@ ParseCommandArgRange(const char *s)
if (*test == ':') {
value = strtol(++test, &test2, 10);
if (*test2 != '\0')
throw FormatProtocolError(ACK_ERROR_ARG,
"Integer or range expected: %s",
s);
throw MakeArgError("Integer or range expected", s);
if (test == test2)
return RangeArg::OpenEnded(range.start);
if (value < 0)
throw FormatProtocolError(ACK_ERROR_ARG,
"Number is negative: %s", s);
throw MakeArgError("Number is negative", s);
if (value > std::numeric_limits<int>::max())
throw FormatProtocolError(ACK_ERROR_ARG,
"Number too large: %s", s);
throw MakeArgError("Number too large", s);
range.end = (unsigned)value;
} else {
......@@ -111,8 +109,7 @@ ParseCommandArgRange(const char *s)
}
if (!range.IsWellFormed())
throw FormatProtocolError(ACK_ERROR_ARG,
"Malformed range: %s", s);
throw MakeArgError("Malformed range", s);
return range;
}
......@@ -123,12 +120,10 @@ ParseCommandArgUnsigned(const char *s, unsigned max_value)
char *endptr;
auto value = strtoul(s, &endptr, 10);
if (endptr == s || *endptr != 0)
throw FormatProtocolError(ACK_ERROR_ARG,
"Integer expected: %s", s);
throw MakeArgError("Integer expected", s);
if (value > max_value)
throw FormatProtocolError(ACK_ERROR_ARG,
"Number too large: %s", s);
throw MakeArgError("Number too large", s);
return (unsigned)value;
}
......@@ -146,8 +141,7 @@ ParseCommandArgBool(const char *s)
char *endptr;
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);
throw MakeArgError("Boolean (0/1) expected", s);
return !!value;
}
......@@ -158,8 +152,7 @@ ParseCommandArgFloat(const char *s)
char *endptr;
auto value = ParseFloat(s, &endptr);
if (endptr == s || *endptr != 0)
throw FormatProtocolError(ACK_ERROR_ARG,
"Float expected: %s", s);
throw MakeArgError("Float expected", s);
return value;
}
......@@ -169,8 +162,7 @@ ParseCommandArgSongTime(const char *s)
{
auto value = ParseCommandArgFloat(s);
if (value < 0)
throw FormatProtocolError(ACK_ERROR_ARG,
"Negative value not allowed: %s", s);
throw MakeArgError("Negative value not allowed", s);
return SongTime::FromS(value);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment