From e10565e5c218e78c84688c8a38f7e91ed83d9ee2 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 17 Apr 2020 15:04:33 +0200
Subject: [PATCH] Cleaning the code

---
 inc/lektor/commands.h |   6 +-
 src/commands.c        | 189 ++++++++++++++++--------------------------
 2 files changed, 76 insertions(+), 119 deletions(-)

diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h
index d533ec30..86d1e05b 100644
--- a/inc/lektor/commands.h
+++ b/inc/lektor/commands.h
@@ -39,8 +39,10 @@ bool command_crop(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr);
 bool command_move(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr);
 bool command_shuffle(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr);
 bool command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]);
-bool command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr);
-bool command_queue_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], long continuation);
+bool command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX],
+                    enum mpd_idle_flag *watch_mask_ptr);
+bool command_queue_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX],
+                        long continuation);
 
 /* The playlists */
 bool command_plt_create(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr);
diff --git a/src/commands.c b/src/commands.c
index 3dbd055d..d03489ed 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -126,7 +126,6 @@ command_status(struct lkt_state *srv, size_t c)
     }
 
     database_queue_current_kara(srv->db, NULL, &songid);
-
     win->get_elapsed(win, &elapsed);
     win->get_duration(win, &duration);
 
@@ -151,7 +150,6 @@ command_status(struct lkt_state *srv, size_t c)
                              queue_state.current < 0 ? -1 : queue_state.current - 1,
                              queue_state.length, elapsed, duration, songid);
     lkt_state_send(srv, c, out);
-
     return true;
 }
 
@@ -160,9 +158,7 @@ command_next(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mask_pt
 {
     *watch_mask_ptr |= MPD_IDLE_PLAYER;
     char filepath[PATH_MAX];
-    bool res = database_queue_next(db, filepath) &&
-               win->load_file(win, filepath);
-    return res;
+    return database_queue_next(db, filepath) && win->load_file(win, filepath);
 }
 
 bool
@@ -177,9 +173,7 @@ command_previous(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mas
 {
     *watch_mask_ptr |= MPD_IDLE_PLAYER;
     char filepath[PATH_MAX];
-    bool res = database_queue_prev(db, filepath) &&
-               win->load_file(win, filepath);
-    return res;
+    return database_queue_prev(db, filepath) && win->load_file(win, filepath);
 }
 
 static inline bool
@@ -187,24 +181,25 @@ __play_that_file(sqlite3 *db, struct lkt_win *win, int pos)
 {
     char filepath[PATH_MAX];
 
+    if (pos == 0)
+        return false;
+
     if (!database_queue_play(db, (int) pos)) {
         fprintf(stderr, " ! __play_that_file: command failed because of db interactions\n");
-        goto error;
+        return false;
     }
 
     if (!database_queue_get_current_file(db, filepath)) {
         fprintf(stderr, " ! __play_that_file: command failed because of get current\n");
-        goto error;
+        return false;
     }
 
     if (!win->load_file(win, filepath)) {
         fprintf(stderr, " ! __play_that_file: command failed because of get current\n");
-        goto error;
+        return false;
     }
 
     return true;
-error:
-    return false;
 }
 
 bool
@@ -213,103 +208,83 @@ command_play(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX],
 {
     *watch_mask_ptr |= MPD_IDLE_PLAYER;
     char *endptr;
-    long pos;
+    long pos = 0;
 
     /* Argument handle. */
 
-    if (args[0] == NULL)
-        pos = 1;
-    else {
-        pos = strtol(args[0], &endptr, 10);
+    pos = args[0] == NULL ? 1 : strtol(args[0], &endptr, 10);
 
-        if ((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN))
-            || (errno != 0 && pos == 0)) {
-            fprintf(stderr, " ! command_play: strtol failed: %s\n", strerror(errno));
-            goto error;
-        }
+    if ((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN))
+        || (errno != 0 && pos == 0)) {
+        fprintf(stderr, " ! command_play: strtol failed: %s\n", strerror(errno));
+        return false;
+    }
 
-        if (endptr == args[0]) {
-            fprintf(stderr, " . command_play: to digit found in string '%s'\n", args[1]);
-            goto error;
-        }
+    if (endptr == args[0]) {
+        fprintf(stderr, " . command_play: to digit found in string '%s'\n", args[1]);
+        return false;
     }
 
     /* Do the actual job here. */
 
     database_queue_stop(db);
 
-    if (win->window)
-        win->close(win);
-
-    if (!win->new(win)) {
+    if (!win->new (win)) {
         fprintf(stderr, " ! command_play: command failed because of null mpv ctx\n");
-        goto error;
+        return false;
     }
 
-    if (!__play_that_file(db, win, pos))
-        goto error;
-
-    return true;
-error:
-    return false;
+    return __play_that_file(db, win, pos);
 }
 
 bool
-command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr)
+command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX],
+               enum mpd_idle_flag *watch_mask_ptr)
 {
     *watch_mask_ptr |= MPD_IDLE_PLAYER;
     char *endptr;
-    int pos;
+    int pos = 0;
     long id;
 
     /* Argument handle. */
 
     if (args[0] == NULL)
-        goto error;
+        return false;
 
     id = strtol(args[0], &endptr, 10);
 
     if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN))
