mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-31 22:25:38 -04:00
spa: add spa_atob() to convert a string to a boolean
This replaces the manual check for "true" and some (inconsistent) return value of atoi. All those instances now require either "true" or "1" to parse as true, any other value (including NULL) is boolean false.
This commit is contained in:
parent
4e70799922
commit
cdfd50e166
18 changed files with 58 additions and 34 deletions
|
|
@ -82,6 +82,17 @@ static inline bool spa_atoi32(const char *str, int32_t *val, int base)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert \a str to a boolean. Allowed boolean values are "true" and a
|
||||||
|
* literal "1", anything else is false.
|
||||||
|
*
|
||||||
|
* \return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
static inline bool spa_atob(const char *str)
|
||||||
|
{
|
||||||
|
return spa_streq(str, "true") || spa_streq(str, "1");
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} /* extern "C" */
|
} /* extern "C" */
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1480,19 +1480,19 @@ struct acp_card *acp_card_new(uint32_t index, const struct acp_dict *props)
|
||||||
|
|
||||||
if (props) {
|
if (props) {
|
||||||
if ((s = acp_dict_lookup(props, "api.alsa.use-ucm")) != NULL)
|
if ((s = acp_dict_lookup(props, "api.alsa.use-ucm")) != NULL)
|
||||||
impl->use_ucm = (spa_streq(s, "true") || atoi(s) == 1);
|
impl->use_ucm = spa_atob(s);
|
||||||
if ((s = acp_dict_lookup(props, "api.alsa.soft-mixer")) != NULL)
|
if ((s = acp_dict_lookup(props, "api.alsa.soft-mixer")) != NULL)
|
||||||
impl->soft_mixer = (spa_streq(s, "true") || atoi(s) == 1);
|
impl->soft_mixer = spa_atob(s);
|
||||||
if ((s = acp_dict_lookup(props, "api.alsa.ignore-dB")) != NULL)
|
if ((s = acp_dict_lookup(props, "api.alsa.ignore-dB")) != NULL)
|
||||||
ignore_dB = (spa_streq(s, "true") || atoi(s) == 1);
|
ignore_dB = spa_atob(s);
|
||||||
if ((s = acp_dict_lookup(props, "device.profile-set")) != NULL)
|
if ((s = acp_dict_lookup(props, "device.profile-set")) != NULL)
|
||||||
profile_set = s;
|
profile_set = s;
|
||||||
if ((s = acp_dict_lookup(props, "device.profile")) != NULL)
|
if ((s = acp_dict_lookup(props, "device.profile")) != NULL)
|
||||||
profile = s;
|
profile = s;
|
||||||
if ((s = acp_dict_lookup(props, "api.acp.auto-profile")) != NULL)
|
if ((s = acp_dict_lookup(props, "api.acp.auto-profile")) != NULL)
|
||||||
impl->auto_profile = (spa_streq(s, "true") || atoi(s) == 1);
|
impl->auto_profile = spa_atob(s);
|
||||||
if ((s = acp_dict_lookup(props, "api.acp.auto-port")) != NULL)
|
if ((s = acp_dict_lookup(props, "api.acp.auto-port")) != NULL)
|
||||||
impl->auto_port = (spa_streq(s, "true") || atoi(s) == 1);
|
impl->auto_port = spa_atob(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl->ucm.default_sample_spec.format = PA_SAMPLE_S16NE;
|
impl->ucm.default_sample_spec.format = PA_SAMPLE_S16NE;
|
||||||
|
|
|
||||||
|
|
@ -959,9 +959,9 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_API_ALSA_PATH)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_API_ALSA_PATH)) != NULL)
|
||||||
snprintf(this->props.device, sizeof(this->props.device), "%s", str);
|
snprintf(this->props.device, sizeof(this->props.device), "%s", str);
|
||||||
if ((str = spa_dict_lookup(info, "api.acp.auto-port")) != NULL)
|
if ((str = spa_dict_lookup(info, "api.acp.auto-port")) != NULL)
|
||||||
this->props.auto_port = spa_streq(str, "true") || atoi(str) != 0;
|
this->props.auto_port = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, "api.acp.auto-profile")) != NULL)
|
if ((str = spa_dict_lookup(info, "api.acp.auto-profile")) != NULL)
|
||||||
this->props.auto_profile = spa_streq(str, "true") || atoi(str) != 0;
|
this->props.auto_profile = spa_atob(str);
|
||||||
|
|
||||||
items = alloca((info->n_items) * sizeof(*items));
|
items = alloca((info->n_items) * sizeof(*items));
|
||||||
spa_dict_for_each(it, info)
|
spa_dict_for_each(it, info)
|
||||||
|
|
|
||||||
|
|
@ -808,11 +808,11 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
} else if (spa_streq(k, "api.alsa.start-delay")) {
|
} else if (spa_streq(k, "api.alsa.start-delay")) {
|
||||||
this->default_start_delay = atoi(s);
|
this->default_start_delay = atoi(s);
|
||||||
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
|
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
|
||||||
this->disable_mmap = (spa_streq(s, "true") || atoi(s) == 1);
|
this->disable_mmap = spa_atob(s);
|
||||||
} else if (spa_streq(k, "api.alsa.disable-batch")) {
|
} else if (spa_streq(k, "api.alsa.disable-batch")) {
|
||||||
this->disable_batch = (spa_streq(s, "true") || atoi(s) == 1);
|
this->disable_batch = spa_atob(s);
|
||||||
} else if (spa_streq(k, "api.alsa.use-chmap")) {
|
} else if (spa_streq(k, "api.alsa.use-chmap")) {
|
||||||
this->props.use_chmap = (spa_streq(s, "true") || atoi(s) == 1);
|
this->props.use_chmap = spa_atob(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -826,11 +826,11 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
} else if (spa_streq(k, "api.alsa.headroom")) {
|
} else if (spa_streq(k, "api.alsa.headroom")) {
|
||||||
this->default_headroom = atoi(s);
|
this->default_headroom = atoi(s);
|
||||||
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
|
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
|
||||||
this->disable_mmap = (spa_streq(s, "true") || atoi(s) == 1);
|
this->disable_mmap = spa_atob(s);
|
||||||
} else if (spa_streq(k, "api.alsa.disable-batch")) {
|
} else if (spa_streq(k, "api.alsa.disable-batch")) {
|
||||||
this->disable_batch = (spa_streq(s, "true") || atoi(s) == 1);
|
this->disable_batch = spa_atob(s);
|
||||||
} else if (spa_streq(k, "api.alsa.use-chmap")) {
|
} else if (spa_streq(k, "api.alsa.use-chmap")) {
|
||||||
this->props.use_chmap = (spa_streq(s, "true") || atoi(s) == 1);
|
this->props.use_chmap = spa_atob(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -768,7 +768,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
|
|
||||||
if (info) {
|
if (info) {
|
||||||
if ((str = spa_dict_lookup(info, "alsa.use-acp")) != NULL)
|
if ((str = spa_dict_lookup(info, "alsa.use-acp")) != NULL)
|
||||||
this->use_acp = spa_streq(str, "true") || atoi(str) != 0;
|
this->use_acp = spa_atob(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -1392,14 +1392,11 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
for (i = 0; info && i < info->n_items; i++) {
|
for (i = 0; info && i < info->n_items; i++) {
|
||||||
const char *k = info->items[i].key;
|
const char *k = info->items[i].key;
|
||||||
const char *s = info->items[i].value;
|
const char *s = info->items[i].value;
|
||||||
if (spa_streq(k, "channelmix.normalize") &&
|
if (spa_streq(k, "channelmix.normalize") && spa_atob(s))
|
||||||
(spa_streq(s, "true") || atoi(s) != 0))
|
|
||||||
this->mix.options |= CHANNELMIX_OPTION_NORMALIZE;
|
this->mix.options |= CHANNELMIX_OPTION_NORMALIZE;
|
||||||
if (spa_streq(k, "channelmix.mix-lfe") &&
|
if (spa_streq(k, "channelmix.mix-lfe") && spa_atob(s))
|
||||||
(spa_streq(s, "true") || atoi(s) != 0))
|
|
||||||
this->mix.options |= CHANNELMIX_OPTION_MIX_LFE;
|
this->mix.options |= CHANNELMIX_OPTION_MIX_LFE;
|
||||||
if (spa_streq(k, "channelmix.upmix") &&
|
if (spa_streq(k, "channelmix.upmix") && spa_atob(s))
|
||||||
(spa_streq(s, "true") || atoi(s) != 0))
|
|
||||||
this->mix.options |= CHANNELMIX_OPTION_UPMIX;
|
this->mix.options |= CHANNELMIX_OPTION_UPMIX;
|
||||||
if (spa_streq(k, "channelmix.lfe-cutoff"))
|
if (spa_streq(k, "channelmix.lfe-cutoff"))
|
||||||
this->mix.lfe_cutoff = atoi(s);
|
this->mix.lfe_cutoff = atoi(s);
|
||||||
|
|
|
||||||
|
|
@ -1385,7 +1385,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
this->monitor_channel_volumes = false;
|
this->monitor_channel_volumes = false;
|
||||||
if (info) {
|
if (info) {
|
||||||
if ((str = spa_dict_lookup(info, "monitor.channel-volumes")) != NULL)
|
if ((str = spa_dict_lookup(info, "monitor.channel-volumes")) != NULL)
|
||||||
this->monitor_channel_volumes = spa_streq(str, "true") || atoi(str) == 1;
|
this->monitor_channel_volumes = spa_atob(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->node.iface = SPA_INTERFACE_INIT(
|
this->node.iface = SPA_INTERFACE_INIT(
|
||||||
|
|
|
||||||
|
|
@ -1007,7 +1007,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
if ((str = spa_dict_lookup(info, "resample.quality")) != NULL)
|
if ((str = spa_dict_lookup(info, "resample.quality")) != NULL)
|
||||||
this->props.quality = atoi(str);
|
this->props.quality = atoi(str);
|
||||||
if ((str = spa_dict_lookup(info, "resample.peaks")) != NULL)
|
if ((str = spa_dict_lookup(info, "resample.peaks")) != NULL)
|
||||||
this->peaks = spa_streq(str, "true") || atoi(str) == 1;
|
this->peaks = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, "factory.mode")) != NULL) {
|
if ((str = spa_dict_lookup(info, "factory.mode")) != NULL) {
|
||||||
if (spa_streq(str, "split"))
|
if (spa_streq(str, "split"))
|
||||||
this->mode = MODE_SPLIT;
|
this->mode = MODE_SPLIT;
|
||||||
|
|
|
||||||
|
|
@ -1508,7 +1508,7 @@ struct spa_bt_backend *backend_hsphfpd_new(struct spa_bt_monitor *monitor,
|
||||||
backend->main_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Loop);
|
backend->main_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Loop);
|
||||||
backend->conn = dbus_connection;
|
backend->conn = dbus_connection;
|
||||||
if (info && (str = spa_dict_lookup(info, "bluez5.msbc-support")))
|
if (info && (str = spa_dict_lookup(info, "bluez5.msbc-support")))
|
||||||
backend->msbc_supported = spa_streq(str, "true") || atoi(str) == 1;
|
backend->msbc_supported = spa_atob(str);
|
||||||
else
|
else
|
||||||
backend->msbc_supported = false;
|
backend->msbc_supported = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1478,7 +1478,7 @@ static DBusHandlerResult profile_new_connection(DBusConnection *conn, DBusMessag
|
||||||
spa_list_append(&backend->rfcomm_list, &rfcomm->link);
|
spa_list_append(&backend->rfcomm_list, &rfcomm->link);
|
||||||
|
|
||||||
if (d->settings && (str = spa_dict_lookup(d->settings, "bluez5.msbc-support")))
|
if (d->settings && (str = spa_dict_lookup(d->settings, "bluez5.msbc-support")))
|
||||||
rfcomm->msbc_support_enabled_in_config = spa_streq(str, "true") || atoi(str) == 1;
|
rfcomm->msbc_support_enabled_in_config = spa_atob(str);
|
||||||
else
|
else
|
||||||
rfcomm->msbc_support_enabled_in_config = false;
|
rfcomm->msbc_support_enabled_in_config = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -789,7 +789,7 @@ struct spa_bt_backend *backend_ofono_new(struct spa_bt_monitor *monitor,
|
||||||
backend->main_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Loop);
|
backend->main_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Loop);
|
||||||
backend->conn = dbus_connection;
|
backend->conn = dbus_connection;
|
||||||
if (info && (str = spa_dict_lookup(info, "bluez5.msbc-support")))
|
if (info && (str = spa_dict_lookup(info, "bluez5.msbc-support")))
|
||||||
backend->msbc_supported = spa_streq(str, "true") || atoi(str) == 1;
|
backend->msbc_supported = spa_atob(str);
|
||||||
else
|
else
|
||||||
backend->msbc_supported = false;
|
backend->msbc_supported = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3876,7 +3876,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
uint32_t tmp;
|
uint32_t tmp;
|
||||||
|
|
||||||
if ((str = spa_dict_lookup(info, "api.bluez5.connection-info")) != NULL &&
|
if ((str = spa_dict_lookup(info, "api.bluez5.connection-info")) != NULL &&
|
||||||
(spa_streq(str, "true") || atoi(str)))
|
spa_atob(str))
|
||||||
this->connection_info_supported = true;
|
this->connection_info_supported = true;
|
||||||
|
|
||||||
if ((str = spa_dict_lookup(info, "bluez5.default.rate")) != NULL &&
|
if ((str = spa_dict_lookup(info, "bluez5.default.rate")) != NULL &&
|
||||||
|
|
@ -3888,7 +3888,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
this->default_audio_info.channels = tmp;
|
this->default_audio_info.channels = tmp;
|
||||||
|
|
||||||
if ((str = spa_dict_lookup(info, "bluez5.sbc-xq-support")) != NULL &&
|
if ((str = spa_dict_lookup(info, "bluez5.sbc-xq-support")) != NULL &&
|
||||||
(spa_streq(str, "true") || atoi(str)))
|
spa_atob(str))
|
||||||
this->enable_sbc_xq = true;
|
this->enable_sbc_xq = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -266,11 +266,11 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
}
|
}
|
||||||
if (info) {
|
if (info) {
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_TIMESTAMP)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_TIMESTAMP)) != NULL)
|
||||||
this->timestamp = (spa_streq(str, "true") || atoi(str) == 1);
|
this->timestamp = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LINE)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LINE)) != NULL)
|
||||||
this->line = (spa_streq(str, "true") || atoi(str) == 1);
|
this->line = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_COLORS)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_COLORS)) != NULL)
|
||||||
this->colors = (spa_streq(str, "true") || atoi(str) == 1);
|
this->colors = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LEVEL)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LEVEL)) != NULL)
|
||||||
this->log.level = atoi(str);
|
this->log.level = atoi(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_FILE)) != NULL) {
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_FILE)) != NULL) {
|
||||||
|
|
|
||||||
|
|
@ -360,7 +360,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
|
|
||||||
if (info) {
|
if (info) {
|
||||||
if ((str = spa_dict_lookup(info, "node.freewheel")) != NULL)
|
if ((str = spa_dict_lookup(info, "node.freewheel")) != NULL)
|
||||||
this->props.freewheel = (spa_streq(str, "true") || atoi(str) == 1);
|
this->props.freewheel = spa_atob(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
spa_loop_add_source(this->data_loop, &this->timer_source);
|
spa_loop_add_source(this->data_loop, &this->timer_source);
|
||||||
|
|
|
||||||
|
|
@ -337,7 +337,7 @@ static void follower_info(void *data, const struct spa_node_info *info)
|
||||||
|
|
||||||
if (info->props) {
|
if (info->props) {
|
||||||
if ((str = spa_dict_lookup(info->props, SPA_KEY_NODE_DRIVER)) != NULL)
|
if ((str = spa_dict_lookup(info->props, SPA_KEY_NODE_DRIVER)) != NULL)
|
||||||
this->driver = spa_streq(str, "true") || atoi(str) == 1;
|
this->driver = spa_atob(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -493,6 +493,22 @@ static void test_streq(void)
|
||||||
spa_assert(!spa_strneq(NULL, "abc", 7));
|
spa_assert(!spa_strneq(NULL, "abc", 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_atob(void)
|
||||||
|
{
|
||||||
|
spa_assert(spa_atob("true"));
|
||||||
|
spa_assert(spa_atob("1"));
|
||||||
|
spa_assert(!spa_atob("0"));
|
||||||
|
spa_assert(!spa_atob("-1"));
|
||||||
|
spa_assert(!spa_atob("10"));
|
||||||
|
spa_assert(!spa_atob("11"));
|
||||||
|
spa_assert(!spa_atob("t"));
|
||||||
|
spa_assert(!spa_atob("yes"));
|
||||||
|
spa_assert(!spa_atob("no"));
|
||||||
|
spa_assert(!spa_atob(NULL));
|
||||||
|
spa_assert(!spa_atob("True")); /* lower-case required */
|
||||||
|
spa_assert(!spa_atob("TRUE"));
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
test_abi();
|
test_abi();
|
||||||
|
|
@ -504,5 +520,6 @@ int main(int argc, char *argv[])
|
||||||
test_ringbuffer();
|
test_ringbuffer();
|
||||||
test_strtol();
|
test_strtol();
|
||||||
test_streq();
|
test_streq();
|
||||||
|
test_atob();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -525,8 +525,7 @@ void pw_init(int *argc, char **argv[])
|
||||||
pw_log_set(log);
|
pw_log_set(log);
|
||||||
|
|
||||||
#ifdef HAVE_SYSTEMD
|
#ifdef HAVE_SYSTEMD
|
||||||
if ((str = getenv("PIPEWIRE_LOG_SYSTEMD")) == NULL ||
|
if ((str = getenv("PIPEWIRE_LOG_SYSTEMD")) == NULL || spa_atob(str)) {
|
||||||
spa_streq(str, "true") || atoi(str) != 0) {
|
|
||||||
log = load_journal_logger(support);
|
log = load_journal_logger(support);
|
||||||
if (log)
|
if (log)
|
||||||
pw_log_set(log);
|
pw_log_set(log);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue