diff options
-rw-r--r-- | buffer.c | 5 | ||||
-rw-r--r-- | config.c | 66 | ||||
-rw-r--r-- | main.c | 34 | ||||
-rw-r--r-- | ted-base.c | 49 |
4 files changed, 133 insertions, 21 deletions
@@ -92,6 +92,11 @@ static void buffer_append_redo(TextBuffer *buffer, BufferEdit const *edit) { if (!buffer->redo_history) buffer_out_of_mem(buffer); } +static void *buffer_malloc(TextBuffer *buffer, size_t size) { + void *ret = malloc(size); + if (!ret) buffer_out_of_mem(buffer); + return ret; +} static void *buffer_calloc(TextBuffer *buffer, size_t n, size_t size) { void *ret = calloc(n, size); @@ -6,7 +6,69 @@ // thing2 = 454 // [section2] // asdf = 123 + +typedef enum { + SECTION_NONE, + SECTION_KEYBOARD +} Section; + void config_read(Ted *ted, char const *filename) { - char section_name[32] = {0}; - // @TODO + FILE *fp = fopen(filename, "rb"); + if (fp) { + u32 line_number = 1; + int line_cap = 4096; + char *line = ted_malloc(ted, (size_t)line_cap); + if (line) { + Section section = SECTION_NONE; + + while (fgets(line, line_cap, fp)) { + char *newline = strchr(line, '\n'); + if (newline || feof(fp)) { + if (newline) *newline = '\0'; + + bool error = false; + + // ok, we've now read a line. + switch (line[0]) { + case '#': // comment + case '\0': // blank line + break; + case '[': { // section header + #define SECTION_HEADER_HELP "Section headers should look like this: [section-name]" + char *closing = strchr(line, ']'); + if (!closing) { + ted_seterr(ted, "%s:" U32_FMT ": Unmatched [. " SECTION_HEADER_HELP, filename, line_number); + error = true; + } else if (closing[1] != '\0') { + ted_seterr(ted, "%s:" U32_FMT ": Text after section. " SECTION_HEADER_HELP, filename, line_number); + error = true; + } else { + *closing = '\0'; + char *section_name = line + 1; + if (streq(section_name, "keyboard")) { + section = SECTION_KEYBOARD; + } else { + ted_seterr(ted, "%s:" U32_FMT ": Unrecognized section: [%s].", filename, line_number, section_name); + error = true; + } + } + } break; + } + + if (error) break; + + ++line_number; + } else { + ted_seterr(ted, "%s:" U32_FMT ": Line is too long.", filename, line_number); + break; + } + } + } + free(line); + if (ferror(fp)) + ted_seterr(ted, "Error reading %s.", filename); + fclose(fp); + } else { + ted_seterr(ted, "Couldn't open file %s.", filename); + } } @@ -20,23 +20,9 @@ no_warn_end #include "string32.c" #include "arr.c" #include "buffer.c" - -typedef struct { - Command cmd; - i64 argument; -} KeyAction; - -#define SCANCODE_COUNT 0x120 // SDL scancodes should be less than this value. -// a "key combo" is some subset of {control, shift, alt} + some key. -#define KEY_COMBO_COUNT (SCANCODE_COUNT << 3) - -typedef struct { - TextBuffer *active_buffer; - KeyAction key_actions[KEY_COMBO_COUNT]; - char error[256]; -} Ted; - +#include "ted-base.c" #include "command.c" +#include "config.c" static void die(char const *fmt, ...) { char buf[256] = {0}; @@ -91,7 +77,16 @@ int main(void) { die("Couldn't load font: %s", text_get_err()); } - bool quit = false; + Ted *ted = calloc(1, sizeof *ted); + if (!ted) { + die("Not enough memory available to run ted."); + } + + config_read(ted, "ted.cfg"); + if (ted_haserr(ted)) { + die("Error reading config: %s", ted_geterr(ted)); + } + TextBuffer text_buffer; TextBuffer *buffer = &text_buffer; buffer_create(buffer, font); @@ -102,9 +97,10 @@ int main(void) { Uint32 time_at_last_frame = SDL_GetTicks(); + bool quit = false; while (!quit) { - #if DEBUG&&0 - printf("\033[H\033[2J"); + #if DEBUG + //printf("\033[H\033[2J"); #endif SDL_Event event; diff --git a/ted-base.c b/ted-base.c new file mode 100644 index 0000000..5ed6469 --- /dev/null +++ b/ted-base.c @@ -0,0 +1,49 @@ +typedef struct { + Command cmd; + i64 argument; +} KeyAction; + +#define SCANCODE_COUNT 0x120 // SDL scancodes should be less than this value. +// a "key combo" is some subset of {control, shift, alt} + some key. +#define KEY_COMBO_COUNT (SCANCODE_COUNT << 3) + +typedef struct { + TextBuffer *active_buffer; + KeyAction key_actions[KEY_COMBO_COUNT]; + char error[256]; +} Ted; + +// this is a macro so we get -Wformat warnings +#define ted_seterr(buffer, ...) \ + snprintf(ted->error, sizeof ted->error - 1, __VA_ARGS__) + +bool ted_haserr(Ted *ted) { + return ted->error[0] != '\0'; +} + +char const *ted_geterr(Ted *ted) { + return ted->error; +} + +static void ted_out_of_mem(Ted *ted) { + ted_seterr(ted, "Out of memory."); +} + +static void *ted_malloc(Ted *ted, size_t size) { + void *ret = malloc(size); + if (!ret) ted_out_of_mem(ted); + return ret; +} + +static void *ted_calloc(Ted *ted, size_t n, size_t size) { + void *ret = calloc(n, size); + if (!ret) ted_out_of_mem(ted); + return ret; +} + +static void *ted_realloc(Ted *ted, void *p, size_t new_size) { + void *ret = realloc(p, new_size); + if (!ret) ted_out_of_mem(ted); + return ret; +} + |