diff options
-rwxr-xr-x | 7ed | bin | 43656 -> 0 bytes | |||
-rw-r--r-- | 7ed.h | 4 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | editmode.c | 79 | ||||
-rw-r--r-- | startmode.c | 63 |
5 files changed, 106 insertions, 42 deletions
Binary files differ @@ -26,4 +26,6 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start); int editmode(char filename[], long focus); -int NEW_LINE(char filename[]); +int NEW_LINE(char filename[], long new_line_pos_temp); + +int remove_line_contents(char filename[], long focus);
\ No newline at end of file @@ -1,6 +1,6 @@ all: 7ed 7ed: - gcc -Wfatal-errors -Wall -Werror -Wextra -g -O2 7ed.c functions.c startmode.c editmode.c -o 7ed + gcc -Wfatal-errors -Wall -Werror -Wextra -g -O3 7ed.c functions.c startmode.c editmode.c -o 7ed clean: rm -f 7ed @@ -92,41 +92,9 @@ int check_end_newline(char filename[]) { } -int NEW_LINE(char filename[]) { // doin this test again - - long new_line_pos; // The "focus" that the newline will be inserted afterwards - long new_line_pos_temp = 0; // temp - char buf[1024]; - int success; - - do { - fprintf(stdout, "Create a new line after: "); - if (!fgets(buf, 1024, stdin)) { // take input from user - fprintf(stderr, "Too many characters\n"); - break; - } - char *endptr; - - new_line_pos_temp = strtol(buf, &endptr, 10); - errno = 0; - if (errno == ERANGE) { - fprintf(stderr, "Sorry, this number is too small or too large.\n"); - success = 0; - } - else if (endptr == buf) { - // no character was read - success = 0; - } - else if (*endptr && *endptr != '\n') { - success = 0; - } - - else { - success = 1; - } - - } while (!success); +int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again + long new_line_pos; if (new_line_pos_temp < 0) { fprintf(stderr, "The new line can not be under 0!\n"); return 1; @@ -140,7 +108,7 @@ int NEW_LINE(char filename[]) { // doin this test again if (clif == 1) { return 1; } - if (linecount == (size_t)new_line_pos) { + if (linecount == (size_t)new_line_pos) { // if the file ends without a newline and the user wants to put a newline at the end, then this code within the if-statement will do that. FILE *file; file = fopen(filename, "a"); fputc('\n', file); @@ -174,6 +142,42 @@ int NEW_LINE(char filename[]) { // doin this test again } +int remove_line_contents(char filename[], long focus) { + + char *line; + size_t start; + int ret = GET_LINE(filename, focus, &line, &start); + if (ret == 1) { + return 1; + } + + int cen = check_end_newline(filename); // check if file ends without newline + // check_end_newline returns 0 if ther is a newline + // it returns -1 if ther is none + char editbuffer[1] = {'\0'}; + write_line(filename, focus, editbuffer, 1); // remove contents in line + + if (cen == -1) { // put newline at end if cen == -1 + NEW_LINE(filename, focus-1); + } + free(line); + return 0; + +} + +int delete_line(char filename[], long focus) { + + char *line; + size_t start; + int ret = GET_LINE(filename, focus, &line, &start); + if (ret == 1) { + return 1; + } + + free(line); + return 0; +} + int editmode(char filename[], long focus) { char *line; @@ -192,7 +196,10 @@ int editmode(char filename[], long focus) { fgets(editbuffer, BUF_SZ_2, stdin); - if (editbuffer[0] == '\n') { continue; } + if (editbuffer[0] == '\n') { + fprintf(stdout, "No changes\n"); + return 0; + } fprintf(stdout, "Do you want to write the changes?\n"); int yesno = CHOICE(); diff --git a/startmode.c b/startmode.c index d9779ba..3fcc046 100644 --- a/startmode.c +++ b/startmode.c @@ -37,7 +37,7 @@ int startmode(char filename[]) { uint64_t focus = 1; while(1) { - + firstwhile: fprintf(stdout, "(%lu): ", focus); char command = getchar(); if (command == '\n') { @@ -58,6 +58,10 @@ int startmode(char filename[]) { fprintf(stderr, "Too many characters\n"); break; } + if (buf[0] == '\n') { + goto firstwhile; // start the first while loop aka startmode + } + char *endptr; Lfocus = strtol(buf, &endptr, 10); @@ -129,10 +133,61 @@ int startmode(char filename[]) { NCAT(filename); break; case 'n': - case 'N': - - NEW_LINE(filename); + case 'N': { + // The "focus" that the newline will be inserted afterwards + uint64_t new_line_pos_temp = 0; // temp + char buf[1024]; + int success; + + do { + fprintf(stdout, "Create a new line after: "); + if (!fgets(buf, 1024, stdin)) { // take input from user + fprintf(stderr, "Too many characters\n"); + break; + } + char *endptr; + + new_line_pos_temp = strtol(buf, &endptr, 10); + errno = 0; + if (errno == ERANGE) { + fprintf(stderr, "Sorry, this number is too small or too large.\n"); + success = 0; + } + else if (endptr == buf) { + // no character was read + success = 0; + } + else if (*endptr && *endptr != '\n') { + success = 0; + } + + else { + success = 1; + } + } while (!success); + NEW_LINE(filename, new_line_pos_temp); + break; } + case 'X': + case 'x': + /* + if (focus == Flines) { + size_t Plines; + int ret = COUNT_LINES_IN_FILE_POSIX(filename, &Plines); + + if (Flines != Plines) { + NEW_LINE(filename); + } + } + */ + int choice = CHOICE(); + if (choice == 0) { + remove_line_contents(filename, focus); + } + break; + case 'D': + case 'd': + fprintf(stdout, "delete\n"); break; default: fprintf(stdout, "?\n"); |