From e2965f7ea63a5426e018c4e8c7f557fcce4c69c0 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 7 Nov 2023 23:12:31 +0100 Subject: 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 --- 7ed.h | 7 ++++ Makefile | 7 ++-- editmode.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ functions.c | 20 ++++++----- startmode.c | 41 ++++++++++++++++++----- 5 files changed, 165 insertions(+), 19 deletions(-) diff --git a/7ed.h b/7ed.h index c7236f0..733be83 100644 --- a/7ed.h +++ b/7ed.h @@ -55,3 +55,10 @@ int startmode(char filename[]); int GET_LINE(char filename[], long focus, char **line, size_t *start); #endif /* GET_LINE_H */ + +#ifndef EDITMODE_H +#define EDITMODE_H + +int editmode(char filename[], long focus); + +#endif /* EDITMODE_H */ \ No newline at end of file diff --git a/Makefile b/Makefile index 5e2326b..6746525 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ all: 7ed 7ed: - gcc -Wfatal-errors -Wall -Werror -Wextra -g 7ed.c functions.c startmode.c -o 7ed + gcc -Wfatal-errors -Wall -Werror -Wextra -g 7ed.c functions.c startmode.c editmode.c -o 7ed + cp 7ed test + mv test/7ed test/7ed-test clean: - rm -f 7ed \ No newline at end of file + rm -f 7ed + rm -f test/7ed-test diff --git a/editmode.c b/editmode.c index e69de29..67bd17e 100644 --- a/editmode.c +++ b/editmode.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#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 diff --git a/functions.c b/functions.c index 34e0dd7..ac3f789 100644 --- a/functions.c +++ b/functions.c @@ -123,15 +123,6 @@ void shuffle(char arr[], int n) { int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Making this function was hell. Hardest thing ive coded in a while. - size_t lines; - int ret = COUNT_LINES_IN_FILE(filename, &lines); - if (ret == 1) { - return EXIT_FAILURE; - } - - if ((long)lines < focus) { // check if focus is bigger than the amount of - return EXIT_FAILURE; // lines in the actual file and returns exit failure - } FILE *file; file = fopen(filename,"r"); // Open file @@ -141,6 +132,17 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Makin return 1; } + /* + size_t lines; + int ret = COUNT_LINES_IN_FILE(filename, &lines); + if (ret == 1) { + return EXIT_FAILURE; + } + + if ((long)lines < focus) { // check if focus is bigger than the amount of + return EXIT_FAILURE; // lines in the actual file and returns exit failure + } + */ if (focus == 1) { int c1_count = 0; while (1) { diff --git a/startmode.c b/startmode.c index 9744b0b..a6f1395 100644 --- a/startmode.c +++ b/startmode.c @@ -5,6 +5,25 @@ #include #include "7ed.h" +int NCAT(char filename[]) { + + FILE *file; + file = fopen(filename,"r"); + + if (file == NULL) { + fprintf(stderr, "Cannot open file.\n"); + return 1; + } + + char buffer[BUF_SZ_2]; + size_t bytes_read; + while ((bytes_read = fread(buffer, 1, BUF_SZ_2, file)) > 0) { + fwrite(buffer, 1, bytes_read, stdout); + } + fclose(file); + return 0; +} + int startmode(char filename[]) { size_t Flines; @@ -43,7 +62,7 @@ int startmode(char filename[]) { Lfocus = strtol(buf, &endptr, 10); errno = 0; if (errno == ERANGE) { - printf("Sorry, this number is too small or too large.\n"); + fprintf(stderr, "Sorry, this number is too small or too large.\n"); success = 0; } else if (endptr == buf) { @@ -63,7 +82,7 @@ int startmode(char filename[]) { } while (!success); if (Lfocus < 1 || Lfocus > (long)Flines) { - fprintf(stdout, "L is either less than 1 or more than %ld\n", Flines); + fprintf(stderr, "L is either less than 1 or more than %ld\n", Flines); } else { focus = Lfocus; } @@ -78,32 +97,38 @@ int startmode(char filename[]) { if (ret == 1) { return EXIT_FAILURE; } - printf("%s", line); + fprintf(stdout, "%s", line); //printf("%ld", start); free(line); break; case 'E': case 'e': - printf("EDIT MODE\n"); + + editmode(filename, focus); + break; case 'C': case 'c': - size_t Flines; - int returnval = COUNT_LINES_IN_FILE(filename, &Flines); + size_t CFlines; + int returnval = COUNT_LINES_IN_FILE(filename, &CFlines); if (returnval == 1) { return EXIT_FAILURE; } - fprintf(stdout,"%s %ld lines\n", filename, Flines); + fprintf(stdout,"%s %ld lines\n", filename, CFlines); break; case 'Q': case 'q': return EXIT_SUCCESS; break; + case 'a': + case 'A': + NCAT(filename); + break; default: - printf("?\n"); + fprintf(stdout, "?\n"); } -- cgit v1.2.3