diff options
author | pommicket <pommicket@gmail.com> | 2023-01-01 22:56:36 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-01-01 22:56:36 -0500 |
commit | a3971fd612813c6544dbe63cb3d7e5d9c0b7b778 (patch) | |
tree | 4c89464fca8366301b121ef597dfee826d37a8bd | |
parent | e06b040fad63fe729de60781658778a0b008ca16 (diff) |
move functions from io.c to session.c
-rw-r--r-- | buffer.c | 15 | ||||
-rw-r--r-- | filesystem.h | 4 | ||||
-rw-r--r-- | io.c | 135 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | session.c | 153 |
5 files changed, 155 insertions, 153 deletions
@@ -169,21 +169,6 @@ static bool buffer_pos_valid(TextBuffer *buffer, BufferPos p) { return p.line < buffer->nlines && p.index <= buffer->lines[p.line].len; } -// write a buffer position to a file -void buffer_pos_write(BufferPos pos, FILE *fp) { - write_u32(fp, pos.line); - write_u32(fp, pos.index); -} - -// read a buffer position from a file, and validate it -BufferPos buffer_pos_read(TextBuffer *buffer, FILE *fp) { - BufferPos pos = {0}; - pos.line = read_u32(fp); - pos.index = read_u32(fp); - buffer_pos_validate(buffer, &pos); - return pos; -} - // are there any unsaved changes? bool buffer_unsaved_changes(TextBuffer *buffer) { if (buffer_is_untitled(buffer) && buffer_empty(buffer)) diff --git a/filesystem.h b/filesystem.h index 0c18494..03ae04a 100644 --- a/filesystem.h +++ b/filesystem.h @@ -40,8 +40,8 @@ int fs_mkdir(char const *path); // 0 if buf is too short to hold the cwd // -1 if we can't get the cwd for whatever reason. int fs_get_cwd(char *buf, size_t buflen); - -void fs_dir_entries_free(FsDirectoryEntry **entries) { +// free the entries generated by fs_list_directory.s +static void fs_dir_entries_free(FsDirectoryEntry **entries) { for (int i = 0; entries[i]; ++i) free(entries[i]); free(entries); @@ -1,135 +0,0 @@ -static void write_u8(FILE *fp, u8 x) { - putc(x, fp); -} - -static void write_u16(FILE *fp, u16 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_u32(FILE *fp, u32 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_u64(FILE *fp, u64 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_i8(FILE *fp, i8 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_i16(FILE *fp, i16 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_i32(FILE *fp, i32 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_i64(FILE *fp, i64 x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_float(FILE *fp, float x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_double(FILE *fp, double x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_char(FILE *fp, char x) { - fwrite(&x, sizeof x, 1, fp); -} - -static void write_bool(FILE *fp, bool x) { - putc(x, fp); -} - -static u8 read_u8(FILE *fp) { - return (u8)getc(fp); -} - -static u16 read_u16(FILE *fp) { - u16 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static u32 read_u32(FILE *fp) { - u32 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static u64 read_u64(FILE *fp) { - u64 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static i8 read_i8(FILE *fp) { - i8 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static i16 read_i16(FILE *fp) { - i16 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static i32 read_i32(FILE *fp) { - i32 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static i64 read_i64(FILE *fp) { - i64 x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static float read_float(FILE *fp) { - float x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static double read_double(FILE *fp) { - double x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static char read_char(FILE *fp) { - char x = 0; - fread(&x, sizeof x, 1, fp); - return x; -} - -static bool read_bool(FILE *fp) { - return (bool)getc(fp); -} - -static void write_cstr(FILE *fp, char const *cstr) { - fwrite(cstr, 1, strlen(cstr) + 1, fp); -} - -static void read_cstr(FILE *fp, char *out, size_t out_sz) { - char *p = out, *end = out + out_sz; - while (1) { - if (p >= end - 1) { - *p = '\0'; - break; - } - int c = getc(fp); - if (c == 0 || c == EOF) { - *p = '\0'; - break; - } - *p++ = (char)c; - } -} @@ -104,7 +104,6 @@ no_warn_end #error "Unrecognized operating system." #endif -#include "io.c" #include "time.c" #include "ted.h" #include "gl.c" @@ -1,6 +1,159 @@ #define SESSION_FILENAME "session.txt" #define SESSION_VERSION "\x7fTED0002" +static void write_u8(FILE *fp, u8 x) { + putc(x, fp); +} + +static void write_u16(FILE *fp, u16 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_u32(FILE *fp, u32 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_u64(FILE *fp, u64 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_i8(FILE *fp, i8 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_i16(FILE *fp, i16 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_i32(FILE *fp, i32 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_i64(FILE *fp, i64 x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_float(FILE *fp, float x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_double(FILE *fp, double x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_char(FILE *fp, char x) { + fwrite(&x, sizeof x, 1, fp); +} + +static void write_bool(FILE *fp, bool x) { + putc(x, fp); +} + +static u8 read_u8(FILE *fp) { + return (u8)getc(fp); +} + +static u16 read_u16(FILE *fp) { + u16 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static u32 read_u32(FILE *fp) { + u32 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static u64 read_u64(FILE *fp) { + u64 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static i8 read_i8(FILE *fp) { + i8 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static i16 read_i16(FILE *fp) { + i16 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static i32 read_i32(FILE *fp) { + i32 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static i64 read_i64(FILE *fp) { + i64 x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static float read_float(FILE *fp) { + float x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static double read_double(FILE *fp) { + double x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static char read_char(FILE *fp) { + char x = 0; + fread(&x, sizeof x, 1, fp); + return x; +} + +static bool read_bool(FILE *fp) { + return (bool)getc(fp); +} + +static void write_cstr(FILE *fp, char const *cstr) { + fwrite(cstr, 1, strlen(cstr) + 1, fp); +} + +static void read_cstr(FILE *fp, char *out, size_t out_sz) { + char *p = out, *end = out + out_sz; + while (1) { + if (p >= end - 1) { + *p = '\0'; + break; + } + int c = getc(fp); + if (c == 0 || c == EOF) { + *p = '\0'; + break; + } + *p++ = (char)c; + } +} + + +// write a buffer position to a file +static void buffer_pos_write(BufferPos pos, FILE *fp) { + write_u32(fp, pos.line); + write_u32(fp, pos.index); +} + +// read a buffer position from a file, and validate it +static BufferPos buffer_pos_read(TextBuffer *buffer, FILE *fp) { + BufferPos pos = {0}; + pos.line = read_u32(fp); + pos.index = read_u32(fp); + buffer_pos_validate(buffer, &pos); + return pos; +} + + static void session_write_node(Ted *ted, FILE *fp, u16 node_idx) { Node *node = &ted->nodes[node_idx]; write_u16(fp, node_idx); |