]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/parse-bool.c: make parse_bool() generic
authorJohan Malm <jgm323@gmail.com>
Sun, 26 Mar 2023 21:34:44 +0000 (22:34 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 26 Apr 2023 14:11:31 +0000 (15:11 +0100)
...to avoid multiple versions of a boolean-parser.

- Optionally take a default value
- Return -1 on error
- Rename get-bool.c to parse-bool.c

include/common/get-bool.h [deleted file]
include/common/parse-bool.h [new file with mode: 0644]
src/action.c
src/common/get-bool.c [deleted file]
src/common/meson.build
src/common/parse-bool.c [new file with mode: 0644]
src/config/rcxml.c

diff --git a/include/common/get-bool.h b/include/common/get-bool.h
deleted file mode 100644 (file)
index 9c480fd..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#ifndef __LABWC_GET_BOOL_H
-#define __LABWC_GET_BOOL_H
-#include <stdbool.h>
-
-/**
- * get_bool - interpret string and return boolean
- * @s: string to interpret
- *
- * Note: This merely performs a case-insensitive check for 'yes' and 'true'.
- * Returns false by default.
- */
-bool get_bool(const char *s);
-
-#endif /* __LABWC_GET_BOOL_H */
diff --git a/include/common/parse-bool.h b/include/common/parse-bool.h
new file mode 100644 (file)
index 0000000..b2d4d6b
--- /dev/null
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LABWC_PARSE_BOOL_H
+#define __LABWC_PARSE_BOOL_H
+#include <stdbool.h>
+
+/**
+ * parse_bool() - Parse boolean value of string.
+ * @string: String to interpret. This check is case-insensitive.
+ * @default_value: Default value to use if string is not a recognised boolean.
+ *                 Use -1 to avoid setting a default value.
+ *
+ * Return: 0 for false; 1 for true; -1 for non-boolean
+ */
+int parse_bool(const char *str, int default_value);
+
+#endif /* __LABWC_PARSE_BOOL_H */
index 920981947522b6ddf329837577d127dc90366f4e..9106059c7de9765c177264037975ee7fea33c73f 100644 (file)
@@ -7,9 +7,9 @@
 #include <unistd.h>
 #include <wlr/util/log.h>
 #include "action.h"
-#include "common/get-bool.h"
 #include "common/list.h"
 #include "common/mem.h"
+#include "common/parse-bool.h"
 #include "common/spawn.h"
 #include "debug.h"
 #include "labwc.h"
@@ -154,7 +154,7 @@ action_arg_from_xml_node(struct action *action, char *nodename, char *content)
                action_arg_add_str(action, "to", content);
        } else if (!strcmp(nodename, "follow.action")) {
                /* SendToDesktop */
-               action_arg_add_bool(action, "follow", get_bool(content));
+               action_arg_add_bool(action, "follow", parse_bool(content, true));
        } else if (!strcmp(nodename, "region.action")) {
                /* SnapToRegion */
                action_arg_add_str(action, NULL, content);
diff --git a/src/common/get-bool.c b/src/common/get-bool.c
deleted file mode 100644 (file)
index afddbb4..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-#include <string.h>
-#include <strings.h>
-#include "common/get-bool.h"
-
-bool
-get_bool(const char *s)
-{
-       if (!s) {
-               return false;
-       }
-       if (!strcasecmp(s, "yes")) {
-               return true;
-       }
-       if (!strcasecmp(s, "true")) {
-               return true;
-       }
-       return false;
-}
index 5f21c25830175b83e22c7a7a1bfa67dcd63afe28..68326d3124e8823e806771b343e8eb270010f4a0 100644 (file)
@@ -3,11 +3,11 @@ labwc_sources += files(
   'dir.c',
   'fd_util.c',
   'font.c',
-  'get-bool.c',
   'grab-file.c',
   'graphic-helpers.c',
   'mem.c',
   'nodename.c',
+  'parse-bool.c',
   'scaled_font_buffer.c',
   'scaled_scene_buffer.c',
   'scene-helpers.c',
diff --git a/src/common/parse-bool.c b/src/common/parse-bool.c
new file mode 100644 (file)
index 0000000..0e1d4b1
--- /dev/null
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <string.h>
+#include <strings.h>
+#include <wlr/util/log.h>
+#include "common/parse-bool.h"
+
+int
+parse_bool(const char *str, int default_value)
+{
+       if (!str) {
+               goto error_not_a_boolean;
+       } else if (!strcasecmp(str, "yes")) {
+               return true;
+       } else if (!strcasecmp(str, "true")) {
+               return true;
+       } else if (!strcasecmp(str, "no")) {
+               return false;
+       } else if (!strcasecmp(str, "false")) {
+               return false;
+       }
+error_not_a_boolean:
+       wlr_log(WLR_ERROR, "(%s) is not a boolean value", str);
+       return default_value;
+}
+
index d64807d33529b763deb4dfe6e3a4e5663cb61eda..910a95c51e2b4d36b2ffd62acbfcdd4adeccda72 100644 (file)
 #include <wlr/util/box.h>
 #include <wlr/util/log.h>
 #include "action.h"
-#include "common/get-bool.h"
 #include "common/list.h"
 #include "common/mem.h"
 #include "common/nodename.h"
+#include "common/parse-bool.h"
 #include "common/string-helpers.h"
 #include "config/keybind.h"
 #include "config/libinput.h"
@@ -265,10 +265,17 @@ fill_libinput_category(char *nodename, char *content)
                        current_libinput_category->name = xstrdup(content);
                }
        } else if (!strcasecmp(nodename, "naturalScroll")) {
-               current_libinput_category->natural_scroll =
-                       get_bool(content) ? 1 : 0;
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               current_libinput_category->natural_scroll = ret;
        } else if (!strcasecmp(nodename, "leftHanded")) {
-               current_libinput_category->left_handed = get_bool(content) ? 1 : 0;
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               current_libinput_category->left_handed = ret;
        } else if (!strcasecmp(nodename, "pointerSpeed")) {
                current_libinput_category->pointer_speed = atof(content);
                if (current_libinput_category->pointer_speed < -1) {
@@ -277,9 +284,12 @@ fill_libinput_category(char *nodename, char *content)
                        current_libinput_category->pointer_speed = 1;
                }
        } else if (!strcasecmp(nodename, "tap")) {
-               current_libinput_category->tap = get_bool(content) ?
-                       LIBINPUT_CONFIG_TAP_ENABLED :
-                       LIBINPUT_CONFIG_TAP_DISABLED;
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               current_libinput_category->tap = ret ? LIBINPUT_CONFIG_TAP_ENABLED
+                       : LIBINPUT_CONFIG_TAP_DISABLED;
        } else if (!strcasecmp(nodename, "tapButtonMap")) {
                if (!strcmp(content, "lrm")) {
                        current_libinput_category->tap_button_map =
@@ -294,13 +304,20 @@ fill_libinput_category(char *nodename, char *content)
                current_libinput_category->accel_profile =
                        get_accel_profile(content);
        } else if (!strcasecmp(nodename, "middleEmulation")) {
-               current_libinput_category->middle_emu = get_bool(content) ?
-                       LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED :
-                       LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               current_libinput_category->middle_emu = ret
+                       ? LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED
+                       : LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
        } else if (!strcasecmp(nodename, "disableWhileTyping")) {
-               current_libinput_category->dwt = get_bool(content) ?
-                       LIBINPUT_CONFIG_DWT_ENABLED :
-                       LIBINPUT_CONFIG_DWT_DISABLED;
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               current_libinput_category->dwt = ret ? LIBINPUT_CONFIG_DWT_ENABLED
+                       : LIBINPUT_CONFIG_DWT_DISABLED;
        }
 }
 
