]> pilppa.org Git - libplp.git/blobdiff - src/str_util.c
api fixes and cleanups
[libplp.git] / src / str_util.c
index aca2fd8488a9a055e919f2e38ebc413e55765b68..abf521b6622467521484b7c3afdd46b270bad795 100644 (file)
@@ -8,6 +8,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
+#include <stdbool.h>
+
+#include <log.h>
 
 char *get_as_hex_str(const char *byte_arr, int byte_count) {
        int     ii;
@@ -33,3 +37,26 @@ char *get_as_hex_str(const char *byte_arr, int byte_count) {
        }
        return ret_val;
 }
+
+bool parse_long(const char *str, long *result) {
+       int     new_result;
+       char    *endptr;
+       bool    ret_val;
+
+       ret_val         = false;
+       errno           = 0;
+       new_result      = strtol(str, &endptr, 10);
+       if (errno != 0) {
+               log_error("invalid input %s, could not convert to integer.\n", str);
+       }
+       else {
+               if (endptr == str) {
+                       log_error("invalid input %s, could not convert to integer.\n", str);
+               }
+               else {
+                       *result = new_result;
+                       ret_val = true;
+               }
+       }
+       return ret_val;
+}