Skip to content

Instantly share code, notes, and snippets.

@AGhost-7
Last active July 27, 2018 05:40
Show Gist options
  • Select an option

  • Save AGhost-7/cdc3f9f11faef1457dfa85ef7137d63a to your computer and use it in GitHub Desktop.

Select an option

Save AGhost-7/cdc3f9f11faef1457dfa85ef7137d63a to your computer and use it in GitHub Desktop.

Revisions

  1. AGhost-7 revised this gist Jul 27, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions history.c
    Original 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 = 10,
    .history_buffer_write_index = 10,
    .history_buffer_read_index = 0,
    .history_buffer_write_index = 0,
    .history_buffer = history_buffer
    };

  2. AGhost-7 revised this gist Jul 27, 2018. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions history.c
    Original 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 0;
    return -1;
    }

    // Implement insertion along with grow logic.

    return 1;
    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 1;
    return 0;
    }
    return 0;
    return -1;
    }

    void
    @@ -75,10 +75,10 @@ int
    main()
    {
    char body[0][0];
    struct history_record history_buffer[10];
    struct history_record history_buffer[4];

    struct text text = {
    .history_buffer_length = 20,
    .history_buffer_length = 4,
    .body = body,
    .history_buffer_read_index = 10,
    .history_buffer_write_index = 10,
  3. AGhost-7 created this gist Jul 27, 2018.
    100 changes: 100 additions & 0 deletions history.c
    Original 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;
    }