From c9362325454fe1fea000be277ba736bfe813df14 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sun, 22 Jun 2014 11:12:51 -0400 Subject: [PATCH] Consistently parse workspace numbers Use ws_name_to_number() to parse workspace numbers where this transformation takes place. --- src/commands.c | 30 ++++++++---------------------- src/workspace.c | 10 +--------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/commands.c b/src/commands.c index b9c130de..a60dde62 100644 --- a/src/commands.c +++ b/src/commands.c @@ -550,12 +550,9 @@ void cmd_move_con_to_workspace_number(I3_CMD, char *which) { /* get the workspace */ Con *output, *workspace = NULL; - char *endptr = NULL; - long parsed_num = strtol(which, &endptr, 10); - if (parsed_num == LONG_MIN || - parsed_num == LONG_MAX || - parsed_num < 0 || - endptr == which) { + long parsed_num = ws_name_to_number(which); + + if (parsed_num == -1) { LOG("Could not parse initial part of \"%s\" as a number.\n", which); // TODO: better error message yerror("Could not parse number"); @@ -954,16 +951,12 @@ void cmd_workspace(I3_CMD, char *which) { void cmd_workspace_number(I3_CMD, char *which) { Con *output, *workspace = NULL; - char *endptr = NULL; - long parsed_num = strtol(which, &endptr, 10); - if (parsed_num == LONG_MIN || - parsed_num == LONG_MAX || - parsed_num < 0 || - endptr == which) { + long parsed_num = ws_name_to_number(which); + + if (parsed_num == -1) { LOG("Could not parse initial part of \"%s\" as a number.\n", which); // TODO: better error message yerror("Could not parse number"); - return; } @@ -1925,15 +1918,8 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) { /* Change the name and try to parse it as a number. */ FREE(workspace->name); workspace->name = sstrdup(new_name); - char *endptr = NULL; - long parsed_num = strtol(new_name, &endptr, 10); - if (parsed_num == LONG_MIN || - parsed_num == LONG_MAX || - parsed_num < 0 || - endptr == new_name) - workspace->num = -1; - else - workspace->num = parsed_num; + + workspace->num = ws_name_to_number(new_name); LOG("num = %d\n", workspace->num); /* By re-attaching, the sort order will be correct afterwards. */ diff --git a/src/workspace.c b/src/workspace.c index 41c02952..54a2bd33 100644 --- a/src/workspace.c +++ b/src/workspace.c @@ -177,15 +177,7 @@ Con *create_workspace_on_output(Output *output, Con *content) { if (!exists) { /* Set ->num to the number of the workspace, if the name actually * is a number or starts with a number */ - char *endptr = NULL; - long parsed_num = strtol(ws->name, &endptr, 10); - if (parsed_num == LONG_MIN || - parsed_num == LONG_MAX || - parsed_num < 0 || - endptr == ws->name) - ws->num = -1; - else - ws->num = parsed_num; + ws->num = ws_name_to_number(ws->name); LOG("Used number %d for workspace with name %s\n", ws->num, ws->name); break; -- 2.39.5