Skip to content

Instantly share code, notes, and snippets.

@AGhost-7
Last active July 27, 2018 05:40
Show Gist options
  • Save AGhost-7/cdc3f9f11faef1457dfa85ef7137d63a to your computer and use it in GitHub Desktop.
Save AGhost-7/cdc3f9f11faef1457dfa85ef7137d63a to your computer and use it in GitHub Desktop.
#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 -1;
}
// Implement insertion along with grow logic.
return 0;
}
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 0;
}
return -1;
}
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[4];
struct text text = {
.history_buffer_length = 4,
.body = body,
.history_buffer_read_index = 0,
.history_buffer_write_index = 0,
.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment