summaryrefslogtreecommitdiff
path: root/editmode.c
diff options
context:
space:
mode:
authorOskar <[email protected]>2023-11-07 23:12:31 +0100
committerOskar <[email protected]>2023-11-07 23:12:31 +0100
commite2965f7ea63a5426e018c4e8c7f557fcce4c69c0 (patch)
treec6c351a729b64a8a0112de6f1e3849746f629695 /editmode.c
parentc09957c3026aa7f25d7bc7a0379d961990bc9e34 (diff)
Added edit mode and function to write the changes to file. Write function is not completely done yet because the temp file created by it is not removed when it is done. But other than that the editor actually works now. There are still some quirks that i need to sort out, especially the fact that you cant actually create a new line beyond the amount in the file you are editing
Diffstat (limited to 'editmode.c')
-rw-r--r--editmode.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/editmode.c b/editmode.c
index e69de29..67bd17e 100644
--- a/editmode.c
+++ b/editmode.c
@@ -0,0 +1,109 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include "7ed.h"
+
+int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer_size) {
+
+char *line;
+size_t start;
+int ret = GET_LINE(filename, focus, &line, &start); // get start position of focus
+if (ret == 1) {
+ return EXIT_FAILURE;
+}
+
+FILE *file;
+FILE *temp;
+file = fopen(filename,"r+"); // Open file
+temp = fopen("temp.7ed","r+");
+if (file == NULL || temp == NULL) { // Check if you can open file
+ fprintf(stderr, "Cannot open file.\n");
+ return 1;
+}
+
+fseek(file, start, SEEK_SET);
+size_t count = 0;
+while (1) {
+ char c = fgetc(file);
+ if (c == '\n') {
+ break; // count the length of the specific line to be replaced
+ } else if (c == EOF) {
+ break;
+ } else {
+ count++;
+ }
+}
+
+fseek(file, 0, SEEK_SET); // go to start of file
+size_t counter = 0;
+char ch;
+for (;; counter++) { // for loop that puts contents of file in to temp
+ // when counter is equal to start, the buffer with the edited content is written to temp
+ if (start == counter) { // after that it uses fseek to point temp right after the written content
+ fprintf(temp, "%s", editbuffer); // and fseek on file will use count variable to skip the content that is supposed to be replaced
+ fseek(temp, start+editbuffer_size-1, SEEK_SET); // After that the function keeps writing everything else to temp
+ fseek(file, start+count, SEEK_SET);
+ }
+ ch = fgetc(file);
+ if (ch == EOF) {
+ break;
+ }
+ fputc(ch, temp);
+
+}
+fseek(temp, 0, SEEK_SET); // reset both files to start at 0 so it doesnt mess with fread and fwrite
+fseek(file, 0, SEEK_SET);
+char buffer[BUF_SZ_2];
+size_t bytes_read;
+while ((bytes_read = fread(buffer, 1, BUF_SZ_2, temp)) > 0) { // Write contents of temp to file
+ fwrite(buffer, 1, bytes_read, file);
+}
+
+free(line);
+fclose(file);
+fclose(temp);
+
+return 0;
+
+}
+
+int editmode(char filename[], long focus) {
+
+char *line;
+size_t start;
+int ret = GET_LINE(filename, focus, &line, &start);
+if (ret == 1) {
+ return EXIT_FAILURE;
+}
+
+fprintf(stdout, "%s", line); // print line to be edited
+free(line);
+
+while (1) {
+ char editbuffer[BUF_SZ_2];
+ fprintf(stdout, "(%ld EDIT): ", focus);
+
+ fgets(editbuffer, BUF_SZ_2, stdin);
+
+ if (editbuffer[0] == '\n') { continue; }
+
+ fprintf(stdout, "Do you want to write the changes?\n");
+ int yesno = CHOICE();
+
+ if (yesno == 1) {
+ printf("N\n");
+ return 0;
+ }
+
+ size_t editbuffer_size = strlen(editbuffer);
+ write_line(filename, focus, editbuffer, editbuffer_size);
+
+ return 0;
+
+}
+
+return EXIT_SUCCESS;
+
+} \ No newline at end of file