diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..08d9005 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/final/binary b/final/binary index 8b4cb0a..9e656ce 100755 Binary files a/final/binary and b/final/binary differ diff --git a/final/dynstr.c b/final/dynstr.c index a55a4b9..d535986 100644 --- a/final/dynstr.c +++ b/final/dynstr.c @@ -5,15 +5,15 @@ #include #include #include +#include typedef struct { - int size; - int capacity; + uint32_t size, capacity; char* data; -} String; +} String_t; -String str_with_capacity(int capacity) { - String s; +String_t str_with_capacity(int capacity) { + String_t s; /// allocate memory for 'capacity' chars s.data = (char*)calloc(capacity, sizeof(char)); @@ -24,8 +24,8 @@ String str_with_capacity(int capacity) { return s; } -String str_from_chars(char* string) { - String s; +String_t str_from_chars(const char* string) { + String_t s; s.data = (char*)calloc(strlen(string), sizeof(char)); strcpy(s.data, string); @@ -34,8 +34,8 @@ String str_from_chars(char* string) { return s; } -String str_from_slice(char* string, int len) { - String s; +String_t str_from_slice(const char* string, int len) { + String_t s; s.data = (char*)calloc(len, sizeof(char)); strncpy(s.data, string, len); @@ -44,22 +44,21 @@ String str_from_slice(char* string, int len) { return s; } - -String str_new() { +String_t str_new() { return str_with_capacity(1); } -int str_dealloc(String* self) { +int str_dealloc(String_t* self) { free(self->data); return 0; } -void str_push(String* self, char c) { +int str_push(String_t* self, char c) { // check size < capacity if (self->size < self->capacity) { self->data[self->size] = c; self->size++; - return; + return 0; } // reallocate to add capacity for an extra char @@ -68,10 +67,10 @@ void str_push(String* self, char c) { self->data[self->size] = c; self->size++; self->capacity = newcap; - return; + return 0; } -char str_pop(String* self) { +char str_pop(String_t* self) { if (self->size == 0) { return '\0'; } @@ -80,31 +79,33 @@ char str_pop(String* self) { return c; } -void str_insert(String* self, int index, char c) { +int str_insert(String_t* self, int index, char c) { if (index > self->size) { - return; + return -1; } self->size++; for (int i = self->size - 1; i > index; i--) { self->data[i] = self->data[i - 1]; } self->data[index] = c; + return 0; } -void str_remove(String* self, int index) { +int str_remove(String_t* self, int index) { if (index >= self->size) { - return; + return -1; } self->size--; for (int i = index; i < self->size; i++) { self->data[i] = self->data[i + 1]; } + return 0; } -String* str_lines(String* self, int* numlines) { +String_t* str_lines(String_t* self, int* numlines) { char* string = self->data; - String* lines = NULL; + String_t* lines = NULL; // find the number of lines in the file *numlines = 0; @@ -121,7 +122,7 @@ String* str_lines(String* self, int* numlines) { } // allocate memory for an array of pointers to each line - lines = (String*)malloc((*numlines + 1) * sizeof(String)); + lines = (String_t*)malloc((*numlines + 1) * sizeof(String_t)); int i = 0; char* start = string; @@ -139,15 +140,15 @@ String* str_lines(String* self, int* numlines) { end++; } } - // returns an array of String + // returns an array of String_t return lines; } /// splits a string into an array of strings based on a delimiter -String* str_split(String* self, int* res_len, char c) { +String_t* str_split(String_t* self, int* res_len, char c) { char* string = self->data; - String* elements = NULL; + String_t* elements = NULL; // find the number of lines in the file @@ -168,7 +169,7 @@ String* str_split(String* self, int* res_len, char c) { } // allocate memory for an array of pointers to each line - elements = (String*)malloc((*res_len) * sizeof(String)); + elements = (String_t*)malloc((*res_len) * sizeof(String_t)); int i = 0; flag = false; @@ -193,40 +194,27 @@ String* str_split(String* self, int* res_len, char c) { } - // returns an array of String + // returns an array of String_t return elements; } -int str_len(String* s) { +int str_len(String_t* s) { return s->size; } -char* to_chars(String* s) { +char* to_chars(String_t* s) { return s->data; } // int main() { -// String s = str_from_chars("hello\nworld\neeeee\notherline\n\0"); +// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n\0"); + +// str_remove(&s, 10); +// str_insert(&s, 10, 'h'); + +// printf("%s\n", to_chars(&s)); -// int numlines = 0; -// String* lns = str_lines(&s, &numlines); // str_dealloc(&s); -// for (int i = 0; i < numlines; i++) { -// printf("%s\n", lns[i].data); -// str_dealloc(&lns[i]); -// } - -// String s2 = str_from_chars("$this$needs$to$be$split"); - -// int elements = 0; -// String* split = str_split(&s2, &elements, '$'); -// str_dealloc(&s2); - -// printf("%d elements\n", elements); -// for (int i = 0; i < elements; i++) { -// printf("%s\n", split[i].data); -// str_dealloc(&split[i]); -// printf("(dealloced)\n"); -// } +// return 0; // } \ No newline at end of file diff --git a/final/dynstr.h b/final/dynstr.h index 0391223..6f86e5b 100644 --- a/final/dynstr.h +++ b/final/dynstr.h @@ -5,32 +5,32 @@ typedef struct { int size; int capacity; char* data; -} String; +} String_t; -String str_with_capacity(int capacity); +String_t str_with_capacity(int capacity); -String str_from_chars(char* string); +String_t str_from_chars(char* string); -String str_from_slice(char* string, int len); +String_t str_from_slice(char* string, int len); -String str_new(); +String_t str_new(); -String* str_lines(String* self, int* numlines); +String_t* str_lines(String_t* self, int* numlines); -// String* str_split(String* self, int* res_len, char c); +// String_t* str_split(String_t* self, int* res_len, char c); -void str_push(String* s, char c); +void str_push(String_t* s, char c); -char str_pop(String* s); +char str_pop(String_t* s); -void str_insert(String* s, int index, char c); +void str_insert(String_t* s, int index, char c); -void str_remove(String* s, int index); +void str_remove(String_t* s, int index); -int str_dealloc(String* s); +int str_dealloc(String_t* s); -char* to_chars(String* s); +char* to_chars(String_t* s); -int str_len(String* s); +int str_len(String_t* s); #endif diff --git a/final/editor.c b/final/editor.c index f90b417..7a91c46 100644 --- a/final/editor.c +++ b/final/editor.c @@ -9,7 +9,7 @@ typedef struct { int screen_line; int screen_col; bool editmode; - String* buffer; + String_t* buffer; } Editor; Editor new_editor() { @@ -18,7 +18,13 @@ Editor new_editor() { e.screen_line = 0; e.screen_col = 0; e.editmode = false; - e.buffer = NULL; + + String_t lines = str_from_chars(""); + e.buffer = str_lines(&lines, &e.lines); + str_dealloc(&lines); + + addstr(""); + return e; } @@ -29,12 +35,18 @@ Editor editor_from(char* input_string) { e.screen_line = 0; e.screen_col = 0; e.editmode = false; - e.buffer = NULL; + + String_t lines = str_from_chars(input_string); + e.buffer = str_lines(&lines, &e.lines); + str_dealloc(&lines); + + addstr(input_string); + return e; } void move_cursor(Editor* self, int x, int y) { - if (x != 0 + if (y != 0 && self->screen_line + y >= 0 && self->screen_line + y <= self->lines) { @@ -45,6 +57,7 @@ void move_cursor(Editor* self, int x, int y) { } } else if (self->screen_col + x < 0) { if (self->screen_line - 1 >= 0) { + self->screen_line -= 1; self->screen_col = str_len(&self->buffer[self->screen_line]); } } else if (self->screen_col + x > str_len(&self->buffer[self->screen_line])) { @@ -70,13 +83,13 @@ void addchar(Editor* self, char c) { if (self->screen_line == self->lines) { // reallocate self->buffer to be 1 larger self->lines++; - self->buffer = realloc(self->buffer, sizeof(String) * (self->lines + 1)); + self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines + 1)); } str_insert(&self->buffer[self->screen_line], self->screen_col, c); move_cursor(self, 1, 0); } -String* to_string(Editor* self) { +String_t* to_string(Editor* self) { } diff --git a/final/editor.h b/final/editor.h index ec45b4b..5e678dd 100644 --- a/final/editor.h +++ b/final/editor.h @@ -4,7 +4,7 @@ typedef struct { int screen_line; int screen_col; bool editmode; - String* buffer; + String_t* buffer; } Editor; Editor new_editor(); @@ -15,4 +15,4 @@ void move_cursor(Editor* self, int x, int y); void delchar(Editor* self); void addchar(Editor* self, char c); -String* to_string(Editor* self); \ No newline at end of file +String_t* to_string(Editor* self); \ No newline at end of file diff --git a/final/main.c b/final/main.c index 865c133..0111961 100644 --- a/final/main.c +++ b/final/main.c @@ -24,7 +24,7 @@ int open_editor() { move(0, 0); - Editor editor = new_editor(); + Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol"); while (true) { refresh(); @@ -35,10 +35,26 @@ int open_editor() { editor.editmode = false; break; case KEY_BACKSPACE: + delchar(&editor); + move_cursor(&editor, -1, 0); + break; + case KEY_DC: delchar(&editor); break; case KEY_ENTER: - editor.editmode = false; + //TODO: next line + break; + case KEY_UP: + move_cursor(&editor, 0, -1); + break; + case KEY_DOWN: + move_cursor(&editor, 0, 1); + break; + case KEY_LEFT: + move_cursor(&editor, -1, 0); + break; + case KEY_RIGHT: + move_cursor(&editor, 1, 0); break; default: addchar(&editor, c); @@ -47,6 +63,7 @@ int open_editor() { } else { switch (c) { case 'q': + endwin(); return 0; case 'i': editor.editmode = true; diff --git a/final/util.c b/final/util.c index e69de29..fd40910 100644 --- a/final/util.c +++ b/final/util.c @@ -0,0 +1,4 @@ + + + + diff --git a/prototype/src/main.rs b/prototype/src/main.rs index 51ed803..281db9b 100644 --- a/prototype/src/main.rs +++ b/prototype/src/main.rs @@ -7,6 +7,16 @@ use std::{ io::Write, }; + +fn somefunc() { + let c = "hello"; + + println!("{}", c); + + let c2 = String::from("hello world"); +} + + struct EditorData { buffer: Vec>, // outer vec is the line, inner is col; file_line: i32, @@ -92,14 +102,14 @@ impl EditorData { /// Insert a character at the cursor, /// then update the cursor position. fn addchar(&mut self, c: char) { - ncurses::insch(keystroke as u32); + ncurses::insch(c as u32); if let Some(line) = self.buffer.get_mut(self.screen_line as usize) { line.insert(self.screen_col as usize, c); } else { self.buffer.push(Vec::new()); self.buffer.get_mut(self.screen_line as usize).unwrap().push(c); } - data.mv_cursor(0, 1); + self.mv_cursor(0, 1); } }