common/parse-bool.c: make parse_bool() generic

...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
This commit is contained in:
Johan Malm 2023-03-26 22:34:44 +01:00 committed by Johan Malm
parent 473f0aacbd
commit fa50149525
7 changed files with 144 additions and 64 deletions

View file

@ -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;
}

View 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',

25
src/common/parse-bool.c Normal file
View file

@ -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;
}