Last active
July 27, 2018 05:40
-
-
Save AGhost-7/cdc3f9f11faef1457dfa85ef7137d63a to your computer and use it in GitHub Desktop.
Revisions
-
AGhost-7 revised this gist
Jul 27, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -80,8 +80,8 @@ main() struct text text = { .history_buffer_length = 4, .body = body, .history_buffer_read_index = 0, .history_buffer_write_index = 0, .history_buffer = history_buffer }; -
AGhost-7 revised this gist
Jul 27, 2018 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,12 +25,12 @@ text_body_insert(struct text *text, uint8_t line, uint8_t column, char *characte { // It should not be possible to go out of bounds. if (text->body_lines < line) { return -1; } // Implement insertion along with grow logic. return 0; } int @@ -51,9 +51,9 @@ text_insert(struct text *text, uint8_t line, uint8_t column, char * characters) record.characters = characters; record.line = line; record.column = column; return 0; } return -1; } void @@ -75,10 +75,10 @@ int main() { char body[0][0]; struct history_record history_buffer[4]; struct text text = { .history_buffer_length = 4, .body = body, .history_buffer_read_index = 10, .history_buffer_write_index = 10, -
AGhost-7 created this gist
Jul 27, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,100 @@ #include <stdlib.h> #include <stdint.h> #include <stddef.h> struct history_record { uint8_t line; uint8_t column; char * characters; }; struct text { uint8_t history_buffer_length; uint8_t history_buffer_write_index; uint8_t history_buffer_read_index; uint8_t body_lines; // Would need some more things to track this growable string, but this // isn't the focus of the exercise. char ** body; struct history_record history_buffer []; }; int text_body_insert(struct text *text, uint8_t line, uint8_t column, char *characters) { // It should not be possible to go out of bounds. if (text->body_lines < line) { return 0; } // Implement insertion along with grow logic. return 1; } int text_insert(struct text *text, uint8_t line, uint8_t column, char * characters) { if (text_body_insert(text, line, column, characters)) { text->history_buffer_write_index++; if(text->history_buffer_write_index >= text->history_buffer_length) { // Since this is a circular buffer, once we hit the array boundary we // just restart the index. This means that we'll write on top of history. // The size of the array determines how much history we can hold on to. text->history_buffer_write_index = 0; } struct history_record record = text->history_buffer[text->history_buffer_write_index]; if (record.characters != NULL) { free(record.characters); } record.characters = characters; record.line = line; record.column = column; return 1; } return 0; } void text_delete(struct text *text, uint8_t line, uint8_t column, uint8_t characters) { } void text_undo(struct text *text) { } void text_redo(struct text *text) { } int main() { char body[0][0]; struct history_record history_buffer[10]; struct text text = { .history_buffer_length = 20, .body = body, .history_buffer_read_index = 10, .history_buffer_write_index = 10, .history_buffer = history_buffer }; text_insert(&text, 0, 0, "h"); text_insert(&text, 0, 1, "e"); text_insert(&text, 0, 2, "l"); text_insert(&text, 0, 3, "l"); text_insert(&text, 0, 4, "0"); text_undo(&text); text_undo(&text); text_redo(&text); text_redo(&text); return 0; }