use stb like a normal person (totally didnt waste an hour)
This commit is contained in:
1895
3rd-party/stb-ds.h
vendored
Normal file
1895
3rd-party/stb-ds.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
11
Makefile
11
Makefile
@@ -1,17 +1,14 @@
|
||||
.DEFAULT_GOAL := purge
|
||||
|
||||
llist.o:
|
||||
cc -c engine/llist.c -o llist.o
|
||||
|
||||
todo.o:
|
||||
cc -c engine/todo.c -o todo.o
|
||||
gcc -c engine/todo.c -o todo.o
|
||||
|
||||
main.o:
|
||||
cc -c main.c -o main.o
|
||||
gcc -c main.c -o main.o
|
||||
|
||||
purge:
|
||||
rm -f *.o
|
||||
make all
|
||||
|
||||
all: todo.o main.o llist.o
|
||||
cc todo.o main.o llist.o -o todd
|
||||
all: todo.o main.o
|
||||
gcc todo.o main.o -o todd
|
||||
|
||||
@@ -12,7 +12,7 @@ void todo_free_item(TodoItem *item) {
|
||||
TodoItem todo_create_item(char *title) {
|
||||
TodoItem item;
|
||||
item.title_length = strlen(title);
|
||||
item.title = malloc(item.title_length);
|
||||
item.title = malloc(item.title_length + 1);
|
||||
strcpy(item.title, title);
|
||||
item.completed = false;
|
||||
|
||||
|
||||
69
main.c
69
main.c
@@ -2,10 +2,14 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "engine/todo.h"
|
||||
#include "engine/llist.h"
|
||||
|
||||
#define MAX_TODOS 10
|
||||
#define SAVE_FILE "todos.todd"
|
||||
#define STB_DS_IMPLEMENTATION
|
||||
|
||||
#include "3rd-party/stb-ds.h"
|
||||
|
||||
TodoItem *todos = NULL;
|
||||
|
||||
enum Command {
|
||||
ADD = 'a',
|
||||
@@ -18,8 +22,6 @@ enum Command {
|
||||
LOAD_FROM_FILE = 'l',
|
||||
};
|
||||
|
||||
llist *todos;
|
||||
|
||||
void add_command_handler() {
|
||||
char title[TODO_MAX_TITLE_LENGTH];
|
||||
printf("Enter todo title: ");
|
||||
@@ -28,11 +30,7 @@ void add_command_handler() {
|
||||
title[strlen(title) - 1] = '\0';
|
||||
|
||||
TodoItem item = todo_create_item(title);
|
||||
if (llist_length(todos) == 0) {
|
||||
todos = llist_create(&item);
|
||||
} else {
|
||||
llist_push(todos, &item);
|
||||
}
|
||||
arrput(todos, item);
|
||||
}
|
||||
|
||||
void mark_command_handler() {
|
||||
@@ -41,34 +39,44 @@ void mark_command_handler() {
|
||||
printf("Enter todo index: ");
|
||||
op_index = getc(stdin) - '0';
|
||||
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() {
|
||||
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) {
|
||||
// TODO!: Implement this
|
||||
/* for (int i = index; i < todos_count - 1; i++) { */
|
||||
/* todos[i] = todos[i + 1]; */
|
||||
/* } */
|
||||
/* todos_count--; */
|
||||
void remove_command_handler() {
|
||||
int op_index = -1;
|
||||
do {
|
||||
printf("Enter todo index: ");
|
||||
op_index = getc(stdin) - '0';
|
||||
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() {
|
||||
FILE *file = fopen(SAVE_FILE, "wb");
|
||||
int todos_count = llist_length(todos);
|
||||
int bs = 0;
|
||||
int todos_count = arrlen(todos);
|
||||
// write a magic value "TODD" to the file
|
||||
fwrite("TODD", sizeof(char), 4, file);
|
||||
// write the number of todos to the file
|
||||
fwrite(&todos_count, sizeof(int), 1, file);
|
||||
// write each todo to the file
|
||||
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);
|
||||
free(buffer);
|
||||
}
|
||||
@@ -85,10 +93,10 @@ int main(int argc, char **argv) {
|
||||
|
||||
switch (cmd) {
|
||||
case ADD:
|
||||
if (llist_length(todos) + 1 > MAX_TODOS) {
|
||||
printf("No more space for new todos\n");
|
||||
break;
|
||||
}
|
||||
/* if (list_length(&todos) + 1 > MAX_TODOS) { */
|
||||
/* printf("No more space for new todos\n"); */
|
||||
/* break; */
|
||||
/* } */
|
||||
add_command_handler();
|
||||
break;
|
||||
case MARK:
|
||||
@@ -98,6 +106,7 @@ int main(int argc, char **argv) {
|
||||
print_command_handler();
|
||||
break;
|
||||
case REMOVE:
|
||||
remove_command_handler();
|
||||
break;
|
||||
case WRITE_TO_FILE:
|
||||
write_to_file_handler();
|
||||
@@ -110,12 +119,12 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
} while (cmd != QUIT);
|
||||
|
||||
arrfree(todos);
|
||||
|
||||
// free all the todos
|
||||
int todos_count = llist_length(todos);
|
||||
|
||||
for (int i = 0; i < todos_count; i++) {
|
||||
todo_free_item(todos[i]->item);
|
||||
llist_pop(todos);
|
||||
}
|
||||
/* for (int i = 0; i < list_length(&todos); i++) { */
|
||||
/* todo_free_item(list_get(&todos, i)); */
|
||||
/* } */
|
||||
/* list_free(&todos); */
|
||||
/* free(todos); */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user