const char *property)
{
const char *val;
+ bool b;
val = udev_device_get_property_value(udev_device, property);
if (!val)
return false;
- if (streq(val, "1"))
- return true;
- if (!streq(val, "0"))
+ if (!parse_boolean_property(val, &b)) {
evdev_log_error(device,
"property %s has invalid value '%s'\n",
property,
val);
- return false;
+ return false;
+ }
+
+ return b;
}
int
assert(strneq(key, "Model", 5));
- if (streq(value, "1"))
- b = true;
- else if (streq(value, "0"))
- b = false;
- else
+ if (!parse_boolean_property(value, &b))
return false;
do {
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_USE_VELOCITY_AVERAGING))) {
p->id = QUIRK_ATTR_USE_VELOCITY_AVERAGING;
- if (streq(value, "1"))
- b = true;
- else if (streq(value, "0"))
- b = false;
- else
+ if (!parse_boolean_property(value, &b))
goto out;
p->type = PT_BOOL;
p->value.b = b;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_TABLET_SMOOTHING))) {
p->id = QUIRK_ATTR_TABLET_SMOOTHING;
- if (streq(value, "1"))
- b = true;
- else if (streq(value, "0"))
- b = false;
- else
+ if (!parse_boolean_property(value, &b))
goto out;
p->type = PT_BOOL;
p->value.b = b;
return true;
}
+bool
+parse_boolean_property(const char *prop, bool *b)
+{
+ if (!prop)
+ return false;
+
+ if (streq(prop, "1"))
+ *b = true;
+ else if (streq(prop, "0"))
+ *b = false;
+ else
+ return false;
+
+ return true;
+}
+
static bool
parse_evcode_string(const char *s, int *type_out, int *code_out)
{
bool parse_dimension_property(const char *prop, size_t *width, size_t *height);
bool parse_calibration_property(const char *prop, float calibration[6]);
bool parse_range_property(const char *prop, int *hi, int *lo);
+bool parse_boolean_property(const char *prop, bool *b);
#define EVENT_CODE_UNDEFINED 0xffff
bool parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents);
bool parse_input_prop_property(const char *prop, unsigned int *props_out, size_t *nprops);
}
END_TEST
+START_TEST(boolean_prop_parser)
+{
+ struct parser_test_range {
+ char *tag;
+ bool success;
+ bool b;
+ } tests[] = {
+ { "0", true, false },
+ { "1", true, true },
+ { "-1", false, false },
+ { "2", false, false },
+ { "abcd", false, false },
+ { NULL, false, false }
+ };
+ int i;
+ bool success, b;
+
+ for (i = 0; tests[i].tag != NULL; i++) {
+ b = false;
+ success = parse_boolean_property(tests[i].tag, &b);
+ ck_assert(success == tests[i].success);
+ if (success)
+ ck_assert_int_eq(b, tests[i].b);
+ else
+ ck_assert_int_eq(b, false);
+ }
+
+ success = parse_boolean_property(NULL, NULL);
+ ck_assert(success == false);
+}
+END_TEST
+
START_TEST(evcode_prop_parser)
{
struct parser_test_tuple {
tcase_add_test(tc, reliability_prop_parser);
tcase_add_test(tc, calibration_prop_parser);
tcase_add_test(tc, range_prop_parser);
+ tcase_add_test(tc, boolean_prop_parser);
tcase_add_test(tc, evcode_prop_parser);
tcase_add_test(tc, input_prop_parser);
tcase_add_test(tc, evdev_abs_parser);