diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-12-03 11:07:04 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-12-03 11:07:04 -0500 |
commit | b2dea77f9698f06a8a9046f00ad00f4c3ebedfb0 (patch) | |
tree | 3fda1ba7d2c76a4af9282fd28d1b4ae639fa4669 | |
parent | 32b18a3e60b59957dd9dd237af9e2303aab93d3b (diff) |
fix file loading on windows
-rw-r--r-- | buffer.c | 5 | ||||
-rw-r--r-- | main.c | 8 |
2 files changed, 8 insertions, 5 deletions
@@ -43,6 +43,8 @@ static Status buffer_line_append_char(Line *line, char32_t c) { return true; } +// fp needs to be a binary file for this to work +// (because of the way we're checking the size of the file) Status buffer_load_file(TextBuffer *buffer, FILE *fp) { assert(fp); fseek(fp, 0, SEEK_END); @@ -59,7 +61,8 @@ Status buffer_load_file(TextBuffer *buffer, FILE *fp) { if (file_contents) { buffer->lines = calloc(1, sizeof *buffer->lines); // first line buffer->nlines = 1; - if (fread(file_contents, 1, file_size, fp) == file_size) { + size_t bytes_read = fread(file_contents, 1, file_size, fp); + if (bytes_read == file_size) { char32_t c = 0; mbstate_t mbstate = {0}; for (u8 *p = file_contents, *end = p + file_size; p != end; ) { @@ -19,9 +19,9 @@ static void die(char const *fmt, ...) { vsnprintf(buf, sizeof buf - 1, fmt, args); va_end(args); - // show a message box, and if that fails, print to stderr + // show a message box, and if that fails, print it if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", buf, NULL) < 0) { - fprintf(stderr, "%s\n", buf); + debug_println("%s\n", buf); } exit(EXIT_FAILURE); @@ -69,7 +69,7 @@ int main(void) { buffer_create(&text_buffer, font); { - FILE *fp = fopen("main.c", "r"); + FILE *fp = fopen("main.c", "rb"); assert(fp); bool success = buffer_load_file(&text_buffer, fp); fclose(fp); @@ -160,7 +160,7 @@ int main(void) { float x1 = 50, y1 = 50, x2 = window_widthf-50, y2 = window_heightf-50; buffer_render(&text_buffer, x1, y1, x2, y2); if (text_has_err()) { - printf("Text error: %s\n", text_get_err()); + debug_println("Text error: %s\n", text_get_err()); break; } } |