summaryrefslogtreecommitdiff
path: root/example/example.c
blob: bce70bb6039e3332a424299ac88f8b71150e0cde (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "prb.h"

#define MAX_NODES 10

typedef struct {
	i32	i32_element;
	i16	i16_element;
	u8	u8_element;
} type_t;

typedef struct stack_node_t {
	i32			value;
	struct stack_node_t	*next;
} stack_node_t;

typedef struct {
	stack_node_t	*first;
	stack_node_t	*last;
} stack_t;

typedef struct node_t {
	i32		value;
	struct node_t	*next;
	struct node_t	*prev;
} node_t;

typedef struct {
	node_t	*first;
	node_t	*last;
} list_t;

i32 main(void)
{
	// os
	printf("os: ");
	switch (OS) {
	case OS_LINUX:
		printf("linux\n");
		break;
	case OS_WINDOWS:
		printf("windows\n");
		break;
	default:
		printf("unsupported\n");
		return 1;
	}

	arena_t path_arena = alloc_arena(MAX_PATH);
	char *dir = sys_getbindir(&path_arena);
	printf("bin directory \"%s\"\n", dir);

	// macros
	u64 offset = offsetof(type_t, u8_element);
	assert(offset == 6);
	printf("offsetof(type_t, u8_element) = %lu\n", offset);
	i32 clamped = clamp(0, -1, 1);
	assert(clamped == 0);
	printf("clamp(0, -1, 1) = %d\n", clamped);
	clamped = clamp(0, 2, 1);
	assert(clamped == 1);
	printf("clamp(0, 2, 1) = %d\n", clamped);

	i32 nums[] = {0, 1, 2, 3};
	printf("mem0: [");
	for (i32 i = 0; i < array_count(nums); ++i)
		printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", ");
	memzero(nums, sizeof(nums));
	printf("] -> [");
	for (i32 i = 0; i < array_count(nums); ++i) {
		assert(!nums[i]);
		printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", ");
	}
	printf("]\n");

	printf("\n");

	printf("Singly linked list (stack for example):\n");

	stack_node_t stack_nodes[MAX_NODES];
	stack_t stack = {0};
	for (i32 i = 0; i < MAX_NODES; ++i) {
		stack_node_t *node = stack_nodes+i;
		node->value = i;
		sllpush(stack.first, stack.last, node);
	}

	for (stack_node_t *node = stack.first; node; node = node->next)
		printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));

	sllpop(stack.first, stack.last);

	printf("After pop:\n");
	for (stack_node_t *node = stack.first; node; node = node->next)
		printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
	
	node_t nodes[MAX_NODES];
	list_t list = {0};
	for (i32 i = 0; i < MAX_NODES; ++i) {
		node_t *node = nodes+i;
		node->value = i;
		dllpushback(list.first, list.last, node);
	}
	
	printf("Doubly linked list:\n");
	for (node_t *node = list.first; node; node = node->next)
		printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
	
	printf("Remove odd numbers:\n");
	for (node_t *node = list.first; node; node = node->next)
		if (node->value % 2 == 1)
			dllremove(list.first, list.last, node);
	
	for (node_t *node = list.first; node; node = node->next)
		printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
	
	printf("\n");

	printf("I'm pretty sure the vectors are fine\n");
	printf("At least for the last few months :)\n\n");

	printf("Arenas:\n");

	arena_t arena = alloc_arena(10);
	u8 *first = push_arena(&arena, 5);
	prb_memset(first, 5, 5);
	u8 *second = push_arena(&arena, 5);
	prb_memset(second, 10, 5);
	printf("|");
	for (i32 i = 0; i < 10; i++)
		printf("%x%s", first[i], ((i+1) % 5 == 0) ? "|" : " ");
	printf("\n");
	release_arena(&arena);

	printf("Strings:\n");

	arena_t str_arena = alloc_arena(0);
	str8_list_t strlist = {0};
	str8_list_push(&str_arena, &strlist, 0, str8lit("first "));
	str8_list_push(&str_arena, &strlist, 0, str8lit("second "));
	str8_list_push(&str_arena, &strlist, 0, str8lit("third\n"));
	str8_print(str8lit("strlist: "));
	str8_list_print(&strlist);

	str8_t fstr = str8_pushf(&str_arena, "Formatted string: %d\n", 69);
	str8_print(fstr);

	memzero_struct(&strlist);
	str8_list_pushf(&str_arena, &strlist, 0, "first %d -> ", 34);
	str8_list_pushf(&str_arena, &strlist, 0, "second: %d -> ", 35);
	str8_list_pushf(&str_arena, &strlist, 0, "sum: %d\n", 34+35);
	str8_list_pushf(&str_arena, &strlist, 1, "%s", "To the front -> ");

	str8_print(str8lit("Formatted string list:\n"));
	str8_list_print(&strlist);

	str8_t catstr = str8_list_join(&str_arena, &strlist);
	str8_print(str8lit("Concatenated string list:\n"));
	str8_print(catstr);

	release_arena(&str_arena);

	const char *filename = "build.sh";
	arena = alloc_arena(megabytes(1));
	char *content;
	if (sys_read_file(&arena, &content, filename))
		printf("%s content:\n%s", filename, content);
	else
		printf("failed to read %s\n", filename);
	release_arena(&arena);

	return(0);
}