-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
51 lines (42 loc) · 990 Bytes
/
util.c
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
#include "util.h"
void strcpy(char dest[], const char src[]) {
int i = 0;
while (src[i] != 0) {
dest[i] = src[i];
i++;
}
/* null terminate string */
dest[i] = '\0';
}
void itoa(u32 num, char dest[], u32 base) {
/* Guard for base 0, since we divide by base */
if (base == 0) return;
u32 i = 0;
/* Use do..while to handle 0 case */
do {
u32 remainder = num % base;
dest[i++] = (remainder > 9) ? 'a' + remainder - 10 : '0' + remainder;
num /= base;
} while (num != 0);
/* FIXME: Might want to remove these base specific prefixes */
switch (base) {
case 2:
dest[i++] = 'b';
dest[i++] = '0';
break;
case 16:
dest[i++] = 'x';
dest[i++] = '0';
break;
}
/* null terminate string */
dest[i] = '\0';
str_reverse(dest, i);
}
void str_reverse(char str[], u32 len) {
for (u32 i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
}