From 5e4e5844a1ad7b1439397efa8b8216d5f9196c76 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Mon, 4 May 2020 15:23:50 +0200 Subject: [PATCH] Less code --- inc/lektor/commands.h | 8 ++++---- inc/lektor/database.h | 5 ++++- src/commands.c | 28 +++++++++++----------------- src/database/find.c | 12 ++++++------ 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h index 47c35a2d..9c413663 100644 --- a/inc/lektor/commands.h +++ b/inc/lektor/commands.h @@ -61,10 +61,10 @@ bool command_noidle(struct lkt_state *srv, size_t c); /* Find commands */ enum lkt_find_action { LKT_FND_ACT_NONE = 0, - LKT_FND_ACT_RESPOND = 1, - LKT_FND_ACT_PRINT = 2, - LKT_FND_ACT_ENQUEUE = 3, - LKT_FND_ACT_ADD = 4, + LKT_FND_ACT_RESPOND = (1 << 1), + LKT_FND_ACT_PRINT = (1 << 2), + LKT_FND_ACT_ENQUEUE = (1 << 3), + LKT_FND_ACT_ADD = (1 << 4), }; /* Find and send karas in the db that match the search expression */ diff --git a/inc/lektor/database.h b/inc/lektor/database.h index 559becbb..24cff874 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -99,7 +99,10 @@ struct lkt_search { int qu_priority; /* Can be used... where the priority is needed! */ int st_value; /* The value of a sticker */ }; - int st_uri; /* URI of a sticker */ + union { + int st_uri; /* URI of a sticker */ + }; + int must_init; /* Skip the init phase ? */ struct lkt_uri *qu_uri; /* Kara uri for a queue init function */ }; diff --git a/src/commands.c b/src/commands.c index 241e7c5a..161e89e4 100644 --- a/src/commands.c +++ b/src/commands.c @@ -394,7 +394,7 @@ lkt_callback_send_row_v2(struct lkt_state *srv, size_t c, int id, int id_len, co bool command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], long continuation, - enum lkt_find_action action, bool(*init)(volatile sqlite3 *, char *, char *, struct lkt_search *)) + enum lkt_find_action action, bool(*init)(volatile sqlite3 *, char *, char *, struct lkt_search *)) { char rgx[PATH_MAX], *col_name, *mpd_tag; int count; @@ -405,6 +405,7 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA .c = c, .qu_uri = &uri, .continuation = continuation, + .must_init = (continuation == 0), .msg_count = lkt_remaining_msg(srv, c) - 3, /* Reserve slots for OK/ACK and continue: */ .init = NULL, }; @@ -421,31 +422,24 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA search.call = NULL; break; case LKT_FND_ACT_ENQUEUE: - if (!continuation) { - /* Begin */ - RETURN_UNLESS(database_queue_state(srv->db, &queue), "Failed to get the status of the queue", false); - search.continuation = queue.current + 1; - } - search.call = (void(*)(void)) lkt_callback_send_row_v2; - search.init = (void(*)(void)) database_queue_add_uri; search.qu_priority = 5; - srv->mpd_idle_events |= MPD_IDLE_PLAYLIST; break; case LKT_FND_ACT_ADD: - if (!continuation) { - /* Begin */ - RETURN_UNLESS(database_queue_state(srv->db, &queue), "Failed to get the status of the queue", false); - search.continuation = queue.current + 1; - } - search.call = (void(*)(void)) lkt_callback_send_row_v2; - search.init = (void(*)(void)) database_queue_add_uri; search.qu_priority = 1; - srv->mpd_idle_events |= MPD_IDLE_PLAYLIST; break; default: return false; } + /* Begin, when we will prints the queue after a modification */ + if (action & (LKT_FND_ACT_ADD | LKT_FND_ACT_ENQUEUE) && ! continuation) { + search.call = (void(*)(void)) lkt_callback_send_row_v2; + search.init = (void(*)(void)) database_queue_add_uri; + RETURN_UNLESS(database_queue_state(srv->db, &queue), "Failed to get the status of the queue", false); + search.continuation = queue.current + 1; + srv->mpd_idle_events |= MPD_IDLE_PLAYLIST; + } + /* Select the right column */ mpd_tag = cmd_args[0]; if (!strcasecmp("any", mpd_tag) || !strcasecmp("all", mpd_tag) || !strcasecmp("query", mpd_tag) || diff --git a/src/database/find.c b/src/database/find.c index 63753ac6..5daba500 100644 --- a/src/database/find.c +++ b/src/database/find.c @@ -22,7 +22,7 @@ database_search_database_init(volatile sqlite3 *db, char *col_name, char *rgx, s ret->type = lkt_search_database; /* Init */ - if (! ret->continuation && ret->init && + if (ret->must_init && ret->init && ! ((lkt_search_init_add_func) ret->init)(db, ret->qu_uri, ret->qu_priority)) { LOG_ERROR_SCT("DB", "%s", "Failed to init search query, init func failed"); return false; @@ -53,10 +53,10 @@ database_search_sticker_init(volatile sqlite3 *db, char *type, char *name, struc ") AS sts" "ON sts.sticker = 'stickers'.id"; static const char *SQL_one_type = - "SELECT name, 'stickers.%s'.id, value " - "FROM 'stickers' " - "LEFT OUTER JOIN 'stickers' " - "ON 'stickers'.id = 'stickers.%s'.sticker"; + "SELECT name, 'stickers.%s'.id, value " + "FROM 'stickers' " + "LEFT OUTER JOIN 'stickers' " + "ON 'stickers'.id = 'stickers.%s'.sticker"; static const char *SQL_check_name = " AND name = ?;"; char SQL[LKT_MAX_SQLITE_STATEMENT]; @@ -91,7 +91,7 @@ database_search_queue_init(volatile sqlite3 *db, char *col_name, char *rgx, stru ret->type = lkt_search_queue; /* Init */ - if (! ret->continuation && ret->init && + if (ret->must_init && ret->init && ! ((lkt_search_init_add_func) ret->init)(db, ret->qu_uri, ret->qu_priority)) { LOG_ERROR_SCT("DB", "%s", "Failed to init search query, called to init func failed"); return false; -- GitLab