@@ -438,9 +455,17 @@ entry(xmlNode *node, char *nodename, char *content)
        } else if (!strcmp(nodename, "gap.core")) {
                rc.gap = atoi(content);
        } else if (!strcasecmp(nodename, "adaptiveSync.core")) {
-               rc.adaptive_sync = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.adaptive_sync = ret;
        } else if (!strcasecmp(nodename, "reuseOutputMode.core")) {
-               rc.reuse_output_mode = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.reuse_output_mode = ret;
        } else if (!strcmp(nodename, "name.theme")) {
                rc.theme_name = xstrdup(content);
        } else if (!strcmp(nodename, "cornerradius.theme")) {
@@ -454,9 +479,17 @@ entry(xmlNode *node, char *nodename, char *content)
        } else if (!strcmp(nodename, "weight.font.theme")) {
                fill_font(nodename, content, font_place);
        } else if (!strcasecmp(nodename, "followMouse.focus")) {
-               rc.focus_follow_mouse = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.focus_follow_mouse = ret;
        } else if (!strcasecmp(nodename, "raiseOnFocus.focus")) {
-               rc.raise_on_focus = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.raise_on_focus = ret;
        } else if (!strcasecmp(nodename, "doubleClickTime.mouse")) {
                long doubleclick_time_parsed = strtol(content, NULL, 10);
                if (doubleclick_time_parsed > 0) {
@@ -478,15 +511,31 @@ entry(xmlNode *node, char *nodename, char *content)
        } else if (!strcasecmp(nodename, "range.snapping")) {
                rc.snap_edge_range = atoi(content);
        } else if (!strcasecmp(nodename, "topMaximize.snapping")) {
-               rc.snap_top_maximize = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.snap_top_maximize = ret;
 
        /* <windowSwitcher show="" preview="" outlines="" /> */
        } else if (!strcasecmp(nodename, "show.windowSwitcher")) {
-               rc.window_switcher.show = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.show = ret;
        } else if (!strcasecmp(nodename, "preview.windowSwitcher")) {
-               rc.window_switcher.preview = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.preview = ret;
        } else if (!strcasecmp(nodename, "outlines.windowSwitcher")) {
-               rc.window_switcher.outlines = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.outlines = ret;
 
        /* Remove this long term - just a friendly warning for now */
        } else if (strstr(nodename, "windowswitcher.core")) {
@@ -494,23 +543,47 @@ entry(xmlNode *node, char *nodename, char *content)
 
        /* The following three are for backward compatibility only */
        } else if (!strcasecmp(nodename, "show.windowSwitcher.core")) {
-               rc.window_switcher.show = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.show = ret;
        } else if (!strcasecmp(nodename, "preview.windowSwitcher.core")) {
-               rc.window_switcher.preview = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.preview = ret;
        } else if (!strcasecmp(nodename, "outlines.windowSwitcher.core")) {
-               rc.window_switcher.outlines = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.outlines = ret;
 
        /* The following three are for backward compatibility only */
        } else if (!strcasecmp(nodename, "cycleViewOSD.core")) {
-               rc.window_switcher.show = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.show = ret;
                wlr_log(WLR_ERROR, "<cycleViewOSD> is deprecated."
                        " Use <windowSwitcher show=\"\" />");
        } else if (!strcasecmp(nodename, "cycleViewPreview.core")) {
-               rc.window_switcher.preview = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.preview = ret;
                wlr_log(WLR_ERROR, "<cycleViewPreview> is deprecated."
                        " Use <windowSwitcher preview=\"\" />");
        } else if (!strcasecmp(nodename, "cycleViewOutlines.core")) {
-               rc.window_switcher.outlines = get_bool(content);
+               int ret = parse_bool(content, -1);
+               if (ret < 0) {
+                       return;
+               }
+               rc.window_switcher.outlines = ret;
                wlr_log(WLR_ERROR, "<cycleViewOutlines> is deprecated."
                        " Use <windowSwitcher outlines=\"\" />");