diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/editor.c | 116 | ||||
-rw-r--r-- | src/editor.h | 35 | ||||
-rw-r--r-- | src/main.c | 9 |
3 files changed, 156 insertions, 4 deletions
diff --git a/src/editor.c b/src/editor.c new file mode 100644 index 0000000..1087ae4 --- /dev/null +++ b/src/editor.c @@ -0,0 +1,116 @@ +/* parc + * Copyright (C) 2025 Ekaitz Zarraga <ekaitz@elenq.tech> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> +#include "piece-table.h" + +typedef struct _string + { + char *start; + size_t length; + } +string; + +void +string_init (string *str) +{ + str->start = NULL; + str->length = 0; +} + +string +read_file (const char *path) +{ + /* + * The buffer has to be freed later. + */ + string out; + FILE *in; + char *buff = NULL; + size_t read = 0, total_size = 0, block_size = 1024; + in = fopen (path, "r"); /* TODO: Error handling!*/ + do + { + buff = realloc (buff, sizeof (*buff) * (total_size + block_size)); + read = fread (buff + total_size, sizeof (*buff), block_size, in); + total_size += read; + } + while (read == block_size ); + buff = realloc (buff, sizeof (*buff) * total_size); + fclose (in); + + out.start = buff; + out.length = total_size; + return out; +} + + + + + +typedef struct _editor + { + piece_table *pt; + const char *path; + string orig; + } +editor; + +/* + * https://texteditors.org/cgi-bin/wiki.pl?Implementing_Undo_For_Text_Editors + */ + +editor * +editor_create (void) +{ + editor *ed = malloc (sizeof (*ed)); + ed->pt = piece_table_create_from_str ("", 0); + ed->path = NULL; + string_init (&ed->orig); + return ed; +} + +void +editor_open (editor *ed, const char *path) +{ + ed->path = path; + piece_table_destroy (ed->pt); + + ed->orig = read_file (ed->path); + ed->pt = piece_table_create_from_str (ed->orig.start, ed->orig.length); +} + +void +editor_destroy (editor *ed) +{ + free (ed->orig.start); + piece_table_destroy (ed->pt); + free (ed); +} + +void +editor_display (editor *ed) +{ + /* Debug only */ + size_t len = piece_table_length (ed->pt); + char *disp = malloc (sizeof (*disp) * len + 1); + piece_table_to_string (ed->pt, disp, len); + printf ("%s", disp); + free (disp); +} diff --git a/src/editor.h b/src/editor.h new file mode 100644 index 0000000..d0b382f --- /dev/null +++ b/src/editor.h @@ -0,0 +1,35 @@ +/* parc + * Copyright (C) 2025 Ekaitz Zarraga <ekaitz@elenq.tech> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef EDITOR_H +#define EDITOR_H + +typedef struct _editor editor; + +editor *editor_create (void); +void editor_open (editor *ed, const char *path); +void editor_destroy (editor *ed); + +/* TODO */ +void editor_move_cursor (editor *ed, size_t where); +void editor_insert_at (editor *ed, size_t where, const char *x); +void editor_delete_at (editor *ed, size_t where, size_t count); + +/* Debug only */ +void editor_display (editor *ed); + +#endif @@ -16,13 +16,14 @@ */ #include <stdio.h> -#include "ui.h" +#include "editor.h" int main (int argc, char* argv[]) { - ui *ui = ui_create (); - ui_loop (ui); - ui_destroy (ui); + editor *ed = editor_create (); + editor_open (ed, "src/main.c"); + editor_display (ed); + editor_destroy (ed); return 0; } |