aboutsummaryrefslogtreecommitdiffstats
path: root/shelpers.c
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2018-02-07 09:26:36 -0500
committerClyne Sullivan <tullivan99@gmail.com>2018-02-07 09:26:36 -0500
commit2b8cf5e771cac4c2b087dc96a7ca05a459f630b5 (patch)
treed1e25ef8520e9df936c231af0d5b760177d6cbde /shelpers.c
parente3cbc8086c25d4fc1d9413815643b3ecaaee2062 (diff)
conditionals, returns, cleaner code
Diffstat (limited to 'shelpers.c')
-rw-r--r--shelpers.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/shelpers.c b/shelpers.c
new file mode 100644
index 0000000..0d2da24
--- /dev/null
+++ b/shelpers.c
@@ -0,0 +1,63 @@
+#include "shelpers.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+char *strclone(const char *s)
+{
+ char *clone = (char *)malloc(strlen(s) + 1);
+ strcpy(clone, s);
+ return clone;
+}
+
+char *strnclone(const char *s, uint32_t n)
+{
+ char *clone = (char *)malloc(n + 1);
+ strncpy(clone, s, n);
+ return clone;
+}
+
+uint8_t eol(int c)
+{
+ return c == '\n' || c == '\0';
+}
+
+uint8_t eot(int c)
+{
+ return eol(c) || c == ' ';
+}
+
+uint8_t eoe(int c)
+{
+ return eol(c) || c == ')';
+}
+
+uint32_t findend(const char *s, char o, char c)
+{
+ uint8_t indent = 0;
+ uint32_t i;
+ for (i = 1; !eol(s[i]); i++) {
+ if (s[i] == o) {
+ indent++;
+ } else if (s[i] == c) {
+ if (indent == 0)
+ break;
+ else
+ indent--;
+ }
+ }
+
+ return i;
+}
+
+void skipblank(const char *s, uint8_t (*cmp)(int), uint32_t *offset)
+{
+ uint32_t i = *offset;
+ while (!cmp(s[i])) {
+ if (s[i] != ' ' && s[i] != '\t')
+ break;
+ i++;
+ }
+ *offset = i;
+}
+