-            || (errno != 0 && id == 0)) {
+        || (errno != 0 && id == 0)) {
         fprintf(stderr, " ! command_playid: strtol failed: %s\n", strerror(errno));
-        goto error;
+        return false;
     }
 
     if (endptr == args[0]) {
         fprintf(stderr, " . command_playid: to digit found in string '%s'\n", args[1]);
-        goto error;
+        return false;
     }
 
     /* Do the work. */
 
     database_queue_stop(db);
 
-    if (win->window)
-        win->close(win);
-
-    if (!win->new(win))
-        goto error;
-
-    if (!database_queue_seekid(db, (int) id, &pos))
-        goto error;
-
-    if (!__play_that_file(db, win, pos))
-        goto error;
+    if (!win->new (win))
+        return false;
 
-error:
-    fprintf(stderr, " ! command_playid: Failed\n");
-    return false;
+    database_queue_seekid(db, (int) id, &pos);
+    return __play_that_file(db, win, pos);
 }
 
 
 bool
 command_stop(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mask_ptr)
 {
-    *watch_mask_ptr |= MPD_IDLE_PLAYER;
-    bool res = database_queue_stop(db);
-    if (res)
+    if (database_queue_stop(db)) {
         win->close(win);
-    return res;
+        *watch_mask_ptr |= MPD_IDLE_PLAYER;
+        return true;
+    }
+    return false;
 }
 
 bool
@@ -324,10 +299,8 @@ command_add(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX],
     *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
     struct lkt_uri_t uri;
     char *query = args[0];
-    bool ret, is_insert = false;
-    int priority = 1;   /* To be modified according to the command (insert or add) later */
+    int ret, priority = 1;  /* To be modified according to the command (insert or add) later (TODO) */
 
-    (void) is_insert;   // Don't know what insert looks like
     (void) win;         // No callbacks to the window for the moment
 
     if (!lkt_uri_from(&uri, query)) {
@@ -439,11 +412,8 @@ command_delid(sqlite3 *db, struct lkt_win *win, char *id_str, enum mpd_idle_flag
        pointer to reload the kara in the same position (but the kara won't be
        the same). */
     database_queue_current_kara(db, NULL, &uri);
-    if (id == (long) uri) {
-        fprintf(stderr, " . command_delid: Can't delete the currently playing kara\n");
+    if (id == (long) uri)
         return false;
-    }
-
     return database_queue_del_id(db, id);
 }
 
@@ -497,12 +467,10 @@ command_help(struct lkt_state *srv, size_t c)
 {
     struct lkt_message *out;
     int idx;
-
     out = lkt_message_new();
     idx = snprintf(out->data, LKT_MESSAGE_MAX, "HELP\n");
     out->data_len = idx;
     lkt_state_send(srv, c, out);
-
     return true;
 }
 
@@ -581,7 +549,6 @@ lkt_callback_send_row_v1(void *_args, int id, int id_len, const char *sql_row)
 {
     struct lkt_callback *args = (struct lkt_callback *) _args;
     struct lkt_message *out;
-
     out = lkt_message_new();
     out->data_len = snprintf(out->data, LKT_MESSAGE_MAX, "%*d %s\n", id_len, id, sql_row);
     lkt_state_send(args->srv, args->c, out);
@@ -608,7 +575,7 @@ lkt_callback_insert_v1(struct lkt_state *srv, size_t c, int id, int id_len, cons
 
 static bool
 __find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], long continuation,
-       enum lkt_find_action action, bool(*init)(sqlite3*, char*, char*, struct lkt_search*))
+       enum lkt_find_action action, bool(*init)(sqlite3 *, char *, char *, struct lkt_search *))
 {
     char rgx[PATH_MAX], *col_name, *mpd_tag;
     int count;
@@ -713,8 +680,8 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA
 }
 
 bool
