aboutsummaryrefslogtreecommitdiffstats
path: root/shelpers.c
blob: 20ec0cc08f486f5169edd27380890e551077424c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "shelpers.h"

#include <memory.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);
	clone[n] = '\0';
	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;
}