diff options
author | Oskar <[email protected]> | 2024-04-19 23:18:03 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-04-19 23:18:03 +0200 |
commit | 73b0414254e6b888656b4e5346292f27b67ac9d9 (patch) | |
tree | 1427052e0515364278638d270755d213768d45c4 | |
parent | 645a02d84b174376beed51de9e8820fe4d217ede (diff) |
Added a way for the program to check if file exists or not. If not then
it creates it. If changes are made to the file like writing newlines
or edits from editmode is written then it will create the file.
-rw-r--r-- | 7ed.h | 3 | ||||
-rw-r--r-- | editmode.c | 3 | ||||
-rw-r--r-- | process_multiples.c | 1 | ||||
-rw-r--r-- | startmode.c | 41 |
4 files changed, 45 insertions, 3 deletions
@@ -12,6 +12,9 @@ #define TRUE_7ED 0 #define FALSE_7ED 1 +#define FILE_EXISTS 0 +#define FILE_NOT_FOUND 1 + int count_lines_in_file(char filename[], uint64_t *lines); int count_lines_in_file_posix(char filename[], size_t *lines); @@ -6,6 +6,8 @@ #include "7ed.h" #include <stdint.h> +extern uint8_t new; + int delete_specified_newline(char filename[], long focus) { // special version of write_line that does as the name says char *line; @@ -277,6 +279,7 @@ int editmode(char filename[], uint64_t focus) { // the editing interface return 0; } + new = 0; size_t editbuffer_size = strlen(editbuffer); write_line(filename, focus, editbuffer, editbuffer_size); diff --git a/process_multiples.c b/process_multiples.c index 8ddb8c8..10a565d 100644 --- a/process_multiples.c +++ b/process_multiples.c @@ -261,7 +261,6 @@ uint64_t call_N(char *multiple, uint64_t focus, uint64_t Flines, char *filename) return 0; } - fprintf(stdout, "%s %ld\n", multiple, Flines); return 0; } diff --git a/startmode.c b/startmode.c index cc19376..7cf532a 100644 --- a/startmode.c +++ b/startmode.c @@ -10,6 +10,7 @@ #include <stdint.h> extern int clfstdin_doubleprint; +uint8_t new = 0; int ncat(char filename[]) { @@ -67,6 +68,7 @@ int call_singles(char single, uint64_t focus, char *filename) { case 'c': case 'C': { + if (new == 1) { fprintf(stdout, "[NEW] "); } int dnl = display_name_linecount(filename); if (dnl == 1) { return EXIT_FAILURE; @@ -76,6 +78,10 @@ int call_singles(char single, uint64_t focus, char *filename) { case 'q': case 'Q': + if (new == 1) { + //fprintf(stdout, "no change, remove\n"); + remove(filename); + } exit(EXIT_SUCCESS); break; @@ -90,10 +96,37 @@ int call_singles(char single, uint64_t focus, char *filename) { return 0; } +uint8_t check_if_file_exists(char *filename) { // If the file exists then do nothing. If it does not exist then make it. + + FILE *file; + file = fopen(filename,"r"); // Open file + if (file == NULL) { + return FILE_NOT_FOUND; + } + + fclose(file); + return FILE_EXISTS; +} + +int fe_stop = 0; + int startmode(char filename[]) { + uint8_t file_existence = check_if_file_exists(filename); + if (fe_stop == 0) { + if (file_existence == FILE_NOT_FOUND) { + //fprintf(stdout, "filenotfound\n"); + new = 1; + fe_stop = 1; + FILE *file = fopen(filename, "w"); + fclose(file); + } + } // The entry to the program. Count lines and display the count. Also show which file is being edited. uint64_t Flines; + + if (new == 1) { fprintf(stdout, "[NEW] "); } + int dnl = display_name_linecount(filename); if (dnl == 1) { return EXIT_FAILURE; @@ -123,11 +156,15 @@ int startmode(char filename[]) { break; case 'n': case 'N': - call_N(multiple, focus, Flines, filename); + if(call_N(multiple, focus, Flines, filename) == 0) { + new = 0; + } break; case 'x': case 'X': - call_X(multiple, focus, Flines, filename); + + call_X(multiple, focus, Flines, filename); + break; case 'd': case 'D': |