Skip to content
Extraits de code Groupes Projets
Vérifiée Valider ce6f1bf2 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Prefere the use of the strtol macro

parent 8c6f729b
Branches
Aucune étiquette associée trouvée
1 requête de fusion!71Resolve "Performance of search commands"
...@@ -177,18 +177,16 @@ bool ...@@ -177,18 +177,16 @@ bool
command_play(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr) command_play(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr)
{ {
*watch_mask_ptr |= MPD_IDLE_PLAYER; *watch_mask_ptr |= MPD_IDLE_PLAYER;
char *endptr; char *endptr, err;
long pos = 0; long pos = 1;
/* Argument handle. */ /* Argument handle. */
if (!args[0]) {
pos = args[0] == NULL ? 1 : strtol(args[0], &endptr, 10); STRTOL(pos, args[0], endptr, err);
RETURN_IF((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN)) || (errno != 0 && pos == 0), RETURN_IF(err, "STRTOL failed", false);
"Failed: strtol", false); }
RETURN_IF(endptr == args[0], "No digit found", false);
/* Do the actual job here. */ /* Do the actual job here. */
database_queue_stop(db); database_queue_stop(db);
RETURN_UNLESS(win->new (win), "Can't create window", false); RETURN_UNLESS(win->new (win), "Can't create window", false);
return __play_that_file(db, win, pos); return __play_that_file(db, win, pos);
...@@ -198,20 +196,16 @@ bool ...@@ -198,20 +196,16 @@ bool
command_playid(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr) command_playid(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr)
{ {
*watch_mask_ptr |= MPD_IDLE_PLAYER; *watch_mask_ptr |= MPD_IDLE_PLAYER;
char *endptr; char *endptr, err;
int pos = 0; int pos = 0;
long id; long id;
/* Argument handle. */ /* Argument handle. */
RETURN_IF(args[0] == NULL, "Invalid argument", false); RETURN_IF(args[0] == NULL, "Invalid argument", false);
id = strtol(args[0], &endptr, 10); STRTOL(id, args[0], endptr, err);
RETURN_IF((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0), RETURN_IF(err, "STRTOL failed", false);
"Failed: strtol", false);
RETURN_IF(endptr == args[0], "No digit found", false);
/* Do the work. */ /* Do the work. */
database_queue_stop(db); database_queue_stop(db);
RETURN_UNLESS(win->new (win), "Can't create window", false); RETURN_UNLESS(win->new (win), "Can't create window", false);
database_queue_seekid(db, (int) id, &pos); database_queue_seekid(db, (int) id, &pos);
...@@ -249,14 +243,12 @@ command_addid(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ ...@@ -249,14 +243,12 @@ command_addid(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_
UNUSED(win); UNUSED(win);
RETURN_UNLESS(args, "Invalid argument", false); RETURN_UNLESS(args, "Invalid argument", false);
long id; long id;
char *endptr, *id_str = args[0]; char *endptr = NULL, err = 0;
struct lkt_uri uri = { .type = uri_id }; struct lkt_uri uri = { .type = uri_id };
errno = 0;
*watch_mask_ptr |= MPD_IDLE_PLAYLIST; *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
id = strtol(id_str, &endptr, 10); STRTOL(id, args[0], endptr, err);
RETURN_IF((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0), "Failed: strtol", false); RETURN_IF(err, "STRTOL failed", false);
uri.value = (void *) (size_t) id; uri.value = (void *) (size_t) id;
RETURN_IF(endptr == args[0], "No digit found", false);
return database_queue_add_uri(db, &uri, 1); return database_queue_add_uri(db, &uri, 1);
} }
...@@ -279,15 +271,12 @@ command_delid(volatile sqlite3 *db, struct lkt_win *win, char *id_str, mpd_idle_ ...@@ -279,15 +271,12 @@ command_delid(volatile sqlite3 *db, struct lkt_win *win, char *id_str, mpd_idle_
{ {
UNUSED(win); UNUSED(win);
long id; long id;
char *endptr; char *endptr = NULL, err = 0;
int uri = 0; int uri = 0;
errno = 0;
*watch_mask_ptr |= MPD_IDLE_PLAYLIST; *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
id = strtol(id_str, &endptr, 10); STRTOL(id, id_str, endptr, err);
RETURN_IF((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0), RETURN_IF(err, "STRTOL failed", false);
"Failed: strtol", false);
RETURN_IF(endptr == id_str, "No digit found", false);
/* If one day we allow suppression of the current kara, will need the `win` /* If one day we allow suppression of the current kara, will need the `win`
pointer to reload the kara in the same position (but the kara won't be pointer to reload the kara in the same position (but the kara won't be
...@@ -301,25 +290,18 @@ bool ...@@ -301,25 +290,18 @@ bool
command_move(volatile sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr) command_move(volatile sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr)
{ {
long from, to; long from, to;
char *endptr; char *endptr, err;
RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false);
errno = 0;
*watch_mask_ptr |= MPD_IDLE_PLAYLIST; *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
/* First argument: from */ /* First argument: from */
STRTOL(from, args[0], endptr, err);
from = strtol(args[0], &endptr, 10); RETURN_IF(err, "STRTOL failed", false);
RETURN_IF((errno == ERANGE && (from == LONG_MAX || from == LONG_MIN)) || (errno != 0 && from == 0),
"Failed: strtol", false);
RETURN_IF(endptr == args[0], "No digit found", false);
/* Second argument: to */ /* Second argument: to */
STRTOL(to, args[1], endptr, err);
to = strtol(args[1], &endptr, 10); RETURN_IF(err, "STRTOL failed", false);
RETURN_IF((errno == ERANGE && (to == LONG_MAX || to == LONG_MIN)) || (errno != 0 && to == 0),
"Failed: strtol", false);
RETURN_IF(endptr == args[0], "No digit found", false);
return database_queue_move(db, from, to); return database_queue_move(db, from, to);
} }
...@@ -516,18 +498,15 @@ command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playback_o ...@@ -516,18 +498,15 @@ command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playback_o
{ {
RETURN_UNLESS(srv, "Invalid argument", false); RETURN_UNLESS(srv, "Invalid argument", false);
UNUSED(c); UNUSED(c);
long val, ret = false; long val;
char *endptr; char *endptr, ret = false;
struct lkt_win *win = &srv->win; struct lkt_win *win = &srv->win;
if (args == NULL || args[0] == NULL) if (args == NULL || args[0] == NULL)
val = 0; val = 0;
else { else {
errno = 0; STRTOL(val, args[0], endptr, ret);
val = strtol(args[0], &endptr, 10); RETURN_IF(ret, "STRTOL failed", false);
RETURN_IF((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0),
"Failed: strtol", false);
RETURN_IF(endptr == args[0], "No digit found", false);
// No values can exceed those boundings, no matter the option // // No values can exceed those boundings, no matter the option //
if (val < 0) if (val < 0)
...@@ -612,21 +591,17 @@ end_plt_add_uri: ...@@ -612,21 +591,17 @@ end_plt_add_uri:
bool bool
command_plt_remove(volatile sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr) command_plt_remove(volatile sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], mpd_idle_flag *watch_mask_ptr)
{ {
char *endptr;
long pos;
RETURN_UNLESS(args && args[0], "Invalid argument", false); RETURN_UNLESS(args && args[0], "Invalid argument", false);
char *endptr, err;
long pos;
if (args[1] == NULL) { if (args[1] == NULL) {
*watch_mask_ptr |= MPD_IDLE_PLAYLIST; *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
return database_plt_remove(db, args[0]); return database_plt_remove(db, args[0]);
} }
pos = strtol(args[1], &endptr, 10); STRTOL(pos, args[1], endptr, err);
RETURN_IF((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN)) || (errno != 0 && pos == 0), RETURN_IF(err, "STRTOL failed", false);
"Failed: strtol", false);
RETURN_IF(endptr == args[1], "No digit found", false);
*watch_mask_ptr |= MPD_IDLE_PLAYLIST; *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
return database_plt_remove_pos(db, args[0], pos); return database_plt_remove_pos(db, args[0], pos);
} }
...@@ -699,7 +674,7 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_ ...@@ -699,7 +674,7 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_
{ {
unsigned int from, to, tmp_switch; unsigned int from, to, tmp_switch;
long val; long val;
char *endptr, *str; char *endptr, err, *str;
struct lkt_callback callback = { struct lkt_callback callback = {
.call = lkt_callback_send_row_v1, .call = lkt_callback_send_row_v1,
.srv = srv, .srv = srv,
...@@ -709,13 +684,8 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_ ...@@ -709,13 +684,8 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_
RETURN_UNLESS(args && args[0] && strlen(args[0]), "Invalid argument", false); RETURN_UNLESS(args && args[0] && strlen(args[0]), "Invalid argument", false);
/* Convert the first integer. */ /* Convert the first integer. */
errno = 0; STRTOL(val, args[0], endptr, err);
str = args[0] + strspn(args[0], "-+ "); // Skip not permited chars (NO NEGATIVE!) // RETURN_IF(err, "STRTOL failed", false);
val = (unsigned int) strtol(str, &endptr, 0);
RETURN_IF((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0),
"Invalid argument", false);
RETURN_IF(endptr == args[0], "No digit found", false);
from = labs(val); from = labs(val);
/* There is nothing after, is is the song pos version. */ /* There is nothing after, is is the song pos version. */
...@@ -725,12 +695,9 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_ ...@@ -725,12 +695,9 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_
/* There is something after, this is the absolute forme of the command. Then /* There is something after, this is the absolute forme of the command. Then
parse the second value. Skip the supposed ':' character. */ parse the second value. Skip the supposed ':' character. */
else { else {
str = endptr + strspn(endptr, "-+: "); // NO NEGATIVE! // str = endptr;
val = strtol(str, &endptr, 0); STRTOL(val, str, endptr, err);
RETURN_IF((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0), RETURN_IF(err, "STRTOL failed", false);
"Invalid argument", false);
RETURN_IF(endptr == args[0], "No digit found", false);
to = labs(val); to = labs(val);
if (to < from) { if (to < from) {
...@@ -866,14 +833,16 @@ bool ...@@ -866,14 +833,16 @@ bool
command_sticker_get(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]) command_sticker_get(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX])
{ {
RETURN_UNLESS(argv[0] && argv[1] && argv[2] && !argv[3], "Invalid argument", false); RETURN_UNLESS(argv[0] && argv[1] && argv[2] && !argv[3], "Invalid argument", false);
int uri = atoi(argv[1]); /* FIXME: Use strtol. */ int uri, err;
char *endptr;
STRTOL(uri, argv[1], endptr, err);
RETURN_IF(err, "STRTOL failed", false);
struct sticker_callback cb = { struct sticker_callback cb = {
.srv = srv, .srv = srv,
.c = c, .c = c,
.call = sticker_send_one_value, .call = sticker_send_one_value,
}; };
RETURN_UNLESS(database_sticker_get(srv->db, argv[0], argv[2], uri, &cb), "Failed to get sticker", RETURN_UNLESS(database_sticker_get(srv->db, argv[0], argv[2], uri, &cb), "Failed to get sticker", false);
false);
return true; return true;
} }
...@@ -882,8 +851,11 @@ command_sticker_set(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS ...@@ -882,8 +851,11 @@ command_sticker_set(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS
{ {
UNUSED(c); UNUSED(c);
RETURN_UNLESS(argv[0] && argv[1] && argv[2] && argv[3] && !argv[4], "Invalid argument", false); RETURN_UNLESS(argv[0] && argv[1] && argv[2] && argv[3] && !argv[4], "Invalid argument", false);
int uri = atoi(argv[1]); /* FIXME: Use strtol. */ int uri, value, err1, err2;
int value = atoi(argv[4]); /* FIXME: Use strtol. */ char *endptr;
STRTOL(uri, argv[1], endptr, err1);
STRTOL(value, argv[4], endptr, err2);
RETURN_IF(err1 || err2, "STRTOL failed", false);
RETURN_UNLESS(database_sticker_set(srv->db, argv[0], argv[2], uri, value), "Failed to get sticker", false); RETURN_UNLESS(database_sticker_set(srv->db, argv[0], argv[2], uri, value), "Failed to get sticker", false);
srv->mpd_idle_events |= MPD_IDLE_STICKER; srv->mpd_idle_events |= MPD_IDLE_STICKER;
return true; return true;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter