]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
utils: add utility to compute integer value from string literal
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Sat, 2 Apr 2022 01:46:22 +0000 (10:46 +0900)
committer坂本 貴史 <o-takashi@sakamocchi.jp>
Sat, 2 Apr 2022 13:19:28 +0000 (22:19 +0900)
The conversion between string literal and integer is required in some
includes libraries.

This commit adds utility for it.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
src/ctl/query.c
src/hwdep/query.c
src/rawmidi/query.c
src/timer/query.c
src/utils/meson.build
src/utils/string.c [new file with mode: 0644]
src/utils/utils.h

index 560cf609f24c30673e1d780d92548ccba0030f6c..373b69df610645a7abe6c9907cacc382ffbfed13 100644 (file)
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
 #include "privates.h"
 
+#include <utils.h>
+
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
@@ -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);
index 9731ab8c022b7bbaf0b10c23569acc2b677a60b4..c345a870ab8953677193657f53c8dddcf585d176 100644 (file)
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
 #include "privates.h"
 
+#include <utils.h>
+
 #include <stdio.h>
 #include <errno.h>
 #include <stdbool.h>
@@ -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;
             }
index 2f40f8e2613a4a10425860ec7fdccf014de35789..e078b3989343dc41f91e450e127e1cab0c10aee4 100644 (file)
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
 #include "privates.h"
 
+#include <utils.h>
+
 #include <stdio.h>
 #include <errno.h>
 #include <stdbool.h>
@@ -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;
             }
index d52df29ccb84ac78fcc71acac7ad702ad4ae6052..38904e998aa424f2e111cc10075087c6da8f0614 100644 (file)
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
 #include "privates.h"
 
+#include <utils.h>
+
 #include <stdio.h>
 #include <stdbool.h>
 #include <errno.h>
@@ -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:
index 0279814101bb161b6c45de2809111f096b2d36a2..20cf8d00531b6c4260c1bbf1b0c77308da51358f 100644 (file)
@@ -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 (file)
index 0000000..72689de
--- /dev/null
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+#include <stdlib.h>
+#include <errno.h>
+
+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;
+}
index fcefa937f230b19d8b484d6d44cda8814a917dff..43257855763771a9ed02dc36270c0abd1ab6e330 100644 (file)
@@ -2,4 +2,6 @@
 #ifndef __ALSA_GOBJECT_UTILS_H__
 #define __ALSA_GOBJECT_UTILS_H__
 
+long long_from_string(const char *literal, long *number);
+
 #endif