summaryrefslogtreecommitdiff
path: root/standard.c
blob: afe6ad4b053fff7c2314df058f0499dc377199ae (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "maths.h"
#include "memory.h"
#include "standard.h"

int string_equal(const char* a, const char* b) {
	while (*a && *b) {
		if (*a != *b) { return 0; }
		a++; b++;
	}
	return 1;
}

int string_copy(char* dst, const char* src) {
	int i;
	for (i = 0; *src; src++, dst++, i++) {
		*dst = *src;
	}
	return i;
}

int string_len(const char* s) {
	int l;
	for (l = 0; *s; s++, l++);
	return l;
}

char* dup_string(Arena* a, const char* s) {
	int size = string_len(s) + 1;
	char* d = arena_alloc_aligned(a, size, 1);
	string_copy(d, s);
	return d;
}

int int_to_buf(int n, char* buf) {
	int i, sign, t;
	unsigned n1;

	if(n == 0) {
		buf[0] = '0';
		buf[1] = '\0';
		return 1;
	}

	i = 0;
	sign = n < 0;

	n1 = sign ? -n : n;

	while (n1 != 0) {
		buf[i++] = n1 % 10 + '0';
		n1 =n1 / 10;
	}

	if(sign) {
		buf[i++] = '-';
 	}

	buf[i] = '\0';

	for (t = 0; t < i/2; t++) {
		buf[t] ^= buf[i-t-1];
		buf[i-t-1] ^= buf[t];
		buf[t] ^= buf[i-t-1];
	}

	return i;
}

int f_to_buf(int f, char* buf) {
	int frac, w, i;

	frac = (((f) % (1 << fbits)) * 100) / (1 << fbits);
	w = f >> fbits;

	i = 0;

	if (f < 0) {
		buf[i] = '-';
		i++;
	}

	i += int_to_buf(absolute(w), buf + i);
	buf[i] = '.';
	i++;
	i += int_to_buf(absolute(frac), buf + i);

	return i;
}