]> sigrok.org Git - libsigrok.git/blobdiff - strutil.c
probe names: Fix cosmetics, add docs, fix off-by-one.
[libsigrok.git] / strutil.c
index 9bdf4336e4394cc1ac851b4f3b50cbe6fc4cee31..ef6d9633a28ba84be38df1ebd7d12bb93e2c5068 100644 (file)
--- a/strutil.c
+++ b/strutil.c
@@ -21,8 +21,8 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sigrok.h>
-#include <sigrok-internal.h>
+#include "sigrok.h"
+#include "sigrok-internal.h"
 
 /**
  * Convert a numeric samplerate value to its "natural" string representation.
@@ -186,19 +186,19 @@ char **sr_parse_triggerstring(struct sr_device *device,
  * Spaces (but not other whitespace) between value and suffix are allowed.
  *
  * @param sizestring A string containing a (decimal) size value.
- * @return The string's size value as uint64_t.
+ * @param size Pointer to uint64_t which will contain the string's size value.
+ * @return SR_OK or error code
  *
- * TODO: Error handling.
  */
-uint64_t sr_parse_sizestring(const char *sizestring)
+int sr_parse_sizestring(const char *sizestring, uint64_t *size)
 {
-       int multiplier;
-       uint64_t val;
+       int multiplier, done;
        char *s;
 
-       val = strtoull(sizestring, &s, 10);
+       *size = strtoull(sizestring, &s, 10);
        multiplier = 0;
-       while (s && *s && multiplier == 0) {
+       done = FALSE;
+       while (s && *s && multiplier == 0 && !done) {
                switch (*s) {
                case ' ':
                        break;
@@ -215,15 +215,18 @@ uint64_t sr_parse_sizestring(const char *sizestring)
                        multiplier = SR_GHZ(1);
                        break;
                default:
-                       val = 0;
-                       multiplier = -1;
+                       done = TRUE;
+                       s--;
                }
                s++;
        }
        if (multiplier > 0)
-               val *= multiplier;
+               *size *= multiplier;
+
+       if (*s && strcasecmp(s, "Hz"))
+               return SR_ERR;
 
-       return val;
+       return SR_OK;
 }
 
 /**
@@ -267,3 +270,17 @@ uint64_t sr_parse_timestring(const char *timestring)
 
        return time_msec;
 }
+
+gboolean sr_parse_boolstring(const char *boolstr)
+{
+       if (!boolstr)
+               return FALSE;
+
+       if (!g_strcasecmp(boolstr, "true") || 
+           !g_strcasecmp(boolstr, "yes") ||
+           !g_strcasecmp(boolstr, "on") ||
+           !g_strcasecmp(boolstr, "1")) 
+               return TRUE;
+
+       return FALSE;
+}