diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-07-09 15:37:44 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-07-09 15:37:44 +0500 |
commit | 3f68a5ea045f3a097768c681ecf60989ca296ec1 (patch) | |
tree | 163cf9ad0631d839da7912afca03a9dadd120806 /example/example.c | |
parent | ea6cdf80a35f0a3c45833e94782cfd1ec7dae929 (diff) |
organized examples
Diffstat (limited to 'example/example.c')
-rw-r--r-- | example/example.c | 172 |
1 files changed, 0 insertions, 172 deletions
diff --git a/example/example.c b/example/example.c deleted file mode 100644 index bce70bb..0000000 --- a/example/example.c +++ /dev/null @@ -1,172 +0,0 @@ -#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); -} |