use stb like a normal person (totally didnt waste an hour)

This commit is contained in:
2023-12-21 19:42:50 +01:00
parent 44a4374fd1
commit 71e935c8ad
4 changed files with 1939 additions and 38 deletions

1895
3rd-party/stb-ds.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,17 +1,14 @@
.DEFAULT_GOAL := purge .DEFAULT_GOAL := purge
llist.o:
cc -c engine/llist.c -o llist.o
todo.o: todo.o:
cc -c engine/todo.c -o todo.o gcc -c engine/todo.c -o todo.o
main.o: main.o:
cc -c main.c -o main.o gcc -c main.c -o main.o
purge: purge:
rm -f *.o rm -f *.o
make all make all
all: todo.o main.o llist.o all: todo.o main.o
cc todo.o main.o llist.o -o todd gcc todo.o main.o -o todd

View File

@@ -12,7 +12,7 @@ void todo_free_item(TodoItem *item) {
TodoItem todo_create_item(char *title) { TodoItem todo_create_item(char *title) {
TodoItem item; TodoItem item;
item.title_length = strlen(title); item.title_length = strlen(title);
item.title = malloc(item.title_length); item.title = malloc(item.title_length + 1);
strcpy(item.title, title); strcpy(item.title, title);
item.completed = false; item.completed = false;

69
main.c
View File

@@ -2,10 +2,14 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "engine/todo.h" #include "engine/todo.h"
#include "engine/llist.h"
#define MAX_TODOS 10 #define MAX_TODOS 10
#define SAVE_FILE "todos.todd" #define SAVE_FILE "todos.todd"
#define STB_DS_IMPLEMENTATION
#include "3rd-party/stb-ds.h"
TodoItem *todos = NULL;
enum Command { enum Command {
ADD = 'a', ADD = 'a',
@@ -18,8 +22,6 @@ enum Command {
LOAD_FROM_FILE = 'l', LOAD_FROM_FILE = 'l',
}; };
llist *todos;
void add_command_handler() { void add_command_handler() {
char title[TODO_MAX_TITLE_LENGTH]; char title[TODO_MAX_TITLE_LENGTH];
printf("Enter todo title: "); printf("Enter todo title: ");
@@ -28,11 +30,7 @@ void add_command_handler() {
title[strlen(title) - 1] = '\0'; title[strlen(title) - 1] = '\0';
TodoItem item = todo_create_item(title); TodoItem item = todo_create_item(title);
if (llist_length(todos) == 0) { arrput(todos, item);
todos = llist_create(&item);
} else {
llist_push(todos, &item);
}
} }
void mark_command_handler() { void mark_command_handler() {
@@ -41,34 +39,44 @@ void mark_command_handler() {
printf("Enter todo index: "); printf("Enter todo index: ");
op_index = getc(stdin) - '0'; op_index = getc(stdin) - '0';
getc(stdin); // remove the newline character from the buffer getc(stdin); // remove the newline character from the buffer
} while (op_index < 0 || op_index >= llist_length(todos)); } while (op_index < 0 || op_index >= arrlen(todos));
todo_mark_item(todos[op_index]->item, !todos[op_index]->item->completed); TodoItem item = todos[op_index];
todo_mark_item(&item, !item.completed);
todos[op_index] = item;
} }
void print_command_handler() { void print_command_handler() {
llist_print(todos, todo_print_item); for (int i = 0; i < arrlen(todos); i++) {
printf("%d. ", i);
todo_print_item(&todos[i]);
}
} }
void remove_todo_at_index(int index) { void remove_command_handler() {
// TODO!: Implement this int op_index = -1;
/* for (int i = index; i < todos_count - 1; i++) { */ do {
/* todos[i] = todos[i + 1]; */ printf("Enter todo index: ");
/* } */ op_index = getc(stdin) - '0';
/* todos_count--; */ getc(stdin); // remove the newline character from the buffer
} while (op_index < 0 || op_index >= arrlen(todos));
arrdel(todos, op_index);
} }
void write_to_file_handler() { void write_to_file_handler() {
FILE *file = fopen(SAVE_FILE, "wb"); FILE *file = fopen(SAVE_FILE, "wb");
int todos_count = llist_length(todos);
int bs = 0; int bs = 0;
int todos_count = arrlen(todos);
// write a magic value "TODD" to the file // write a magic value "TODD" to the file
fwrite("TODD", sizeof(char), 4, file); fwrite("TODD", sizeof(char), 4, file);
// write the number of todos to the file // write the number of todos to the file
fwrite(&todos_count, sizeof(int), 1, file); fwrite(&todos_count, sizeof(int), 1, file);
// write each todo to the file // write each todo to the file
for (int i = 0; i < todos_count; i++) { for (int i = 0; i < todos_count; i++) {
char *buffer = todo_item_serialize(todos[i]->item, &bs); char *buffer = todo_item_serialize(&todos[i], &bs);
fwrite(buffer, sizeof(char), bs, file); fwrite(buffer, sizeof(char), bs, file);
free(buffer); free(buffer);
} }
@@ -85,10 +93,10 @@ int main(int argc, char **argv) {
switch (cmd) { switch (cmd) {
case ADD: case ADD:
if (llist_length(todos) + 1 > MAX_TODOS) { /* if (list_length(&todos) + 1 > MAX_TODOS) { */
printf("No more space for new todos\n"); /* printf("No more space for new todos\n"); */
break; /* break; */
} /* } */
add_command_handler(); add_command_handler();
break; break;
case MARK: case MARK:
@@ -98,6 +106,7 @@ int main(int argc, char **argv) {
print_command_handler(); print_command_handler();
break; break;
case REMOVE: case REMOVE:
remove_command_handler();
break; break;
case WRITE_TO_FILE: case WRITE_TO_FILE:
write_to_file_handler(); write_to_file_handler();
@@ -110,12 +119,12 @@ int main(int argc, char **argv) {
} }
} while (cmd != QUIT); } while (cmd != QUIT);
arrfree(todos);
// free all the todos /* for (int i = 0; i < list_length(&todos); i++) { */
int todos_count = llist_length(todos); /* todo_free_item(list_get(&todos, i)); */
/* } */
for (int i = 0; i < todos_count; i++) { /* list_free(&todos); */
todo_free_item(todos[i]->item); /* free(todos); */
llist_pop(todos);
}
} }