diff options
author | Max Kellermann <max@duempel.org> | 2015-08-11 21:35:52 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-08-11 22:43:10 +0200 |
commit | cbdc3194cc20aa5abcce8b53c44d46a96002ad3a (patch) | |
tree | 04164bedb2ca82200ebcb4f4a01f658a4f79b016 | |
parent | 993df0fd289d4426c633fd6e6d12bffe6061599d (diff) |
protocol/ArgParser: add struct RangeArg
-rw-r--r-- | src/command/DatabaseCommands.cxx | 10 | ||||
-rw-r--r-- | src/command/PlaylistCommands.cxx | 12 | ||||
-rw-r--r-- | src/command/QueueCommands.cxx | 41 | ||||
-rw-r--r-- | src/protocol/ArgParser.cxx | 13 | ||||
-rw-r--r-- | src/protocol/ArgParser.hxx | 12 | ||||
-rw-r--r-- | test/test_protocol.cxx | 23 |
6 files changed, 60 insertions, 51 deletions
diff --git a/src/command/DatabaseCommands.cxx b/src/command/DatabaseCommands.cxx index 66ca2c279..8999fb1ea 100644 --- a/src/command/DatabaseCommands.cxx +++ b/src/command/DatabaseCommands.cxx @@ -67,15 +67,15 @@ handle_lsinfo2(Client &client, ConstBuffer<const char *> args) static CommandResult handle_match(Client &client, ConstBuffer<const char *> args, bool fold_case) { - unsigned window_start = 0, window_end = std::numeric_limits<int>::max(); + RangeArg window; if (args.size >= 2 && strcmp(args[args.size - 2], "window") == 0) { - if (!check_range(client, &window_start, &window_end, - args.back())) + if (!ParseCommandArg(client, window, args.back())) return CommandResult::ERROR; args.pop_back(); args.pop_back(); - } + } else + window.SetAll(); SongFilter filter; if (!filter.Parse(args, fold_case)) { @@ -87,7 +87,7 @@ handle_match(Client &client, ConstBuffer<const char *> args, bool fold_case) Error error; return db_selection_print(client, selection, true, false, - window_start, window_end, error) + window.start, window.end, error) ? CommandResult::OK : print_error(client, error); } diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index 516726500..83ac482ea 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -70,12 +70,10 @@ handle_save(Client &client, ConstBuffer<const char *> args) CommandResult handle_load(Client &client, ConstBuffer<const char *> args) { - unsigned start_index, end_index; - - if (args.size < 2) { - start_index = 0; - end_index = unsigned(-1); - } else if (!check_range(client, &start_index, &end_index, args[1])) + RangeArg range; + if (args.size < 2) + range.SetAll(); + else if (!ParseCommandArg(client, range, args[1])) return CommandResult::ERROR; const ScopeBulkEdit bulk_edit(client.partition); @@ -83,7 +81,7 @@ handle_load(Client &client, ConstBuffer<const char *> args) Error error; const SongLoader loader(client); if (!playlist_open_into_queue(args.front(), - start_index, end_index, + range.start, range.end, client.playlist, client.player_control, loader, error)) return print_error(client, error); diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 561ef1027..5e051fc91 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -175,12 +175,11 @@ handle_rangeid(Client &client, ConstBuffer<const char *> args) CommandResult handle_delete(Client &client, ConstBuffer<const char *> args) { - unsigned start, end; - - if (!check_range(client, &start, &end, args.front())) + RangeArg range; + if (!ParseCommandArg(client, range, args.front())) return CommandResult::ERROR; - PlaylistResult result = client.partition.DeleteRange(start, end); + auto result = client.partition.DeleteRange(range.start, range.end); return print_playlist_result(client, result); } @@ -206,11 +205,13 @@ handle_playlist(Client &client, gcc_unused ConstBuffer<const char *> args) CommandResult handle_shuffle(gcc_unused Client &client, ConstBuffer<const char *> args) { - unsigned start = 0, end = client.playlist.queue.GetLength(); - if (args.size == 1 && !check_range(client, &start, &end, args.front())) + RangeArg range; + if (args.IsEmpty()) + range.SetAll(); + else if (!ParseCommandArg(client, range, args.front())) return CommandResult::ERROR; - client.partition.Shuffle(start, end); + client.partition.Shuffle(range.start, range.end); return CommandResult::OK; } @@ -248,12 +249,14 @@ handle_plchangesposid(Client &client, ConstBuffer<const char *> args) CommandResult handle_playlistinfo(Client &client, ConstBuffer<const char *> args) { - unsigned start = 0, end = std::numeric_limits<unsigned>::max(); - - if (args.size == 1 && !check_range(client, &start, &end, args.front())) + RangeArg range; + if (args.IsEmpty()) + range.SetAll(); + else if (!ParseCommandArg(client, range, args.front())) return CommandResult::ERROR; - if (!playlist_print_info(client, client.playlist, start, end)) + if (!playlist_print_info(client, client.playlist, + range.start, range.end)) return print_playlist_result(client, PlaylistResult::BAD_RANGE); @@ -322,14 +325,14 @@ handle_prio(Client &client, ConstBuffer<const char *> args) } for (const char *i : args) { - unsigned start_position, end_position; - if (!check_range(client, &start_position, &end_position, i)) + RangeArg range; + if (!ParseCommandArg(client, range, i)) return CommandResult::ERROR; PlaylistResult result = - client.partition.SetPriorityRange(start_position, - end_position, - priority); + client.partition.SetPriorityRange(range.start, + range.end, + priority); if (result != PlaylistResult::SUCCESS) return print_playlist_result(client, result); } @@ -369,16 +372,16 @@ handle_prioid(Client &client, ConstBuffer<const char *> args) CommandResult handle_move(Client &client, ConstBuffer<const char *> args) { - unsigned start, end; + RangeArg range; int to; - if (!check_range(client, &start, &end, args[0])) + if (!ParseCommandArg(client, range, args[0])) return CommandResult::ERROR; if (!check_int(client, &to, args[1])) return CommandResult::ERROR; PlaylistResult result = - client.partition.MoveRange(start, end, to); + client.partition.MoveRange(range.start, range.end, to); return print_playlist_result(client, result); } diff --git a/src/protocol/ArgParser.cxx b/src/protocol/ArgParser.cxx index b8837eeb4..b290b7138 100644 --- a/src/protocol/ArgParser.cxx +++ b/src/protocol/ArgParser.cxx @@ -65,8 +65,7 @@ check_int(Client &client, int *value_r, const char *s) } bool -check_range(Client &client, unsigned *value_r1, unsigned *value_r2, - const char *s) +ParseCommandArg(Client &client, RangeArg &value_r, const char *s) { char *test, *test2; long value; @@ -81,8 +80,8 @@ check_range(Client &client, unsigned *value_r1, unsigned *value_r2, if (value == -1 && *test == 0) { /* compatibility with older MPD versions: specifying "-1" makes MPD display the whole list */ - *value_r1 = 0; - *value_r2 = std::numeric_limits<int>::max(); + value_r.start = 0; + value_r.end = std::numeric_limits<int>::max(); return true; } @@ -98,7 +97,7 @@ check_range(Client &client, unsigned *value_r1, unsigned *value_r2, return false; } - *value_r1 = (unsigned)value; + value_r.start = (unsigned)value; if (*test == ':') { value = strtol(++test, &test2, 10); @@ -123,9 +122,9 @@ check_range(Client &client, unsigned *value_r1, unsigned *value_r2, return false; } - *value_r2 = (unsigned)value; + value_r.end = (unsigned)value; } else { - *value_r2 = (unsigned)value + 1; + value_r.end = (unsigned)value + 1; } return true; diff --git a/src/protocol/ArgParser.hxx b/src/protocol/ArgParser.hxx index 7df71d30b..7866b88d5 100644 --- a/src/protocol/ArgParser.hxx +++ b/src/protocol/ArgParser.hxx @@ -34,9 +34,17 @@ check_uint32(Client &client, uint32_t *dst, const char *s); bool check_int(Client &client, int *value_r, const char *s); +struct RangeArg { + unsigned start, end; + + void SetAll() { + start = 0; + end = unsigned(-1); + } +}; + bool -check_range(Client &client, unsigned *value_r1, unsigned *value_r2, - const char *s); +ParseCommandArg(Client &client, RangeArg &value_r, const char *s); bool check_unsigned(Client &client, unsigned *value_r, const char *s); diff --git a/test/test_protocol.cxx b/test/test_protocol.cxx index fb35cbc66..d5b60323a 100644 --- a/test/test_protocol.cxx +++ b/test/test_protocol.cxx @@ -32,21 +32,22 @@ void ArgParserTest::TestRange() { Client &client = *(Client *)nullptr; - unsigned a, b; - CPPUNIT_ASSERT(check_range(client, &a, &b, "1")); - CPPUNIT_ASSERT_EQUAL(1u, a); - CPPUNIT_ASSERT_EQUAL(2u, b); + RangeArg range; - CPPUNIT_ASSERT(check_range(client, &a, &b, "1:5")); - CPPUNIT_ASSERT_EQUAL(1u, a); - CPPUNIT_ASSERT_EQUAL(5u, b); + CPPUNIT_ASSERT(ParseCommandArg(client, range, "1")); + CPPUNIT_ASSERT_EQUAL(1u, range.start); + CPPUNIT_ASSERT_EQUAL(2u, range.end); - CPPUNIT_ASSERT(check_range(client, &a, &b, "1:")); - CPPUNIT_ASSERT_EQUAL(1u, a); - CPPUNIT_ASSERT(b >= 999999u); + CPPUNIT_ASSERT(ParseCommandArg(client, range, "1:5")); + CPPUNIT_ASSERT_EQUAL(1u, range.start); + CPPUNIT_ASSERT_EQUAL(5u, range.end); - CPPUNIT_ASSERT(!check_range(client, &a, &b, "-2")); + CPPUNIT_ASSERT(ParseCommandArg(client, range, "1:")); + CPPUNIT_ASSERT_EQUAL(1u, range.start); + CPPUNIT_ASSERT(range.end >= 999999u); + + CPPUNIT_ASSERT(!ParseCommandArg(client, range, "-2")); CPPUNIT_ASSERT_EQUAL(ACK_ERROR_ARG, last_error); } |