diff --git a/final/binary b/final/binary index 71919e9..75f6145 100755 Binary files a/final/binary and b/final/binary differ diff --git a/final/editor.c b/final/editor.c index d1d5ea2..c5636c0 100644 --- a/final/editor.c +++ b/final/editor.c @@ -23,6 +23,8 @@ void add_toolbar(Editor* self) { addstr(mode); } +#define min(x, y) ((x) < (y) ? (x) : (y)) + void refresh_buffer(Editor* self) { move(0, 0); @@ -36,24 +38,62 @@ void refresh_buffer(Editor* self) { for (size_t i = self->y_offset; i < self->lines && i < self->y_offset + max_y; i++) { // for (size_t i = 0; i < self->lines; i++) { // adding the line number - char line_no[5]; + char line_no[6]; snprintf(line_no, 5, "%-5d", i + 1); // adding the line of text to the buffer addstr(line_no); addch(' '); - addstr(to_chars(&self->buffer[i])); + + String_t line_segment = str_slice( + &self->buffer[i], + self->x_offset, + min(str_len(&self->buffer[i]), self->x_offset + max_x - 5) + ); + + addstr(to_chars(&line_segment)); + str_dealloc(&line_segment); + addstr("\n"); } add_toolbar(self); - move(self->buffer_line, self->buffer_col + 5); + move(self->buffer_line - self->y_offset, self->buffer_col - self->x_offset + 5); } void move_cursor_on_screen(Editor* editor, int x, int y) { int newx = x + 5; // account for the line number - move(y, newx); + int max_x, max_y; + getmaxyx(stdscr, max_y, max_x); + + bool flag = false; + + while (newx + 3 > max_x + editor->x_offset) { + editor->x_offset += 1; + flag = true; + } + + while (newx - 3 < editor->x_offset) { + editor->x_offset -= 1; + flag = true; + } + + while (y + 3 > max_y + editor->y_offset) { + editor->y_offset += 1; + flag = true; + } + + while (y - 3 < editor->y_offset) { + editor->y_offset -= 1; + flag = true; + } + + if (flag) { + refresh_buffer(editor); + } + + move(y - editor->y_offset, newx - editor->x_offset); } void switch_mode(Editor* self) { @@ -63,12 +103,15 @@ void switch_mode(Editor* self) { void newline(Editor* self) { move(self->buffer_line, 0); - char line_no[5]; + char line_no[6]; snprintf(line_no, 5, "%-5d", self->buffer_line + 1); + + fprintf(stderr, "ln: %d, col: %d", self->buffer_line, self->buffer_col); + // adding the line of text to the buffer addstr(line_no); addch(' '); - move(self->buffer_line, self->buffer_col + 5); + move_cursor_on_screen(self, self->buffer_col, self->buffer_line); } Editor editor_from(String_t input_string) { @@ -120,6 +163,7 @@ void move_cursor(Editor* self, int x, int y) { // moving the cursor left or right in any other case self->buffer_col += x; } + fprintf(stderr, "ln: %d, col: %d xoff: %d yoff: %d\n", self->buffer_line, self->buffer_col, self->x_offset, self->y_offset); move_cursor_on_screen(self, self->buffer_col, self->buffer_line); } diff --git a/final/log.txt b/final/log.txt index 50edf5f..19e8c59 100644 --- a/final/log.txt +++ b/final/log.txt @@ -1 +1,258 @@ -Fatal glibc error: malloc.c:2599 (sysmalloc): assertion failed: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0) +starting editor openiing file somecode.cln: 1, col: 0 xoff: 0 yoff: 0 +ln: 2, col: 0 xoff: 0 yoff: 0 +ln: 3, col: 0 xoff: 0 yoff: 0 +ln: 4, col: 0 xoff: 0 yoff: 0 +ln: 5, col: 0 xoff: 0 yoff: 0 +ln: 6, col: 0 xoff: 0 yoff: 0 +ln: 7, col: 0 xoff: 0 yoff: 0 +ln: 8, col: 0 xoff: 0 yoff: 0 +ln: 9, col: 0 xoff: 0 yoff: 0 +ln: 10, col: 0 xoff: 0 yoff: 0 +ln: 11, col: 0 xoff: 0 yoff: 0 +ln: 12, col: 0 xoff: 0 yoff: 0 +ln: 13, col: 0 xoff: 0 yoff: 0 +ln: 14, col: 0 xoff: 0 yoff: 0 +ln: 15, col: 0 xoff: 0 yoff: 0 +ln: 16, col: 0 xoff: 0 yoff: 0 +ln: 17, col: 0 xoff: 0 yoff: 0 +ln: 18, col: 0 xoff: 0 yoff: 0 +ln: 19, col: 0 xoff: 0 yoff: 0 +ln: 20, col: 0 xoff: 0 yoff: 0 +ln: 21, col: 0 xoff: 0 yoff: 0 +ln: 22, col: 0 xoff: 0 yoff: 0 +ln: 23, col: 0 xoff: 0 yoff: 0 +ln: 24, col: 0 xoff: 0 yoff: 0 +ln: 25, col: 0 xoff: 0 yoff: 0 +ln: 26, col: 0 xoff: 0 yoff: 0 +ln: 27, col: 0 xoff: 0 yoff: 0 +ln: 28, col: 0 xoff: 0 yoff: 0 +ln: 29, col: 0 xoff: 0 yoff: 0 +ln: 30, col: 0 xoff: 0 yoff: 0 +ln: 31, col: 0 xoff: 0 yoff: 0 +ln: 32, col: 0 xoff: 0 yoff: 0 +ln: 33, col: 0 xoff: 0 yoff: 0 +ln: 34, col: 0 xoff: 0 yoff: 0 +ln: 35, col: 0 xoff: 0 yoff: 0 +ln: 36, col: 0 xoff: 0 yoff: 0 +ln: 37, col: 0 xoff: 0 yoff: 1 +ln: 38, col: 0 xoff: 0 yoff: 2 +ln: 39, col: 0 xoff: 0 yoff: 3 +ln: 40, col: 0 xoff: 0 yoff: 4 +ln: 41, col: 0 xoff: 0 yoff: 5 +ln: 42, col: 0 xoff: 0 yoff: 6 +ln: 43, col: 0 xoff: 0 yoff: 7 +ln: 44, col: 0 xoff: 0 yoff: 8 +ln: 45, col: 0 xoff: 0 yoff: 9 +ln: 46, col: 0 xoff: 0 yoff: 10 +ln: 47, col: 0 xoff: 0 yoff: 11 +ln: 48, col: 0 xoff: 0 yoff: 12 +ln: 49, col: 0 xoff: 0 yoff: 13 +ln: 50, col: 0 xoff: 0 yoff: 14 +ln: 51, col: 0 xoff: 0 yoff: 15 +ln: 52, col: 0 xoff: 0 yoff: 16 +ln: 53, col: 0 xoff: 0 yoff: 17 +ln: 54, col: 0 xoff: 0 yoff: 18 +ln: 55, col: 0 xoff: 0 yoff: 19 +ln: 56, col: 0 xoff: 0 yoff: 20 +ln: 57, col: 0 xoff: 0 yoff: 21 +ln: 58, col: 0 xoff: 0 yoff: 22 +ln: 59, col: 0 xoff: 0 yoff: 23 +ln: 60, col: 0 xoff: 0 yoff: 24 +ln: 61, col: 0 xoff: 0 yoff: 25 +ln: 62, col: 0 xoff: 0 yoff: 26 +ln: 63, col: 0 xoff: 0 yoff: 27 +ln: 64, col: 0 xoff: 0 yoff: 28 +ln: 65, col: 0 xoff: 0 yoff: 29 +ln: 66, col: 0 xoff: 0 yoff: 30 +ln: 67, col: 0 xoff: 0 yoff: 31 +ln: 68, col: 0 xoff: 0 yoff: 32 +ln: 69, col: 0 xoff: 0 yoff: 33 +ln: 70, col: 0 xoff: 0 yoff: 34 +ln: 71, col: 0 xoff: 0 yoff: 35 +ln: 72, col: 0 xoff: 0 yoff: 36 +ln: 73, col: 0 xoff: 0 yoff: 37 +ln: 74, col: 0 xoff: 0 yoff: 38 +ln: 75, col: 0 xoff: 0 yoff: 39 +ln: 76, col: 0 xoff: 0 yoff: 40 +ln: 77, col: 0 xoff: 0 yoff: 41 +ln: 78, col: 0 xoff: 0 yoff: 42 +ln: 79, col: 0 xoff: 0 yoff: 43 +ln: 80, col: 0 xoff: 0 yoff: 44 +ln: 81, col: 0 xoff: 0 yoff: 45 +ln: 82, col: 0 xoff: 0 yoff: 46 +ln: 83, col: 0 xoff: 0 yoff: 47 +ln: 84, col: 0 xoff: 0 yoff: 48 +ln: 85, col: 0 xoff: 0 yoff: 49 +ln: 86, col: 0 xoff: 0 yoff: 50 +ln: 87, col: 0 xoff: 0 yoff: 51 +ln: 88, col: 0 xoff: 0 yoff: 52 +ln: 89, col: 0 xoff: 0 yoff: 53 +ln: 90, col: 0 xoff: 0 yoff: 54 +ln: 91, col: 0 xoff: 0 yoff: 55 +ln: 92, col: 0 xoff: 0 yoff: 56 +ln: 93, col: 0 xoff: 0 yoff: 57 +ln: 94, col: 0 xoff: 0 yoff: 58 +ln: 95, col: 0 xoff: 0 yoff: 59 +ln: 96, col: 0 xoff: 0 yoff: 60 +ln: 97, col: 0 xoff: 0 yoff: 61 +ln: 98, col: 0 xoff: 0 yoff: 62 +ln: 99, col: 0 xoff: 0 yoff: 63 +ln: 100, col: 0 xoff: 0 yoff: 64 +ln: 101, col: 0 xoff: 0 yoff: 65 +ln: 102, col: 0 xoff: 0 yoff: 66 +ln: 103, col: 0 xoff: 0 yoff: 67 +ln: 104, col: 0 xoff: 0 yoff: 68 +ln: 105, col: 0 xoff: 0 yoff: 69 +ln: 106, col: 0 xoff: 0 yoff: 70 +ln: 107, col: 0 xoff: 0 yoff: 71 +ln: 108, col: 0 xoff: 0 yoff: 72 +ln: 109, col: 0 xoff: 0 yoff: 73 +ln: 110, col: 0 xoff: 0 yoff: 74 +ln: 111, col: 0 xoff: 0 yoff: 75 +ln: 112, col: 0 xoff: 0 yoff: 76 +ln: 113, col: 0 xoff: 0 yoff: 77 +ln: 114, col: 0 xoff: 0 yoff: 78 +ln: 115, col: 0 xoff: 0 yoff: 79 +ln: 116, col: 0 xoff: 0 yoff: 80 +ln: 117, col: 0 xoff: 0 yoff: 81 +ln: 118, col: 0 xoff: 0 yoff: 82 +ln: 119, col: 0 xoff: 0 yoff: 83 +ln: 120, col: 0 xoff: 0 yoff: 84 +ln: 121, col: 0 xoff: 0 yoff: 85 +ln: 122, col: 0 xoff: 0 yoff: 86 +ln: 123, col: 0 xoff: 0 yoff: 87 +ln: 124, col: 0 xoff: 0 yoff: 88 +ln: 125, col: 0 xoff: 0 yoff: 89 +ln: 126, col: 0 xoff: 0 yoff: 90 +ln: 127, col: 0 xoff: 0 yoff: 91 +ln: 128, col: 0 xoff: 0 yoff: 92 +ln: 129, col: 0 xoff: 0 yoff: 93 +ln: 130, col: 0 xoff: 0 yoff: 94 +ln: 131, col: 0 xoff: 0 yoff: 95 +ln: 132, col: 0 xoff: 0 yoff: 96 +ln: 133, col: 0 xoff: 0 yoff: 97 +ln: 134, col: 0 xoff: 0 yoff: 98 +ln: 135, col: 0 xoff: 0 yoff: 99 +ln: 136, col: 0 xoff: 0 yoff: 100 +ln: 137, col: 0 xoff: 0 yoff: 101 +ln: 138, col: 0 xoff: 0 yoff: 102 +ln: 139, col: 0 xoff: 0 yoff: 103 +ln: 140, col: 0 xoff: 0 yoff: 104 +ln: 141, col: 0 xoff: 0 yoff: 105 +ln: 142, col: 0 xoff: 0 yoff: 106 +ln: 143, col: 0 xoff: 0 yoff: 107 +ln: 144, col: 0 xoff: 0 yoff: 108 +ln: 145, col: 0 xoff: 0 yoff: 109 +ln: 146, col: 0 xoff: 0 yoff: 110 +ln: 147, col: 0 xoff: 0 yoff: 111 +ln: 148, col: 0 xoff: 0 yoff: 112 +ln: 149, col: 0 xoff: 0 yoff: 113 +ln: 150, col: 0 xoff: 0 yoff: 114 +ln: 151, col: 0 xoff: 0 yoff: 115 +ln: 152, col: 0 xoff: 0 yoff: 116 +ln: 153, col: 0 xoff: 0 yoff: 117 +ln: 154, col: 0 xoff: 0 yoff: 118 +ln: 155, col: 0 xoff: 0 yoff: 119 +ln: 156, col: 0 xoff: 0 yoff: 120 +ln: 157, col: 0 xoff: 0 yoff: 121 +ln: 158, col: 0 xoff: 0 yoff: 122 +ln: 159, col: 0 xoff: 0 yoff: 123 +ln: 160, col: 0 xoff: 0 yoff: 124 +ln: 161, col: 0 xoff: 0 yoff: 125 +ln: 162, col: 0 xoff: 0 yoff: 126 +ln: 163, col: 0 xoff: 0 yoff: 127 +ln: 164, col: 0 xoff: 0 yoff: 128 +ln: 165, col: 0 xoff: 0 yoff: 129 +ln: 166, col: 0 xoff: 0 yoff: 130 +ln: 167, col: 0 xoff: 0 yoff: 131 +ln: 168, col: 0 xoff: 0 yoff: 132 +ln: 169, col: 0 xoff: 0 yoff: 133 +ln: 170, col: 0 xoff: 0 yoff: 134 +ln: 171, col: 0 xoff: 0 yoff: 135 +ln: 172, col: 0 xoff: 0 yoff: 136 +ln: 173, col: 0 xoff: 0 yoff: 137 +ln: 174, col: 0 xoff: 0 yoff: 138 +ln: 175, col: 0 xoff: 0 yoff: 139 +ln: 176, col: 0 xoff: 0 yoff: 140 +ln: 177, col: 0 xoff: 0 yoff: 141 +ln: 178, col: 0 xoff: 0 yoff: 142 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +105 +ln: 178, col: 0ln: 178, col: 1 xoff: 0 yoff: 143 +100 +ln: 178, col: 2 xoff: 0 yoff: 143 +97 +ln: 178, col: 3 xoff: 0 yoff: 143 +109 +ln: 178, col: 4 xoff: 0 yoff: 143 +110 +ln: 178, col: 5 xoff: 0 yoff: 143 +32 +ln: 178, col: 6 xoff: 0 yoff: 143 +ln: 178, col: 5 xoff: 0 yoff: 143 +ln: 178, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 3 xoff: 0 yoff: 143 +ln: 178, col: 2 xoff: 0 yoff: 143 +ln: 178, col: 1 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +105 +ln: 178, col: 1 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +111 +ln: 178, col: 0ln: 178, col: 1 xoff: 0 yoff: 143 +ln: 177, col: 1 xoff: 0 yoff: 143 +ln: 176, col: 1 xoff: 0 yoff: 143 +ln: 175, col: 1 xoff: 0 yoff: 143 +410 +ln: 175, col: 2 xoff: 0 yoff: 143 +410 +ln: 175, col: 3 xoff: 0 yoff: 155 +ln: 175, col: 4 xoff: 0 yoff: 155 +ln: 175, col: 3 xoff: 0 yoff: 155 +ln: 175, col: 2 xoff: 0 yoff: 155 +ln: 175, col: 1 xoff: 0 yoff: 155 +ln: 175, col: 2 xoff: 0 yoff: 155 +ln: 174, col: 2 xoff: 0 yoff: 155 +ln: 175, col: 2 xoff: 0 yoff: 155 +ln: 176, col: 2 xoff: 0 yoff: 155 +ln: 176, col: 1 xoff: 0 yoff: 155 +ln: 176, col: 0 xoff: 0 yoff: 155 +ln: 175, col: 22 xoff: 0 yoff: 155 +ln: 176, col: 0 xoff: 0 yoff: 155 +ln: 176, col: 1 xoff: 0 yoff: 155 +ln: 176, col: 0 xoff: 0 yoff: 155 +ln: 175, col: 22 xoff: 0 yoff: 155 diff --git a/final/somefile.c b/final/somefile.c deleted file mode 100644 index 745908c..0000000 --- a/final/somefile.c +++ /dev/null @@ -1,221 +0,0 @@ -/// dynamic array class -/// written by: Harry Irving - -#include -#include -#include -#include -#include - -typedef struct { - uint32_t size, capacity; - char* data; -} String_t; - -String_t str_with_capacity(int capacity) { - String_t s; - - /// allocate memory for 'capacity' chars - s.data = (char*)calloc(capacity, sizeof(char)); - - s.size = 0; - s.capacity = capacity; - - return s; -} - -String_t str_from_chars(const char* string) { - String_t s; - - s.data = (char*)calloc(strlen(string), sizeof(char)); - strcpy(s.data, string); - s.size = strlen(string); - s.capacity = strlen(string); - return 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); - s.size = len; - s.capacity = len; - return s; -} - - -String_t str_new() { - return str_with_capacity(1); -} - - - -int str_dealloc(String_t* self) { - free(self->data); - return 0; -} - -int str_push(String_t* self, char c) { - // check size < capacity - if (self->size < self->capacity) { - self->data[self->size] = c; - self->size++; - return 0; - } - - // reallocate to add capacity for an extra char - int newcap = self->capacity + sizeof(char); - self->data = (char*)realloc(self->data, newcap); - self->data[self->size] = c; - self->size++; - self->capacity = newcap; - return 0; -} - -int str_push_str(String_t* self, char* c) { - if (self->size + strlen(c) >= self->capacity) { - self->capacity = ( self->size + strlen(c) ) * sizeof(char); - self->data = (char*)realloc(self->data, self->capacity); - - for (int i = 0; i < strlen(c); i++) { - self->data[self->size + i] = c[i]; - } - self->size = self->capacity; - } -} - -String_t str_from_file(FILE* file) { - if (file == NULL) { - return str_new(); - } - - char buffer[1024]; - - String_t string = str_new(); - - while (fgets(buffer, sizeof(buffer), file) != NULL) { - str_push_str(&string, buffer); - } - - fclose(file); - - return string; -} - -char str_pop(String_t* self) { - if (self->size == 0) { - return '\0'; - } - self->size--; - char c = self->data[self->size]; - return c; -} - -int str_insert(String_t* self, int index, char c) { - if (index > self->size) { - 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; -} - -int str_remove(String_t* self, int index) { - if (index >= self->size) { - return -1; - } - self->size--; - for (int i = index; i < self->size; i++) { - self->data[i] = self->data[i + 1]; - } - return 0; -} - -/// splits a string into an array of strings based on a delimiter -String_t* str_split(String_t* self, int* res_len, char c) { - - char* string = self->data; - String_t* elements = NULL; - - // find the number of lines in the file - - bool flag = false; - *res_len = 0; - for (int i = 0; i < strlen(string); i++) { - if (string[i] == c) { - if (flag) { - (*res_len)++; - flag = false; - } - } else { - flag = true; - } - } - if (flag) { - (*res_len)++; - } - - // allocate memory for an array of pointers to each line - elements = (String_t*)malloc((*res_len) * sizeof(String_t)); - - int i = 0; - flag = false; - char* start = string; - char* end = string; - - while (*end != '\0') { - if (*end == c) { - if (flag) { - elements[i] = str_from_slice(start, end - start); - i++; - } - end++; - start = end; - flag = false; - } else { - end++; - flag = true; - } - } if (flag) { - elements[i] = str_from_slice(start, end - start); - } - - - // returns an array of String_t - return elements; -} - -String_t* str_lines(String_t* self, int* numlines) { - return str_split(self, numlines, '\n'); -} - -int str_len(String_t* s) { - return s->size; -} - -char* to_chars(String_t* s) { - return s->data; -} - -// int main() { -// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n"); - -// str_remove(&s, 10); -// str_insert(&s, 10, 'h'); - -// printf("%s\n", to_chars(&s)); - -// int linen = 0; -// String_t* lines = str_lines(&s, &linen); -// for (int i=0; i < linen; i++) { -// printf("%s", to_chars(&lines[i])); -// } - -// str_dealloc(&s); - -// return 0; -// }