-command_set_playback_option(struct lkt_state *srv, size_t c,
-                            enum lkt_playback_option opt, char *args[LKT_MESSAGE_MAX])
+command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playback_option opt,
+                            char *args[LKT_MESSAGE_MAX])
 {
     if (srv == NULL)
         return false;
@@ -844,23 +811,21 @@ command_plt_remove(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_
         return database_plt_remove(db, args[0]);
     }
 
-    else {
-        pos = strtol(args[1], &endptr, 10);
-
-        if ((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN))
-            || (errno != 0 && pos == 0)) {
-            fprintf(stderr, " ! command_plt_remove: strtol failed: %s\n", strerror(errno));
-            return false;
-        }
+    pos = strtol(args[1], &endptr, 10);
 
-        if (endptr == args[1]) {
-            fprintf(stderr, " . command_plt_remove: to digit found in string '%s'\n", args[1]);
-            return false;
-        }
+    if ((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN))
+        || (errno != 0 && pos == 0)) {
+        fprintf(stderr, " ! command_plt_remove: strtol failed: %s\n", strerror(errno));
+        return false;
+    }
 
-        *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
-        return database_plt_remove_pos(db, args[0], pos);
+    if (endptr == args[1]) {
+        fprintf(stderr, " . command_plt_remove: to digit found in string '%s'\n", args[1]);
+        return false;
     }
+
+    *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
+    return database_plt_remove_pos(db, args[0], pos);
 }
 
 bool
@@ -869,12 +834,12 @@ command_plt_clear(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_f
     if (args == NULL || args[0] == NULL)
         return false;
 
-    if (database_plt_clear(db, args[0])) {
-        *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
-        return true;
-    } else
+    if (!database_plt_clear(db, args[0]))
         return false;
 
+    *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
+    return true;
+
 }
 
 bool
@@ -883,59 +848,51 @@ command_plt_rename(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_
     if (args == NULL || args[0] == NULL || args[1] == NULL)
         return false;
 
-    if (database_plt_rename(db, args[0], args[1])) {
-        *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
-        return true;
-    } else
+    if (!database_plt_rename(db, args[0], args[1]))
         return false;
+
+    *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
+    return true;
 }
 
 bool
 command_plt_export(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr)
 {
-    int ret = false;
-
     if (args == NULL || args[0] == NULL || args[1] == NULL)
         return false;
 
     if (!database_attach(db, args[0], args[1]))
-        goto error;
+        return false;
 
     if (!database_plt_export(db, args[0]))
-        goto error;
+        return false;
 
     if (!database_detach(db, args[0]))
-        goto error;
+        return false;
 
     fprintf(stderr, " * Exported playlist %s with path '%s'\n", args[0], args[1]);
     *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
-    ret = true;
-error:
-    return ret;
+    return true;
 }
 
 bool
 command_plt_import(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr)
 {
-    int ret = false;
-
     if (args == NULL || args[0] == NULL || args[1] == NULL)
         return false;
 
     if (!database_attach(db, args[0], args[1]))
-        goto error;
+        return false;
 
     if (!database_plt_import(db, args[0]))
-        goto error;
+        return false;
 
     if (!database_detach(db, args[0]))
-        goto error;
+        return false;
 
     fprintf(stderr, " * Imported playlist %s with path '%s'\n", args[0], args[1]);
     *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
-    ret = true;
-error:
-    return ret;
+    return true;
 }
 
 bool
@@ -963,9 +920,7 @@ command_plt_add_uri(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle
 bool
 command_shuffle(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr)
 {
-    bool ret = database_queue_shuffle(db);
-
-    if (ret) {
+    if (database_queue_shuffle(db)) {
         *watch_mask_ptr |= MPD_IDLE_PLAYER;
         return true;
     }
@@ -1091,8 +1046,8 @@ command_user_add(sqlite3 *db, char *argv[LKT_MESSAGE_ARGS_MAX])
         return true;
     }
 
-    return false;
     fprintf(stderr, " . command_user_add: Failed to add user %s\n", argv[0]);
+    return false;
 }
 
 /* Stickers */
-- 
GitLab