diff --git a/final/diff.txt b/final/diff.txt deleted file mode 100644 index 50940c0..0000000 --- a/final/diff.txt +++ /dev/null @@ -1,270 +0,0 @@ -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -ADDED -ADDED -ADDED -ADDED -ADDED -ADDED -ADDED -ADDED -ADDED -ADDED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -REMOVED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED -UNMODIFIED diff --git a/final/editor.c b/final/editor.c index 49fc5e6..1102244 100644 --- a/final/editor.c +++ b/final/editor.c @@ -177,6 +177,7 @@ void newline(Editor* self) { move_cursor_on_screen(self, self->buffer_col, self->buffer_line); } +// instantiates a new editor object given an input string and a filename Editor editor_from(String_t input_string, String_t filename) { Editor e; e.lines = 0; @@ -203,10 +204,12 @@ Editor editor_from(String_t input_string, String_t filename) { return e; } +// creates a new editor object from a filename with an emptry buffer Editor new_editor(String_t filename) { return editor_from(str_new(), filename); } +// prints the changelog for the editor. int print_diff(Editor* self) { char buffer[512]; @@ -216,6 +219,8 @@ int print_diff(Editor* self) { for (size_t i = 0; i < self->diff.size; i++) { LineState state = self->diff.data[i]; + + // for each token in the state, we print the corresponding line if there is a change. switch (state) { // case UNMODIFIED: // original_offset = arr_original_offset(&self->diff, i); @@ -227,7 +232,6 @@ int print_diff(Editor* self) { // ); // str_push_str(&log_string, buffer); // break; - case MODIFIED: original_offset = arr_original_offset(&self->diff, i); modified_offset = arr_modified_offset(&self->diff, i); @@ -279,6 +283,7 @@ int print_diff(Editor* self) { str_dealloc(&log_string); } +// saves the current buffer to a file int save_file(Editor* self) { String_t log_string = str_new(); char log_message[64]; @@ -299,7 +304,8 @@ int save_file(Editor* self) { } } - arr_print(&self->diff); + // debug method + // arr_print(&self->diff); print_diff(self); free(name); @@ -310,12 +316,11 @@ int save_file(Editor* self) { } free(self->original); self->original = str_clone_all(self->buffer, self->lines); + self->original_len = self->lines; snprintf(log_message, 64, "Edited file [%s]\n", to_chars(&self->filename)); str_push_str(&log_string, log_message); - self->original_len = self->lines; - snprintf(log_message, 64, "Old Length: [%d] New Length: [%d]\n", self->original_len, self->lines); str_push_str(&log_string, log_message); @@ -324,7 +329,7 @@ int save_file(Editor* self) { fprintf(stderr, "Error writing log\n"); return -1; } - + printf("wrote log"); str_dealloc(&log_string); @@ -333,6 +338,8 @@ int save_file(Editor* self) { return 0; } +// move the cursor around the screen +// this uses extensive bounds checking to ensure that the cursor moves to the next line correctly void move_cursor(Editor* self, int x, int y) { if (y != 0 && (int)(self->buffer_line) + y >= 0 @@ -420,8 +427,13 @@ void pressed_enter(Editor* self) { } self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity)); - // logs the change of adding a new line - arr_insert(&self->diff, self->buffer_line + 1, ADDED); + if (self->buffer_col == 0) { + arr_insert(&self->diff, self->buffer_line, ADDED); + } else if (self->buffer_col == str_len(&self->buffer[self->buffer_line])) { + arr_insert(&self->diff, self->buffer_line + 1, ADDED); + } else { + arr_insert(&self->diff, self->buffer_line + 1, ADDED); + } if (self->buffer_line == self->lines -1) { // if at end of file add an empty line @@ -490,6 +502,7 @@ void addchar(Editor* self, char c) { move_cursor(self, 1, 0); } +// closes the editor and ensures that all used resources are deallocated void shutdown_editor(Editor* self) { for (size_t i = 0; i < self->capacity; i++) { str_dealloc(&self->buffer[i]); diff --git a/final/log.txt b/final/log.txt index cca5046..7b272cc 100644 --- a/final/log.txt +++ b/final/log.txt @@ -1,27 +1,56 @@ -REMOVED 16 | -REMOVED 17 | -REMOVED 18 | modifie +ADDED 18 | +ADDED 19 | +ADDED 20 | +Edited file [somefile.c] +Old Length: [254] New Length: [254] +REMOVED 18 | REMOVED 19 | REMOVED 20 | -ADDED 35 | } AND modified a line. -ADDED 36 | -ADDED 37 | -ADDED 38 | added some lines -ADDED 39 | -ADDED 40 | -ADDED 41 | +Edited file [somefile.c] +Old Length: [251] New Length: [251] ADDED 42 | -ADDED 43 | -ADDED 44 | -REMOVED 40 | } AND modified a line. -REMOVED 41 | +ADDED 43 | fn main() -> idk { +ADDED 44 | print("") +ADDED 46 | +Edited file [somefile.c] +Old Length: [255] New Length: [255] REMOVED 42 | -REMOVED 43 | added some lines -REMOVED 44 | -REMOVED 45 | -REMOVED 46 | -REMOVED 47 | -REMOVED 48 | -REMOVED 49 | -REMOVED 50 | -REMOVED 51 | +MODIFIED 43 | fn main() -> idk { + -> 42 | +MODIFIED 44 | print("") + -> 43 | +MODIFIED 45 | } + -> 44 | String_t str_from_chars(const char* string) { +Edited file [somefile.c] +Old Length: [252] New Length: [252] +Opened File [somefile.c] +ADDED 28 | +REMOVED 28 | +Edited file [somefile.c] +Old Length: [252] New Length: [252] +ADDED 42 | +REMOVED 42 | +Edited file [somefile.c] +Old Length: [252] New Length: [252] +ADDED 47 | +ADDED 48 | +Edited file [somefile.c] +Old Length: [254] New Length: [254] +MODIFIED 48 | + -> 48 | fn some() -> idk { +ADDED 49 | print(idk) +ADDED 50 | } +Edited file [somefile.c] +Old Length: [256] New Length: [256] +Opened File [somefile.c] +ADDED 28 | +REMOVED 28 | +Edited file [somefile.c] +Old Length: [256] New Length: [256] +ADDED 28 | +ADDED 29 | +ADDED 30 | +ADDED 31 | +Edited file [somefile.c] +Old Length: [260] New Length: [260] +Opened File [somefile.c] diff --git a/final/state.c b/final/state.c index 118d893..db65594 100644 --- a/final/state.c +++ b/final/state.c @@ -21,6 +21,7 @@ typedef struct { } StateArray; StateArray arr_init(int capacity) { + // creates a new array to store the state StateArray arr; arr.size = capacity; arr.capacity = capacity; @@ -32,39 +33,49 @@ StateArray arr_init(int capacity) { return arr; } -void arr_print(StateArray* self) { - FILE* log_file = fopen("diff.txt", "w"); - if (log_file == NULL) { - fprintf(stderr, "Error: Could not open log file\n"); - } +// prints the state of the array into a file +// this method is for debug purposes! +// void arr_print(StateArray* self) { +// FILE* log_file = fopen("diff.txt", "w"); +// if (log_file == NULL) { +// fprintf(stderr, "Error: Could not open log file\n"); +// } - for (int i = 0; i < self->size; i++) { - switch (self->data[i]) { - case UNMODIFIED: - fwrite("UNMODIFIED\n", sizeof(char), strlen("UNMODIFIED\n"), log_file); - break; - case MODIFIED: - fwrite("MODIFIED\n", sizeof(char), strlen("MODIFIED\n"), log_file); - break; - case ADDED: - fwrite("ADDED\n", sizeof(char), strlen("ADDED\n"), log_file); - break; - case REMOVED: - fwrite("REMOVED\n", sizeof(char), strlen("REMOVED\n"), log_file); - break; - } - } +// for (int i = 0; i < self->size; i++) { +// switch (self->data[i]) { +// case UNMODIFIED: +// fwrite("UNMODIFIED\n", sizeof(char), strlen("UNMODIFIED\n"), log_file); +// break; +// case MODIFIED: +// fwrite("MODIFIED\n", sizeof(char), strlen("MODIFIED\n"), log_file); +// break; +// case ADDED: +// fwrite("ADDED\n", sizeof(char), strlen("ADDED\n"), log_file); +// break; +// case REMOVED: +// fwrite("REMOVED\n", sizeof(char), strlen("REMOVED\n"), log_file); +// break; +// } +// } - fclose(log_file); -} +// fclose(log_file); +// } +// calculates an offset to use for indexing into the state relative to the buffer. +// this is because some elements might not be aligned due to needing to insert the "REMOVED" element in some cases. int arr_added_or_removed_before(StateArray* self, int index) { int count = 0; for (int i = 0; i < index; i++) { - if (self->data[i] == ADDED || self->data[i] == REMOVED) { - if (self->data[i] == REMOVED) { - index++; - } + // if (self->data[i] == ADDED || self->data[i] == REMOVED) { + // if (self->data[i] == REMOVED) { + // // every time a "REMOVED" element is found, we increase the index by 1 + // index++; + // } + // count++; + // } + if (self->data[i] == REMOVED) { + // every time a "REMOVED" element is found, we increase the index by 1 + index++; count++; } }