From: Takashi Sakamoto Date: Sat, 2 Apr 2022 01:46:22 +0000 (+0900) Subject: utils: add utility to compute integer value from string literal X-Git-Tag: v0.3.0~231 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=cc92f5ca9478b0785d8286affb7d367eaec2fe94;p=alsa-gobject.git utils: add utility to compute integer value from string literal The conversion between string literal and integer is required in some includes libraries. This commit adds utility for it. Signed-off-by: Takashi Sakamoto --- diff --git a/src/ctl/query.c b/src/ctl/query.c index 560cf60..373b69d 100644 --- a/src/ctl/query.c +++ b/src/ctl/query.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "privates.h" +#include + #include #include #include @@ -153,12 +155,9 @@ void alsactl_get_card_id_list(guint **entries, gsize *entry_count, if (dev != NULL) { const char *sysnum = udev_device_get_sysnum(dev); long val; - char *endptr; - errno = 0; - val = strtol(sysnum, &endptr, 10); - if (errno == 0 && *endptr == '\0' && val >= 0) { - (*entries)[index] = (guint)val; + if (!long_from_string(sysnum, &val)) { + (*entries)[index] = val; ++index; } udev_device_unref(dev); diff --git a/src/hwdep/query.c b/src/hwdep/query.c index 9731ab8..c345a87 100644 --- a/src/hwdep/query.c +++ b/src/hwdep/query.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "privates.h" +#include + #include #include #include @@ -183,11 +185,8 @@ void alsahwdep_get_device_id_list(guint card_id, guint **entries, if (dev != NULL) { const char *sysnum = udev_device_get_sysnum(dev); long val; - char *endptr; - errno = 0; - val = strtol(sysnum, &endptr, 10); - if (errno == 0 && *endptr == '\0' && val >= 0) { + if (!long_from_string(sysnum, &val)) { (*entries)[index] = (guint)val; ++index; } diff --git a/src/rawmidi/query.c b/src/rawmidi/query.c index 2f40f8e..e078b39 100644 --- a/src/rawmidi/query.c +++ b/src/rawmidi/query.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "privates.h" +#include + #include #include #include @@ -183,11 +185,8 @@ void alsarawmidi_get_device_id_list(guint card_id, guint **entries, if (dev != NULL) { const char *sysnum = udev_device_get_sysnum(dev); long val; - char *endptr; - errno = 0; - val = strtol(sysnum, &endptr, 10); - if (errno == 0 && *endptr == '\0' && val >= 0) { + if (!long_from_string(sysnum, &val)) { (*entries)[index] = (guint)val; ++index; } diff --git a/src/timer/query.c b/src/timer/query.c index d52df29..38904e9 100644 --- a/src/timer/query.c +++ b/src/timer/query.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "privates.h" +#include + #include #include #include @@ -305,7 +307,7 @@ static void timer_get_node_param_value(const char *param_name, char *buf, int fd; ssize_t len; long v; - char *term; + int err; snprintf(literal, sizeof(literal), SYSFS_SND_TIMER_NODE "parameters/%s", param_name); @@ -322,11 +324,9 @@ static void timer_get_node_param_value(const char *param_name, char *buf, goto end; } - v = strtol(buf, &term, 10); - if (errno > 0) - generate_file_error(error, errno, "strtol()"); - else if (*term != '\n') - generate_file_error(error, EIO, "strtol()"); + err = long_from_string(buf, &v); + if (err < 0) + generate_file_error(error, -err, "Fail to parse snd_timer module parameter as integer value"); else *val = (int)v; end: diff --git a/src/utils/meson.build b/src/utils/meson.build index 0279814..20cf8d0 100644 --- a/src/utils/meson.build +++ b/src/utils/meson.build @@ -3,6 +3,7 @@ headers = [ ] sources = [ + 'string.c', ] dependencies = [ diff --git a/src/utils/string.c b/src/utils/string.c new file mode 100644 index 0000000..72689de --- /dev/null +++ b/src/utils/string.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +#include +#include + +long long_from_string(const char *literal, long *number) +{ + long val; + char *endptr; + + errno = 0; + val = strtol(literal, &endptr, 10); + if (errno > 0) + return -errno; + if (!endptr || endptr == literal || *endptr != 0) + return -EINVAL; + *number = val; + return 0; +} diff --git a/src/utils/utils.h b/src/utils/utils.h index fcefa93..4325785 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -2,4 +2,6 @@ #ifndef __ALSA_GOBJECT_UTILS_H__ #define __ALSA_GOBJECT_UTILS_H__ +long long_from_string(const char *literal, long *number); + #endif