summaryrefslogtreecommitdiff
path: root/firmware/src/editor.c
diff options
context:
space:
mode:
authorCamil Staps2015-08-20 22:37:23 +0200
committerCamil Staps2015-08-20 22:46:47 +0200
commit3056e083d78b6edec6bab1ebac3fa9e3708644d7 (patch)
tree80dd8334e7a598d4d5080778ed5dcf3b5769392e /firmware/src/editor.c
Initial commit
Diffstat (limited to 'firmware/src/editor.c')
-rw-r--r--firmware/src/editor.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/firmware/src/editor.c b/firmware/src/editor.c
new file mode 100644
index 0000000..7b5eeb1
--- /dev/null
+++ b/firmware/src/editor.c
@@ -0,0 +1,87 @@
+#include "editor.h"
+#include <string.h>
+
+Editor newEditor(unsigned int size) {
+ unsigned char* block = calloc(1, size + 1);
+ if (block == NULL)
+ return NULL;
+
+ Editor editor;
+ editor.front.start = editor.front.end = block;
+ editor.back.start = editor.back.end = block + size;
+
+ return editor;
+}
+
+inline void freeEditor(Editor editor) {
+ free(editor.front.start);
+}
+
+inline unsigned int getFreeSpace(Editor editor) {
+ return editor.back.start - editor.front.end;
+}
+
+inline unsigned isFull(Editor editor) {
+ return getFreeSpace(editor) == NULL;
+}
+
+unsigned char* getNearCursor(Editor editor, signed int start, signed int end) {
+ unsigned char* block = (unsigned char*) calloc(1, end - start + 1);
+ if (block == NULL)
+ return NULL;
+
+ memcpy(block, editor.front.end + start, min(end - start, max(0, 0 - start)));
+ memcpy(block + max(0, 0 - start), editor.back.start + max(0, start), end - max(0, start));
+ return block;
+}
+
+void moveCursor(Editor editor, signed int change) {
+ if (change > 0) {
+ change = max(editor.back.end - editor.back.start, change);
+ memcpy(editor.front.end, editor.back.start, change);
+ editor.front.end += change;
+ editor.back.start += change;
+ } else if (change < 0) {
+ change = max(editor.front.end - editor.front.start, 0 - change);
+ memcpy(editor.back.start - change, editor.front.end - change, change);
+ editor.front.end -= change;
+ editor.back.start -= change;
+ }
+}
+
+inline void writeAtCursor(Editor editor, unsigned char character) {
+ editor.front.end++;
+ *editor.front.end = character;
+}
+
+void writeStringAtCursor(Editor editor, unsigned char* string) {
+ editor.front.end++;
+ memcpy(editor.front.end, string, strlen(string));
+ editor.front.end += strlen(string) - 1;
+}
+
+inline void writeAfterCursor(Editor editor, unsigned char character) {
+ editor.back.start--;
+ *editor.back.start = character;
+}
+
+void writeStringAfterCursor(Editor editor, unsigned char* string) {
+ editor.back.start -= strlen(string);
+ memcpy(editor.back.start, string, strlen(string));
+}
+
+inline void deleteAtCursor(Editor editor) {
+ editor.front.end--;
+}
+
+inline void deleteNAtCursor(Editor editor, unsigned int length) {
+ editor.front.end -= length;
+}
+
+inline void deleteAfterCursor(Editor editor) {
+ editor.back.start++;
+}
+
+inline void deleteNAfterCursor(Editor editor, unsigned int length) {
+ editor.back.start += length;
+}