From 9231f420c199a7c12cb8aad17bcca80a0aaca6ad Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 11 Aug 2015 22:34:22 +0200 Subject: protocol/ArgParser: overload as ParseCommandArg(), pass references --- src/command/OtherCommands.cxx | 4 ++-- src/command/OutputCommands.cxx | 6 +++--- src/command/PlayerCommands.cxx | 36 ++++++++++++++++------------------- src/command/PlaylistCommands.cxx | 8 +++----- src/command/QueueCommands.cxx | 41 ++++++++++++++++------------------------ src/command/TagCommands.cxx | 4 ++-- src/protocol/ArgParser.cxx | 20 ++++++++++---------- src/protocol/ArgParser.hxx | 8 ++++---- 8 files changed, 56 insertions(+), 71 deletions(-) diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index e5696e696..4fc7e56f6 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -309,7 +309,7 @@ CommandResult handle_setvol(Client &client, Request args) { unsigned level; - if (!check_unsigned(client, &level, args.front())) + if (!ParseCommandArg(client, level, args.front())) return CommandResult::ERROR; if (level > 100) { @@ -330,7 +330,7 @@ CommandResult handle_volume(Client &client, Request args) { int relative; - if (!check_int(client, &relative, args.front())) + if (!ParseCommandArg(client, relative, args.front())) return CommandResult::ERROR; if (relative < -100 || relative > 100) { diff --git a/src/command/OutputCommands.cxx b/src/command/OutputCommands.cxx index b562dc406..885144168 100644 --- a/src/command/OutputCommands.cxx +++ b/src/command/OutputCommands.cxx @@ -34,7 +34,7 @@ handle_enableoutput(Client &client, Request args) assert(args.size == 1); unsigned device; - if (!check_unsigned(client, &device, args.front())) + if (!ParseCommandArg(client, device, args.front())) return CommandResult::ERROR; if (!audio_output_enable_index(client.partition.outputs, device)) { @@ -52,7 +52,7 @@ handle_disableoutput(Client &client, Request args) assert(args.size == 1); unsigned device; - if (!check_unsigned(client, &device, args.front())) + if (!ParseCommandArg(client, device, args.front())) return CommandResult::ERROR; if (!audio_output_disable_index(client.partition.outputs, device)) { @@ -70,7 +70,7 @@ handle_toggleoutput(Client &client, Request args) assert(args.size == 1); unsigned device; - if (!check_unsigned(client, &device, args.front())) + if (!ParseCommandArg(client, device, args.front())) 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 c2a42bbf9..ac7fdecfd 100644 --- a/src/command/PlayerCommands.cxx +++ b/src/command/PlayerCommands.cxx @@ -61,9 +61,9 @@ CommandResult handle_play(Client &client, Request args) { int song = -1; - - if (!args.IsEmpty() && !check_int(client, &song, args.front())) + if (!args.IsEmpty() && !ParseCommandArg(client, song, args.front())) return CommandResult::ERROR; + PlaylistResult result = client.partition.PlayPosition(song); return print_playlist_result(client, result); } @@ -72,8 +72,7 @@ CommandResult handle_playid(Client &client, Request args) { int id = -1; - - if (!args.IsEmpty() && !check_int(client, &id, args.front())) + if (!args.IsEmpty() && !ParseCommandArg(client, id, args.front())) return CommandResult::ERROR; PlaylistResult result = client.partition.PlayId(id); @@ -99,7 +98,7 @@ handle_pause(Client &client, Request args) { if (!args.IsEmpty()) { bool pause_flag; - if (!check_bool(client, &pause_flag, args.front())) + if (!ParseCommandArg(client, pause_flag, args.front())) return CommandResult::ERROR; client.player_control.SetPause(pause_flag); @@ -250,7 +249,7 @@ CommandResult handle_repeat(Client &client, Request args) { bool status; - if (!check_bool(client, &status, args.front())) + if (!ParseCommandArg(client, status, args.front())) return CommandResult::ERROR; client.partition.SetRepeat(status); @@ -261,7 +260,7 @@ CommandResult handle_single(Client &client, Request args) { bool status; - if (!check_bool(client, &status, args.front())) + if (!ParseCommandArg(client, status, args.front())) return CommandResult::ERROR; client.partition.SetSingle(status); @@ -272,7 +271,7 @@ CommandResult handle_consume(Client &client, Request args) { bool status; - if (!check_bool(client, &status, args.front())) + if (!ParseCommandArg(client, status, args.front())) return CommandResult::ERROR; client.partition.SetConsume(status); @@ -283,7 +282,7 @@ CommandResult handle_random(Client &client, Request args) { bool status; - if (!check_bool(client, &status, args.front())) + if (!ParseCommandArg(client, status, args.front())) return CommandResult::ERROR; client.partition.SetRandom(status); @@ -304,7 +303,7 @@ handle_seek(Client &client, Request args) unsigned song; SongTime seek_time; - if (!check_unsigned(client, &song, args[0])) + if (!ParseCommandArg(client, song, args[0])) return CommandResult::ERROR; if (!ParseCommandArg(client, seek_time, args[1])) return CommandResult::ERROR; @@ -319,8 +318,7 @@ handle_seekid(Client &client, Request args) { unsigned id; SongTime seek_time; - - if (!check_unsigned(client, &id, args[0])) + if (!ParseCommandArg(client, id, args[0])) return CommandResult::ERROR; if (!ParseCommandArg(client, seek_time, args[1])) return CommandResult::ERROR; @@ -348,11 +346,10 @@ CommandResult handle_crossfade(Client &client, Request args) { unsigned xfade_time; - - if (!check_unsigned(client, &xfade_time, args.front())) + if (!ParseCommandArg(client, xfade_time, args.front())) return CommandResult::ERROR; - client.player_control.SetCrossFade(xfade_time); + client.player_control.SetCrossFade(xfade_time); return CommandResult::OK; } @@ -360,11 +357,10 @@ CommandResult handle_mixrampdb(Client &client, Request args) { float db; - - if (!check_float(client, &db, args.front())) + if (!ParseCommandArg(client, db, args.front())) return CommandResult::ERROR; - client.player_control.SetMixRampDb(db); + client.player_control.SetMixRampDb(db); return CommandResult::OK; } @@ -372,9 +368,9 @@ CommandResult handle_mixrampdelay(Client &client, Request args) { float delay_secs; - - if (!check_float(client, &delay_secs, args.front())) + if (!ParseCommandArg(client, delay_secs, args.front())) return CommandResult::ERROR; + client.player_control.SetMixRampDelay(delay_secs); return CommandResult::OK; diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index 56da71078..f6f610d7a 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -146,8 +146,7 @@ handle_playlistdelete(Client &client, Request args) { const char *const name = args[0]; unsigned from; - - if (!check_unsigned(client, &from, args[1])) + if (!ParseCommandArg(client, from, args[1])) return CommandResult::ERROR; Error error; @@ -161,10 +160,9 @@ handle_playlistmove(Client &client, Request args) { const char *const name = args.front(); unsigned from, to; - - if (!check_unsigned(client, &from, args[1])) + if (!ParseCommandArg(client, from, args[1])) return CommandResult::ERROR; - if (!check_unsigned(client, &to, args[2])) + if (!ParseCommandArg(client, to, args[2])) return CommandResult::ERROR; Error error; diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index c11af26bb..634510cc8 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -105,7 +105,7 @@ handle_addid(Client &client, Request args) if (args.size == 2) { unsigned to; - if (!check_unsigned(client, &to, args[1])) + if (!ParseCommandArg(client, to, args[1])) return CommandResult::ERROR; PlaylistResult result = client.partition.MoveId(added_id, to); if (result != PlaylistResult::SUCCESS) { @@ -155,7 +155,7 @@ CommandResult handle_rangeid(Client &client, Request args) { unsigned id; - if (!check_unsigned(client, &id, args.front())) + if (!ParseCommandArg(client, id, args.front())) return CommandResult::ERROR; SongTime start, end; @@ -188,8 +188,7 @@ CommandResult handle_deleteid(Client &client, Request args) { unsigned id; - - if (!check_unsigned(client, &id, args.front())) + if (!ParseCommandArg(client, id, args.front())) return CommandResult::ERROR; PlaylistResult result = client.partition.DeleteId(id); @@ -269,7 +268,7 @@ handle_playlistid(Client &client, Request args) { if (!args.IsEmpty()) { unsigned id; - if (!check_unsigned(client, &id, args.front())) + if (!ParseCommandArg(client, id, args.front())) return CommandResult::ERROR; bool ret = playlist_print_id(client, client.playlist, id); @@ -315,8 +314,7 @@ handle_prio(Client &client, Request args) { const char *const priority_string = args.shift(); unsigned priority; - - if (!check_unsigned(client, &priority, priority_string)) + if (!ParseCommandArg(client, priority, priority_string)) return CommandResult::ERROR; if (priority > 0xff) { @@ -346,8 +344,7 @@ handle_prioid(Client &client, Request args) { const char *const priority_string = args.shift(); unsigned priority; - - if (!check_unsigned(client, &priority, priority_string)) + if (!ParseCommandArg(client, priority, priority_string)) return CommandResult::ERROR; if (priority > 0xff) { @@ -358,7 +355,7 @@ handle_prioid(Client &client, Request args) for (const char *i : args) { unsigned song_id; - if (!check_unsigned(client, &song_id, i)) + if (!ParseCommandArg(client, song_id, i)) return CommandResult::ERROR; PlaylistResult result = @@ -376,9 +373,8 @@ handle_move(Client &client, Request args) RangeArg range; int to; - if (!ParseCommandArg(client, range, args[0])) - return CommandResult::ERROR; - if (!check_int(client, &to, args[1])) + if (!ParseCommandArg(client, range, args[0]) || + !ParseCommandArg(client, to, args[1])) return CommandResult::ERROR; PlaylistResult result = @@ -391,11 +387,10 @@ handle_moveid(Client &client, Request args) { unsigned id; int to; - - if (!check_unsigned(client, &id, args[0])) - return CommandResult::ERROR; - if (!check_int(client, &to, args[1])) + if (!ParseCommandArg(client, id, args[0]) || + !ParseCommandArg(client, to, args[1])) return CommandResult::ERROR; + PlaylistResult result = client.partition.MoveId(id, to); return print_playlist_result(client, result); } @@ -404,10 +399,8 @@ CommandResult handle_swap(Client &client, Request args) { unsigned song1, song2; - - if (!check_unsigned(client, &song1, args[0])) - return CommandResult::ERROR; - if (!check_unsigned(client, &song2, args[1])) + if (!ParseCommandArg(client, song1, args[0]) || + !ParseCommandArg(client, song2, args[1])) return CommandResult::ERROR; PlaylistResult result = @@ -419,10 +412,8 @@ CommandResult handle_swapid(Client &client, Request args) { unsigned id1, id2; - - if (!check_unsigned(client, &id1, args[0])) - return CommandResult::ERROR; - if (!check_unsigned(client, &id2, args[1])) + if (!ParseCommandArg(client, id1, args[0]) || + !ParseCommandArg(client, id2, args[1])) return CommandResult::ERROR; PlaylistResult result = client.partition.SwapIds(id1, id2); diff --git a/src/command/TagCommands.cxx b/src/command/TagCommands.cxx index 8997c84c3..aa8cf139d 100644 --- a/src/command/TagCommands.cxx +++ b/src/command/TagCommands.cxx @@ -32,7 +32,7 @@ CommandResult handle_addtagid(Client &client, Request args) { unsigned song_id; - if (!check_unsigned(client, &song_id, args.front())) + if (!ParseCommandArg(client, song_id, args.front())) return CommandResult::ERROR; const char *const tag_name = args[1]; @@ -57,7 +57,7 @@ CommandResult handle_cleartagid(Client &client, Request args) { unsigned song_id; - if (!check_unsigned(client, &song_id, args.front())) + if (!ParseCommandArg(client, song_id, args.front())) return CommandResult::ERROR; TagType tag_type = TAG_NUM_OF_ITEM_TYPES; diff --git a/src/protocol/ArgParser.cxx b/src/protocol/ArgParser.cxx index b290b7138..2c3e21f79 100644 --- a/src/protocol/ArgParser.cxx +++ b/src/protocol/ArgParser.cxx @@ -41,7 +41,7 @@ check_uint32(Client &client, uint32_t *dst, const char *s) } bool -check_int(Client &client, int *value_r, const char *s) +ParseCommandArg(Client &client, int &value_r, const char *s) { char *test; long value; @@ -60,7 +60,7 @@ check_int(Client &client, int *value_r, const char *s) return false; } - *value_r = (int)value; + value_r = (int)value; return true; } @@ -131,7 +131,7 @@ ParseCommandArg(Client &client, RangeArg &value_r, const char *s) } bool -check_unsigned(Client &client, unsigned *value_r, const char *s) +ParseCommandArg(Client &client, unsigned &value_r, const char *s) { unsigned long value; char *endptr; @@ -149,12 +149,12 @@ check_unsigned(Client &client, unsigned *value_r, const char *s) return false; } - *value_r = (unsigned)value; + value_r = (unsigned)value; return true; } bool -check_bool(Client &client, bool *value_r, const char *s) +ParseCommandArg(Client &client, bool &value_r, const char *s) { long value; char *endptr; @@ -166,12 +166,12 @@ check_bool(Client &client, bool *value_r, const char *s) return false; } - *value_r = !!value; + value_r = !!value; return true; } bool -check_float(Client &client, float *value_r, const char *s) +ParseCommandArg(Client &client, float &value_r, const char *s) { float value; char *endptr; @@ -183,7 +183,7 @@ check_float(Client &client, float *value_r, const char *s) return false; } - *value_r = value; + value_r = value; return true; } @@ -191,7 +191,7 @@ bool ParseCommandArg(Client &client, SongTime &value_r, const char *s) { float value; - bool success = check_float(client, &value, s) && value >= 0; + bool success = ParseCommandArg(client, value, s) && value >= 0; if (success) value_r = SongTime::FromS(value); @@ -202,7 +202,7 @@ bool ParseCommandArg(Client &client, SignedSongTime &value_r, const char *s) { float value; - bool success = check_float(client, &value, s); + bool success = ParseCommandArg(client, value, s); if (success) value_r = SignedSongTime::FromS(value); diff --git a/src/protocol/ArgParser.hxx b/src/protocol/ArgParser.hxx index 7866b88d5..d71894771 100644 --- a/src/protocol/ArgParser.hxx +++ b/src/protocol/ArgParser.hxx @@ -32,7 +32,7 @@ bool check_uint32(Client &client, uint32_t *dst, const char *s); bool -check_int(Client &client, int *value_r, const char *s); +ParseCommandArg(Client &client, int &value_r, const char *s); struct RangeArg { unsigned start, end; @@ -47,13 +47,13 @@ bool ParseCommandArg(Client &client, RangeArg &value_r, const char *s); bool -check_unsigned(Client &client, unsigned *value_r, const char *s); +ParseCommandArg(Client &client, unsigned &value_r, const char *s); bool -check_bool(Client &client, bool *value_r, const char *s); +ParseCommandArg(Client &client, bool &value_r, const char *s); bool -check_float(Client &client, float *value_r, const char *s); +ParseCommandArg(Client &client, float &value_r, const char *s); bool ParseCommandArg(Client &client, SongTime &value_r, const char *s); -- cgit v1.2.3