diff options
-rw-r--r-- | 7ed.h | 16 | ||||
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | editmode.c | 100 | ||||
-rw-r--r-- | functions.c | 29 | ||||
-rw-r--r-- | startmode.c | 62 | ||||
-rw-r--r-- | test/newlinetest | 2 |
6 files changed, 140 insertions, 71 deletions
@@ -10,22 +10,24 @@ -int COUNT_LINES_IN_FILE (char filename[], uint64_t *lines); +int count_lines_in_file(char filename[], uint64_t *lines); -int COUNT_LINES_IN_FILE_POSIX (char filename[], size_t *lines); +int count_lines_in_file_posix(char filename[], size_t *lines); -void CONFIRM(); +void confirm(); -int CHOICE(); +int choice(); void shuffle(char arr[], int n); int startmode(char filename[]); -int GET_LINE(char filename[], long focus, char **line, size_t *start); +int get_line(char filename[], long focus, char **line, size_t *start); int editmode(char filename[], long focus); -int NEW_LINE(char filename[], long new_line_pos_temp); +int new_line(char filename[], long new_line_pos_temp); -int remove_line_contents(char filename[], long focus);
\ No newline at end of file +int remove_line_contents(char filename[], long focus); + +int delete_specified_newline(char filename[], long focus);
\ No newline at end of file @@ -1,6 +1,8 @@ This is my take on a so called "Line editor" Its a crude and simple line editor. +WARNING: Do not use this program on any files that you dont want to risk damaging, deleting or overwriting. I can not gaurantee that this program will function as expected. + TODO: - Create a function to retrieve a line from file (DONE) It's called: GET_LINE(char filename[], long focus, char **line) - Create edit mode to actually make edits to the file (DONE) @@ -6,11 +6,69 @@ #include "7ed.h" #include <stdint.h> -int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer_size) { +int delete_specified_newline(char filename[], long focus) { // special version of write_line that does as the name says char *line; size_t start; - int ret = GET_LINE(filename, focus, &line, &start); // get start position of focus + int ret = get_line(filename, focus, &line, &start); // get start position of focus + if (ret == 1) { + return EXIT_FAILURE; + } + // TODO: Some code that checks if its the first line in the file. Because such a situation might cause a problem. Im not entirely sure yet. + + start = start-1; // decrement by 1 to place start position at the newline + + FILE *file; + FILE *temp; + file = fopen(filename,"r"); // Open file + temp = fopen("temp.7ed","w+"); // Open temp file + if (file == NULL || temp == NULL) { // Check if you can open file + fprintf(stderr, "Cannot open file. delete_specified_newline\n"); + return 1; + } + + 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 + + fseek(temp, start, SEEK_SET); // After that the function keeps writing everything else to temp + fseek(file, start+1, SEEK_SET); // +1 to skip over the newline and thus having it be removed + } + 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 + fclose(file); + file = fopen(filename, "w"); // open file again to write the rest + if (file == NULL) { // Check if you can open file + fprintf(stderr, "Cannot open file for writing.\n"); + return 1; + } + 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); + remove("temp.7ed"); + return 0; + +} + +int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer_size) { // writes text at specified line + + char *line; + size_t start; + int ret = get_line(filename, focus, &line, &start); // get start position of focus if (ret == 1) { return EXIT_FAILURE; } @@ -74,7 +132,7 @@ int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer } -int check_end_newline(char filename[]) { +int check_end_newline(char filename[]) { // function that checks if a file ends with a newline FILE *file; file = fopen(filename, "r"); @@ -92,7 +150,7 @@ int check_end_newline(char filename[]) { } -int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again +int new_line(char filename[], long new_line_pos_temp) { // creates a new line within a file after a specified line number long new_line_pos; if (new_line_pos_temp < 0) { @@ -104,13 +162,17 @@ int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again int cen = check_end_newline(filename); if (cen == -1) { size_t linecount; - int clif = COUNT_LINES_IN_FILE(filename, &linecount); + int clif = count_lines_in_file(filename, &linecount); if (clif == 1) { return 1; } 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"); + if (file == NULL) { + fprintf(stderr, "Cannot open file. new_line()\n"); + return 1; + } fputc('\n', file); fputc('\n', file); fclose(file); @@ -123,7 +185,7 @@ int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again // line2 is the line right after line size_t start; // start is the position at the start of line // start2 is the position at the start of line2 - int ret = GET_LINE(filename, new_line_pos+1, &line, &start); + int ret = get_line(filename, new_line_pos+1, &line, &start); if (ret == 1) { return 1; } @@ -142,11 +204,11 @@ int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again } -int remove_line_contents(char filename[], long focus) { +int remove_line_contents(char filename[], long focus) { // removes contents of a specified line without removing the newline char *line; size_t start; - int ret = GET_LINE(filename, focus, &line, &start); + int ret = get_line(filename, focus, &line, &start); if (ret == 1) { return 1; } @@ -158,31 +220,18 @@ int remove_line_contents(char filename[], long focus) { 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); + 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) { +int editmode(char filename[], long focus) { // the editing interface char *line; size_t start; - int ret = GET_LINE(filename, focus, &line, &start); + int ret = get_line(filename, focus, &line, &start); if (ret == 1) { return EXIT_FAILURE; } @@ -202,10 +251,9 @@ int editmode(char filename[], long focus) { } fprintf(stdout, "Do you want to write the changes?\n"); - int yesno = CHOICE(); + int yesno = choice(); if (yesno == 1) { - printf("N\n"); return 0; } diff --git a/functions.c b/functions.c index a03195d..b308622 100644 --- a/functions.c +++ b/functions.c @@ -6,21 +6,21 @@ #include <string.h> #include <stdint.h> -void CONFIRM() { +void confirm() { struct termios old,new; - tcgetattr(fileno(stdin),&old); // gets something? - tcgetattr(fileno(stdin),&new); // gets something else? - cfmakeraw(&new); // makes new terminal settings - tcsetattr(fileno(stdin),TCSANOW,&new); // sets those settings immediately (TCSANOW) to &new + tcgetattr(fileno(stdin),&old); + tcgetattr(fileno(stdin),&new); + cfmakeraw(&new); + tcsetattr(fileno(stdin),TCSANOW,&new); fputs("Press any key to continue...",stdout); fflush(NULL); fgetc(stdin); - tcsetattr(fileno(stdin),TCSANOW,&old); // goes back to old settings - puts(""); // newline + tcsetattr(fileno(stdin),TCSANOW,&old); + puts(""); } -int CHOICE() { +int choice() { char choice; do { @@ -47,7 +47,7 @@ int CHOICE() { return EXIT_FAILURE; } -int COUNT_LINES_IN_FILE (char filename[], uint64_t *lines) { +int count_lines_in_file (char filename[], uint64_t *lines) { // Does not follow posix because this function accounts for if the last line does not end with a newline. @@ -83,7 +83,7 @@ int COUNT_LINES_IN_FILE (char filename[], uint64_t *lines) { return 0; } -int COUNT_LINES_IN_FILE_POSIX (char filename[], size_t *lines) { +int count_lines_in_file_posix (char filename[], size_t *lines) { // Same function as before but posix @@ -97,7 +97,7 @@ int COUNT_LINES_IN_FILE_POSIX (char filename[], size_t *lines) { } char buffer[BUF_SZ_4]; // Creates buffer with size of BUF_SZ_4 - while (1) { +while (1) { // hello! size_t bytes_read = fread(buffer, 1, BUF_SZ_4, file); // puts chars from file in to buffer and returns the amount of bytes. for (size_t i = 0 ; i < bytes_read; i++) { if (buffer[i] == '\n') { // Searches through the buffer for newlines. @@ -122,14 +122,14 @@ 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. +int get_line(char filename[], long focus, char **line, size_t *start) { // Making this function was hell. Hardest thing ive coded in a while. FILE *file; file = fopen(filename,"r"); // Open file if (file == NULL) { // Check if you can open file - fprintf(stderr, "Cannot open file GET_LINE.\n"); + fprintf(stderr, "Cannot open file get_line.\n"); return 1; } @@ -162,8 +162,7 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Makin } *start = 0; // Is start the start of where line - - //printf("%s", c1buf); // The purpose of this if statement is that it will only print line 1. Not too elegant of a way to handle this but its the only way i knew how to. + } else { focus--; diff --git a/startmode.c b/startmode.c index 3fcc046..5df9032 100644 --- a/startmode.c +++ b/startmode.c @@ -6,13 +6,13 @@ #include "7ed.h" #include <stdint.h> -int NCAT(char filename[]) { +int ncat(char filename[]) { FILE *file; file = fopen(filename,"r"); if (file == NULL) { - fprintf(stderr, "Cannot open file. NCAT\n"); + fprintf(stderr, "Cannot open file. ncat\n"); return 1; } @@ -28,7 +28,7 @@ int NCAT(char filename[]) { int startmode(char filename[]) { uint64_t Flines; - int returnval = COUNT_LINES_IN_FILE(filename, &Flines); + int returnval = count_lines_in_file(filename, &Flines); if (returnval == 1) { return EXIT_FAILURE; } @@ -38,6 +38,12 @@ int startmode(char filename[]) { while(1) { firstwhile: + + int ret = count_lines_in_file(filename, &Flines); // update Flines every time + if (ret == 1) { + return EXIT_FAILURE; + } + fprintf(stdout, "(%lu): ", focus); char command = getchar(); if (command == '\n') { @@ -98,7 +104,7 @@ int startmode(char filename[]) { char *line; size_t start; - int ret = GET_LINE(filename, focus, &line, &start); + int ret = get_line(filename, focus, &line, &start); if (ret == 1) { return EXIT_FAILURE; } @@ -117,7 +123,7 @@ int startmode(char filename[]) { case 'c': uint64_t CFlines; - int returnval = COUNT_LINES_IN_FILE(filename, &CFlines); + int returnval = count_lines_in_file(filename, &CFlines); if (returnval == 1) { return EXIT_FAILURE; } @@ -130,7 +136,7 @@ int startmode(char filename[]) { break; case 'a': case 'A': - NCAT(filename); + ncat(filename); break; case 'n': case 'N': { @@ -166,29 +172,41 @@ int startmode(char filename[]) { } } while (!success); - NEW_LINE(filename, new_line_pos_temp); + 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) { + + int choic = choice(); + if (choic == 0) { remove_line_contents(filename, focus); } break; case 'D': - case 'd': - fprintf(stdout, "delete\n"); - break; + case 'd': { + int choic = choice(); + if (choic == 1) { + return 1; + } + int increment = 0; + if (focus == 1) { // if we are at line 1 then increment becomes 1 + increment++; + } + int rlc = remove_line_contents(filename, focus); + if (rlc == 1) { + return 1; + } + + int dsn = delete_specified_newline(filename, focus+increment); + if (dsn == 1) { + return 1; + } + if (focus == 1) { + break; + } + focus--; + + break; } default: fprintf(stdout, "?\n"); } diff --git a/test/newlinetest b/test/newlinetest index 0b669b6..f00c965 100644 --- a/test/newlinetest +++ b/test/newlinetest @@ -7,4 +7,4 @@ 7 8 9 -10
\ No newline at end of file +10 |