Skip to content

Commit 7e6cbec

Browse files
committed
xml: teach xml_get_bool_text() correct bool parsing
1 parent 0f91e6d commit 7e6cbec

File tree

6 files changed

+49
-49
lines changed

6 files changed

+49
-49
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(PROJECT_SOURCES
4646
src/xml.cpp src/xml.h
4747
src/find-themes.cpp src/find-themes.h
4848
src/pair.h
49+
src/parse-bool.cpp src/parse-bool.h
4950

5051
src/appearance.cpp src/appearance.h src/appearance.ui
5152
src/behaviour.cpp src/behaviour.h src/behaviour.ui
@@ -85,12 +86,12 @@ lxqt_translate_desktop(PROJECT_DESKTOP_FILES
8586
#===================================================================================================
8687
include(CTest)
8788

88-
add_executable(t1000 tests/t1000-add-xpath-node.cpp tests/tap.cpp src/xml.cpp)
89+
add_executable(t1000 tests/t1000-add-xpath-node.cpp tests/tap.cpp src/xml.cpp src/parse-bool.cpp)
8990
target_link_libraries(t1000 PRIVATE ${GLIB_LDFLAGS} ${LIBXML2_LIBRARIES})
9091
target_include_directories(t1000 PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIR})
9192
add_test(t1000 t1000)
9293

93-
add_executable(t1001 tests/t1001-nodenames.cpp tests/tap.cpp)
94+
add_executable(t1001 tests/t1001-nodenames.cpp tests/tap.cpp src/parse-bool.cpp)
9495
target_link_libraries(t1001 PRIVATE ${GLIB_LDFLAGS} ${LIBXML2_LIBRARIES})
9596
target_include_directories(t1001 PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIR})
9697
add_test(t1001 t1001)

src/parse-bool.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <cstring>
2+
#include "log.h"
3+
#include "parse-bool.h"
4+
5+
int parseBool(const char *str, int defaultValue)
6+
{
7+
if (!str)
8+
goto error_not_a_boolean;
9+
else if (!strcasecmp(str, "yes"))
10+
return 1;
11+
else if (!strcasecmp(str, "true"))
12+
return 1;
13+
else if (!strcasecmp(str, "on"))
14+
return 1;
15+
else if (!strcmp(str, "1"))
16+
return 1;
17+
else if (!strcasecmp(str, "no"))
18+
return 0;
19+
else if (!strcasecmp(str, "false"))
20+
return 0;
21+
else if (!strcasecmp(str, "off"))
22+
return 0;
23+
else if (!strcmp(str, "0"))
24+
return 0;
25+
error_not_a_boolean:
26+
warn("{} is not a boolean value", str);;
27+
return defaultValue;
28+
}
29+
30+

src/parse-bool.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#pragma once
3+
4+
/**
5+
* parseBool() - Parse boolean value of string.
6+
* @string: String to interpret. This check is case-insensitive.
7+
* @default_value: Default value to use if string is not a recognised boolean.
8+
* Use -1 to avoid setting a default value.
9+
*
10+
* Return: 0 for false; 1 for true; -1 for non-boolean
11+
*/
12+
int parseBool(const char *str, int defaultValue);

src/settings.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,6 @@ void setStr(QString name, QString value)
215215
}
216216
}
217217

218-
/**
219-
* parse_bool() - Parse boolean value of string.
220-
* @string: String to interpret. This check is case-insensitive.
221-
* @default_value: Default value to use if string is not a recognised boolean.
222-
* Use -1 to avoid setting a default value.
223-
*
224-
* Return: 0 for false; 1 for true; -1 for non-boolean
225-
*/
226-
int parseBool(const char *str, int defaultValue)
227-
{
228-
if (!str)
229-
goto error_not_a_boolean;
230-
else if (!strcasecmp(str, "yes"))
231-
return 1;
232-
else if (!strcasecmp(str, "true"))
233-
return 1;
234-
else if (!strcasecmp(str, "on"))
235-
return 1;
236-
else if (!strcmp(str, "1"))
237-
return 1;
238-
else if (!strcasecmp(str, "no"))
239-
return 0;
240-
else if (!strcasecmp(str, "false"))
241-
return 0;
242-
else if (!strcasecmp(str, "off"))
243-
return 0;
244-
else if (!strcmp(str, "0"))
245-
return 0;
246-
error_not_a_boolean:
247-
qDebug() << str << "is not a boolean value";
248-
return defaultValue;
249-
}
250-
251218
void setBool(QString name, int value)
252219
{
253220
std::shared_ptr<Setting> setting = retrieve(name);
@@ -265,9 +232,3 @@ void setBool(QString name, int value)
265232
setting->setValue(value);
266233
}
267234
}
268-
269-
void setBoolfromString(QString name, QString value)
270-
{
271-
int boolValue = parseBool(value.toStdString().c_str(), -1);
272-
setBool(name, boolValue);
273-
}

src/settings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,3 @@ void setInt(QString name, int value);
6464
void setStr(QString name, QString value);
6565
int parseBool(const char *str, int defaultValue);
6666
void setBool(QString name, int value);
67-
void setBoolfromString(QString name, QString value);

src/xml.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string.h>
1414
#include <strings.h>
1515
#include <unistd.h>
16+
#include "parse-bool.h"
1617
#include "xml.h"
1718

1819
enum xml_mode {
@@ -226,13 +227,9 @@ int xml_get_bool_text(const char *nodename)
226227
if (!value || !*value) {
227228
return -1;
228229
}
229-
if (!strcasecmp(value, "yes") || !strcasecmp(value, "true")) {
230-
return 1;
231-
} else if (!strcasecmp(value, "no") || !strcasecmp(value, "false")) {
232-
return 0;
233-
} else {
234-
return -1;
235-
}
230+
231+
/* Parse consistently with labwc */
232+
return parseBool(value, -1);
236233
}
237234

238235
/* case-insensitive */

0 commit comments

Comments
 (0)