diff gui/util/string.c @ 36989:0b80003f6542

Relocate the cut functions. Put them into the string functions file.
author ib
date Thu, 27 Mar 2014 09:15:47 +0000
parents b33ff300128e
children fa8b6892b0bf
line wrap: on
line diff
--- a/gui/util/string.c	Thu Mar 27 02:22:17 2014 +0000
+++ b/gui/util/string.c	Thu Mar 27 09:15:47 2014 +0000
@@ -169,6 +169,51 @@
 }
 
 /**
+ * @brief Extract a part of a string delimited by a separator character.
+ *
+ * @param in string to be analyzed
+ * @param out pointer suitable to store the extracted part
+ * @param sep separator character
+ * @param num number of separator characters to be skipped before extraction starts
+ * @param maxout maximum length of extracted part (including the trailing null byte)
+ */
+void cutItemString(char *in, char *out, char sep, int num, size_t maxout)
+{
+    int n;
+    unsigned int i, c;
+
+    for (c = 0, n = 0, i = 0; in[i]; i++) {
+        if (in[i] == sep)
+            n++;
+        if (n >= num && in[i] != sep && c + 1 < maxout)
+            out[c++] = in[i];
+        if (n >= num && in[i + 1] == sep)
+            break;
+    }
+
+    if (c < maxout)
+        out[c] = 0;
+}
+
+/**
+ * @brief Extract a numeric part of a string delimited by a separator character.
+ *
+ * @param in string to be analyzed
+ * @param sep separator character
+ * @param num number of separator characters to be skipped before extraction starts
+ *
+ * @return extracted number (numeric part)
+ */
+int cutItemToInt(char *in, char sep, int num)
+{
+    char tmp[64];
+
+    cutItem(in, tmp, sep, num);
+
+    return atoi(tmp);
+}
+
+/**
  * @brief A strchr() that can handle NULL pointers.
  *
  * @param str string to examine