diff options
author | Max Kellermann <max@duempel.org> | 2015-08-11 22:18:40 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-08-11 23:02:34 +0200 |
commit | b1480167be487d09ff46bb86ad02041fb28acff1 (patch) | |
tree | d819a825fb82c4258cc17d5f40793409f61600a1 /src/command | |
parent | e118e958f7f9b87054679ebf6102d6923abbba81 (diff) | |
download | mpd-b1480167be487d09ff46bb86ad02041fb28acff1.tar.gz mpd-b1480167be487d09ff46bb86ad02041fb28acff1.tar.xz mpd-b1480167be487d09ff46bb86ad02041fb28acff1.zip |
command/Request: add parser methods
Wrapper for protocol/ArgParser.cxx.
Diffstat (limited to '')
-rw-r--r-- | src/command/DatabaseCommands.cxx | 9 | ||||
-rw-r--r-- | src/command/OtherCommands.cxx | 9 | ||||
-rw-r--r-- | src/command/OutputCommands.cxx | 7 | ||||
-rw-r--r-- | src/command/PlayerCommands.cxx | 29 | ||||
-rw-r--r-- | src/command/PlaylistCommands.cxx | 14 | ||||
-rw-r--r-- | src/command/QueueCommands.cxx | 46 | ||||
-rw-r--r-- | src/command/Request.hxx | 40 | ||||
-rw-r--r-- | src/command/TagCommands.cxx | 5 |
8 files changed, 92 insertions, 67 deletions
diff --git a/src/command/DatabaseCommands.cxx b/src/command/DatabaseCommands.cxx index 99a200848..6e6f3399b 100644 --- a/src/command/DatabaseCommands.cxx +++ b/src/command/DatabaseCommands.cxx @@ -33,7 +33,6 @@ #include "util/Error.hxx" #include "SongFilter.hxx" #include "protocol/Result.hxx" -#include "protocol/ArgParser.hxx" #include "BulkEdit.hxx" #include <string.h> @@ -54,7 +53,7 @@ CommandResult handle_lsinfo2(Client &client, Request args) { /* default is root directory */ - const char *const uri = args.IsEmpty() ? "" : args.front(); + const auto uri = args.GetOptional(0, ""); const DatabaseSelection selection(uri, false); @@ -70,7 +69,7 @@ handle_match(Client &client, Request args, bool fold_case) { RangeArg window; if (args.size >= 2 && strcmp(args[args.size - 2], "window") == 0) { - if (!ParseCommandArg(client, window, args.back())) + if (!args.Parse(args.size - 1, window, client)) return CommandResult::ERROR; args.pop_back(); @@ -190,7 +189,7 @@ CommandResult handle_listall(Client &client, Request args) { /* default is root directory */ - const char *const uri = args.IsEmpty() ? "" : args.front(); + const auto uri = args.GetOptional(0, ""); Error error; return db_selection_print(client, DatabaseSelection(uri, true), @@ -275,7 +274,7 @@ CommandResult handle_listallinfo(Client &client, Request args) { /* default is root directory */ - const char *const uri = args.IsEmpty() ? "" : args.front(); + const auto uri = args.GetOptional(0, ""); Error error; return db_selection_print(client, DatabaseSelection(uri, true), diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index 816764a5d..2e20d8dd5 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -32,7 +32,6 @@ #include "tag/TagHandler.hxx" #include "TimePrint.hxx" #include "decoder/DecoderPrint.hxx" -#include "protocol/ArgParser.hxx" #include "protocol/Result.hxx" #include "ls.hxx" #include "mixer/Volume.hxx" @@ -116,7 +115,7 @@ CommandResult handle_listfiles(Client &client, Request args) { /* default is root directory */ - const char *const uri = args.IsEmpty() ? "" : args.front(); + const auto uri = args.GetOptional(0, ""); if (memcmp(uri, "file:///", 8) == 0) /* list local directory */ @@ -155,7 +154,7 @@ CommandResult handle_lsinfo(Client &client, Request args) { /* default is root directory */ - const char *const uri = args.IsEmpty() ? "" : args.front(); + const auto uri = args.GetOptional(0, ""); if (memcmp(uri, "file:///", 8) == 0) { /* print information about an arbitrary local file */ @@ -309,7 +308,7 @@ CommandResult handle_setvol(Client &client, Request args) { unsigned level; - if (!ParseCommandArg(client, level, args.front(), 100)) + if (!args.Parse(0, level, client, 100)) return CommandResult::ERROR; if (!volume_level_change(client.partition.outputs, level)) { @@ -325,7 +324,7 @@ CommandResult handle_volume(Client &client, Request args) { int relative; - if (!ParseCommandArg(client, relative, args.front(), -100, 100)) + if (!args.Parse(0, relative, client, -100, 100)) return CommandResult::ERROR; const int old_volume = volume_level_get(client.partition.outputs); diff --git a/src/command/OutputCommands.cxx b/src/command/OutputCommands.cxx index 885144168..39602aba8 100644 --- a/src/command/OutputCommands.cxx +++ b/src/command/OutputCommands.cxx @@ -23,7 +23,6 @@ #include "output/OutputPrint.hxx" #include "output/OutputCommand.hxx" #include "protocol/Result.hxx" -#include "protocol/ArgParser.hxx" #include "client/Client.hxx" #include "Partition.hxx" #include "util/ConstBuffer.hxx" @@ -34,7 +33,7 @@ handle_enableoutput(Client &client, Request args) assert(args.size == 1); unsigned device; - if (!ParseCommandArg(client, device, args.front())) + if (!args.Parse(0, device, client)) return CommandResult::ERROR; if (!audio_output_enable_index(client.partition.outputs, device)) { @@ -52,7 +51,7 @@ handle_disableoutput(Client &client, Request args) assert(args.size == 1); unsigned device; - if (!ParseCommandArg(client, device, args.front())) + if (!args.Parse(0, device, client)) return CommandResult::ERROR; if (!audio_output_disable_index(client.partition.outputs, device)) { @@ -70,7 +69,7 @@ handle_toggleoutput(Client &client, Request args) assert(args.size == 1); unsigned device; - if (!ParseCommandArg(client, device, args.front())) + if (!args.Parse(0, device, client)) return CommandResult::ERROR; if (!audio_output_toggle_index(client.partition.outputs, device)) { diff --git a/src/command/PlayerCommands.cxx b/src/command/PlayerCommands.cxx index ac7fdecfd..e0c0f61a8 100644 --- a/src/command/PlayerCommands.cxx +++ b/src/command/PlayerCommands.cxx @@ -28,7 +28,6 @@ #include "Partition.hxx" #include "Instance.hxx" #include "protocol/Result.hxx" -#include "protocol/ArgParser.hxx" #include "AudioFormat.hxx" #include "ReplayGainConfig.hxx" #include "util/ConstBuffer.hxx" @@ -61,7 +60,7 @@ CommandResult handle_play(Client &client, Request args) { int song = -1; - if (!args.IsEmpty() && !ParseCommandArg(client, song, args.front())) + if (!args.ParseOptional(0, song, client)) return CommandResult::ERROR; PlaylistResult result = client.partition.PlayPosition(song); @@ -72,7 +71,7 @@ CommandResult handle_playid(Client &client, Request args) { int id = -1; - if (!args.IsEmpty() && !ParseCommandArg(client, id, args.front())) + if (!args.ParseOptional(0, id, client)) return CommandResult::ERROR; PlaylistResult result = client.partition.PlayId(id); @@ -98,7 +97,7 @@ handle_pause(Client &client, Request args) { if (!args.IsEmpty()) { bool pause_flag; - if (!ParseCommandArg(client, pause_flag, args.front())) + if (!args.Parse(0, pause_flag, client)) return CommandResult::ERROR; client.player_control.SetPause(pause_flag); @@ -249,7 +248,7 @@ CommandResult handle_repeat(Client &client, Request args) { bool status; - if (!ParseCommandArg(client, status, args.front())) + if (!args.Parse(0, status, client)) return CommandResult::ERROR; client.partition.SetRepeat(status); @@ -260,7 +259,7 @@ CommandResult handle_single(Client &client, Request args) { bool status; - if (!ParseCommandArg(client, status, args.front())) + if (!args.Parse(0, status, client)) return CommandResult::ERROR; client.partition.SetSingle(status); @@ -271,7 +270,7 @@ CommandResult handle_consume(Client &client, Request args) { bool status; - if (!ParseCommandArg(client, status, args.front())) + if (!args.Parse(0, status, client)) return CommandResult::ERROR; client.partition.SetConsume(status); @@ -282,7 +281,7 @@ CommandResult handle_random(Client &client, Request args) { bool status; - if (!ParseCommandArg(client, status, args.front())) + if (!args.Parse(0, status, client)) return CommandResult::ERROR; client.partition.SetRandom(status); @@ -303,9 +302,9 @@ handle_seek(Client &client, Request args) unsigned song; SongTime seek_time; - if (!ParseCommandArg(client, song, args[0])) + if (!args.Parse(0, song, client)) return CommandResult::ERROR; - if (!ParseCommandArg(client, seek_time, args[1])) + if (!args.Parse(1, seek_time, client)) return CommandResult::ERROR; PlaylistResult result = @@ -318,9 +317,9 @@ handle_seekid(Client &client, Request args) { unsigned id; SongTime seek_time; - if (!ParseCommandArg(client, id, args[0])) + if (!args.Parse(0, id, client)) return CommandResult::ERROR; - if (!ParseCommandArg(client, seek_time, args[1])) + if (!args.Parse(1, seek_time, client)) return CommandResult::ERROR; PlaylistResult result = @@ -346,7 +345,7 @@ CommandResult handle_crossfade(Client &client, Request args) { unsigned xfade_time; - if (!ParseCommandArg(client, xfade_time, args.front())) + if (!args.Parse(0, xfade_time, client)) return CommandResult::ERROR; client.player_control.SetCrossFade(xfade_time); @@ -357,7 +356,7 @@ CommandResult handle_mixrampdb(Client &client, Request args) { float db; - if (!ParseCommandArg(client, db, args.front())) + if (!args.Parse(0, db, client)) return CommandResult::ERROR; client.player_control.SetMixRampDb(db); @@ -368,7 +367,7 @@ CommandResult handle_mixrampdelay(Client &client, Request args) { float delay_secs; - if (!ParseCommandArg(client, delay_secs, args.front())) + if (!args.Parse(0, delay_secs, client)) return CommandResult::ERROR; client.player_control.SetMixRampDelay(delay_secs); diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index f6f610d7a..0122cecbd 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -33,7 +33,6 @@ #include "queue/Playlist.hxx" #include "TimePrint.hxx" #include "client/Client.hxx" -#include "protocol/ArgParser.hxx" #include "protocol/Result.hxx" #include "ls.hxx" #include "Mapper.hxx" @@ -71,10 +70,8 @@ handle_save(Client &client, Request args) CommandResult handle_load(Client &client, Request args) { - RangeArg range; - if (args.size < 2) - range.SetAll(); - else if (!ParseCommandArg(client, range, args[1])) + RangeArg range = RangeArg::All(); + if (!args.ParseOptional(1, range, client)) return CommandResult::ERROR; const ScopeBulkEdit bulk_edit(client.partition); @@ -146,7 +143,7 @@ handle_playlistdelete(Client &client, Request args) { const char *const name = args[0]; unsigned from; - if (!ParseCommandArg(client, from, args[1])) + if (!args.Parse(1, from, client)) return CommandResult::ERROR; Error error; @@ -160,9 +157,8 @@ handle_playlistmove(Client &client, Request args) { const char *const name = args.front(); unsigned from, to; - if (!ParseCommandArg(client, from, args[1])) - return CommandResult::ERROR; - if (!ParseCommandArg(client, to, args[2])) + if (!args.Parse(1, from, client) || + !args.Parse(2, to, client)) return CommandResult::ERROR; Error error; diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 9a64ceb25..1413a68c9 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -30,7 +30,6 @@ #include "client/Client.hxx" #include "Partition.hxx" #include "BulkEdit.hxx" -#include "protocol/ArgParser.hxx" #include "protocol/Result.hxx" #include "ls.hxx" #include "util/ConstBuffer.hxx" @@ -105,8 +104,9 @@ handle_addid(Client &client, Request args) if (args.size == 2) { unsigned to; - if (!ParseCommandArg(client, to, args[1])) + if (!args.Parse(1, to, client)) return CommandResult::ERROR; + PlaylistResult result = client.partition.MoveId(added_id, to); if (result != PlaylistResult::SUCCESS) { CommandResult ret = @@ -155,7 +155,7 @@ CommandResult handle_rangeid(Client &client, Request args) { unsigned id; - if (!ParseCommandArg(client, id, args.front())) + if (!args.Parse(0, id, client)) return CommandResult::ERROR; SongTime start, end; @@ -177,7 +177,7 @@ CommandResult handle_delete(Client &client, Request args) { RangeArg range; - if (!ParseCommandArg(client, range, args.front())) + if (!args.Parse(0, range, client)) return CommandResult::ERROR; auto result = client.partition.DeleteRange(range.start, range.end); @@ -188,7 +188,7 @@ CommandResult handle_deleteid(Client &client, Request args) { unsigned id; - if (!ParseCommandArg(client, id, args.front())) + if (!args.Parse(0, id, client)) return CommandResult::ERROR; PlaylistResult result = client.partition.DeleteId(id); @@ -205,10 +205,8 @@ handle_playlist(Client &client, gcc_unused Request args) CommandResult handle_shuffle(gcc_unused Client &client, Request args) { - RangeArg range; - if (args.IsEmpty()) - range.SetAll(); - else if (!ParseCommandArg(client, range, args.front())) + RangeArg range = RangeArg::All(); + if (!args.ParseOptional(0, range, client)) return CommandResult::ERROR; client.partition.Shuffle(range.start, range.end); @@ -249,10 +247,8 @@ handle_plchangesposid(Client &client, Request args) CommandResult handle_playlistinfo(Client &client, Request args) { - RangeArg range; - if (args.IsEmpty()) - range.SetAll(); - else if (!ParseCommandArg(client, range, args.front())) + RangeArg range = RangeArg::All(); + if (!args.ParseOptional(0, range, client)) return CommandResult::ERROR; if (!playlist_print_info(client, client.playlist, @@ -268,7 +264,7 @@ handle_playlistid(Client &client, Request args) { if (!args.IsEmpty()) { unsigned id; - if (!ParseCommandArg(client, id, args.front())) + if (!args.Parse(0, id, client)) return CommandResult::ERROR; bool ret = playlist_print_id(client, client.playlist, id); @@ -312,9 +308,8 @@ handle_playlistsearch(Client &client, Request args) CommandResult handle_prio(Client &client, Request args) { - const char *const priority_string = args.shift(); unsigned priority; - if (!ParseCommandArg(client, priority, priority_string, 0xff)) + if (!args.ParseShift(0, priority, client, 0xff)) return CommandResult::ERROR; for (const char *i : args) { @@ -336,9 +331,8 @@ handle_prio(Client &client, Request args) CommandResult handle_prioid(Client &client, Request args) { - const char *const priority_string = args.shift(); unsigned priority; - if (!ParseCommandArg(client, priority, priority_string, 0xff)) + if (!args.ParseShift(0, priority, client, 0xff)) return CommandResult::ERROR; for (const char *i : args) { @@ -361,8 +355,8 @@ handle_move(Client &client, Request args) RangeArg range; int to; - if (!ParseCommandArg(client, range, args[0]) || - !ParseCommandArg(client, to, args[1])) + if (!args.Parse(0, range, client) || + !args.Parse(1, to, client)) return CommandResult::ERROR; PlaylistResult result = @@ -375,8 +369,8 @@ handle_moveid(Client &client, Request args) { unsigned id; int to; - if (!ParseCommandArg(client, id, args[0]) || - !ParseCommandArg(client, to, args[1])) + if (!args.Parse(0, id, client) || + !args.Parse(1, to, client)) return CommandResult::ERROR; PlaylistResult result = client.partition.MoveId(id, to); @@ -387,8 +381,8 @@ CommandResult handle_swap(Client &client, Request args) { unsigned song1, song2; - if (!ParseCommandArg(client, song1, args[0]) || - !ParseCommandArg(client, song2, args[1])) + if (!args.Parse(0, song1, client) || + !args.Parse(1, song2, client)) return CommandResult::ERROR; PlaylistResult result = @@ -400,8 +394,8 @@ CommandResult handle_swapid(Client &client, Request args) { unsigned id1, id2; - if (!ParseCommandArg(client, id1, args[0]) || - !ParseCommandArg(client, id2, args[1])) + if (!args.Parse(0, id1, client) || + !args.Parse(1, id2, client)) return CommandResult::ERROR; PlaylistResult result = client.partition.SwapIds(id1, id2); diff --git a/src/command/Request.hxx b/src/command/Request.hxx index 18fdff7f3..38b3c584c 100644 --- a/src/command/Request.hxx +++ b/src/command/Request.hxx @@ -21,14 +21,54 @@ #define MPD_REQUEST_HXX #include "check.h" +#include "protocol/ArgParser.hxx" #include "util/ConstBuffer.hxx" +#include <utility> + +#include <assert.h> + +class Client; + class Request : public ConstBuffer<const char *> { typedef ConstBuffer<const char *> Base; public: constexpr Request(const char *const*argv, size_type n) :Base(argv, n) {} + + constexpr const char *GetOptional(unsigned idx, + const char *default_value=nullptr) const { + return idx < size + ? data[idx] + : default_value; + } + + template<typename T, typename... Args> + bool Parse(unsigned idx, T &value_r, Client &client, + Args&&... args) { + assert(idx < size); + + return ParseCommandArg(client, value_r, data[idx], + std::forward<Args>(args)...); + } + + template<typename T, typename... Args> + bool ParseOptional(unsigned idx, T &value_r, Client &client, + Args&&... args) { + return idx >= size || + Parse(idx, value_r, client, + std::forward<Args>(args)...); + } + + template<typename T, typename... Args> + bool ParseShift(unsigned idx, T &value_r, Client &client, + Args&&... args) { + bool success = Parse(idx, value_r, client, + std::forward<Args>(args)...); + shift(); + return success; + } }; #endif diff --git a/src/command/TagCommands.cxx b/src/command/TagCommands.cxx index aa8cf139d..2690f39bf 100644 --- a/src/command/TagCommands.cxx +++ b/src/command/TagCommands.cxx @@ -22,7 +22,6 @@ #include "Request.hxx" #include "CommandError.hxx" #include "client/Client.hxx" -#include "protocol/ArgParser.hxx" #include "protocol/Result.hxx" #include "tag/Tag.hxx" #include "Partition.hxx" @@ -32,7 +31,7 @@ CommandResult handle_addtagid(Client &client, Request args) { unsigned song_id; - if (!ParseCommandArg(client, song_id, args.front())) + if (!args.Parse(0, song_id, client)) return CommandResult::ERROR; const char *const tag_name = args[1]; @@ -57,7 +56,7 @@ CommandResult handle_cleartagid(Client &client, Request args) { unsigned song_id; - if (!ParseCommandArg(client, song_id, args.front())) + if (!args.Parse(0, song_id, client)) return CommandResult::ERROR; TagType tag_type = TAG_NUM_OF_ITEM_TYPES; |