From cfe7a2d4b3dd25ce9577417416e6313572e9e2ea Mon Sep 17 00:00:00 2001 From: Oskar Date: Sat, 13 Apr 2024 18:40:47 +0200 Subject: x implemented and d partially implemented --- 7ed.h | 4 +- Makefile | 2 +- TODO | 2 + editmode.c | 20 ++++++- process_multiples.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++---- process_multiples.h | 4 +- startmode.c | 4 +- 7 files changed, 175 insertions(+), 17 deletions(-) diff --git a/7ed.h b/7ed.h index 90788b5..85ccd9f 100644 --- a/7ed.h +++ b/7ed.h @@ -32,4 +32,6 @@ int new_line(char filename[], long long new_line_pos_temp); int remove_line_contents(char filename[], uint64_t focus); -int delete_specified_newline(char filename[], long focus); \ No newline at end of file +int delete_specified_newline(char filename[], long focus); + +int remove_line_contents_and_newline(char *filename, uint64_t focus); \ No newline at end of file diff --git a/Makefile b/Makefile index e4ac720..1be84f3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS_TESTBIN=-O2 -Wfatal-errors -Wall -Werror -Wextra -g -fsanitize=address +CFLAGS_TESTBIN=-O3 -Wfatal-errors -Wall -Werror -Wextra -g -fsanitize=address CFLAGS=-O2 -flto -march=native -DNDEBUG -fomit-frame-pointer -s -static TARGET=7ed TESTTARGET=7ed-TESTING diff --git a/TODO b/TODO index b947f8e..b3ea199 100644 --- a/TODO +++ b/TODO @@ -2,5 +2,7 @@ TODO list. This is mostly so i can plan more granularly and remember small stuff This is not my overall big planning board. (1 highest prio, 10 lowest) 1 - Start working on X +1 - Start working on D +2 - Re-write new_line and remove_line_contents for better performance. (They are very inefficient) 2 - All L and N functionality is done. Did some testing and made some fixes. I will keep this here for now in case i find anything more. 2 - Not really a TODO point but more of an observation and a realisation i have made. I could have just used regex to do the validation of my commands. How about that... Well im too far in (sunk cost fallacy or whatever its called). diff --git a/editmode.c b/editmode.c index 149a158..759f9d5 100644 --- a/editmode.c +++ b/editmode.c @@ -226,6 +226,25 @@ int remove_line_contents(char *filename, uint64_t focus) { // removes contents o } +int remove_line_contents_and_newline(char *filename, uint64_t focus) { + + int increment = 0; + if (focus == 1) { // checks if its line 1. This is so that we can remove the newline properly. + 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; + } + + return 0; +} + int editmode(char filename[], uint64_t focus) { // the editing interface char *line; @@ -252,7 +271,6 @@ int editmode(char filename[], uint64_t focus) { // the editing interface return 0; } - fprintf(stdout, "Do you want to write the changes?\n"); int yesno = choice(); if (yesno == 1) { diff --git a/process_multiples.c b/process_multiples.c index b99b74d..af164c8 100644 --- a/process_multiples.c +++ b/process_multiples.c @@ -266,41 +266,177 @@ uint64_t call_N(char *multiple, uint64_t focus, uint64_t Flines, char *filename) return 0; } +int call_X_plus_continue(char *multiple, uint64_t focus, char *filename, uint64_t Flines) { // NOT DONE!! IT IS A COPY OF call_N_plus_continue !!! + + char new_multiple[32] = { '\0' }; + + int i = 0; + int j = 2; + for( ; multiple[j] != '\n' ; i++, j++) { + new_multiple[i] = multiple[j]; + } + + char *endptr; + uint64_t amount_of_lines_to_remove = strtol(new_multiple, &endptr, 10); + errno = 0; + if (errno == ERANGE) { + return -1; + } + if (endptr == new_multiple) { + return -1; + } + + if(check_L_linecount(Flines, focus+(amount_of_lines_to_remove-1) /*-1 because we are not doing currentline + amount of lines. That would be 1 too much*/, MODE_L) == _INVALID) { + return -1; + } + + if (choice() == 1) { return -1; } + + for(uint64_t count = 0 ; count < amount_of_lines_to_remove ; count++) { // This is very inefficient if you are adding thousands of lines but why would you wanna do that anyway? + remove_line_contents(filename, focus+count); + } + + return 0; +} + +int call_X_immediate(char *multiple, uint64_t Flines, char *filename) { + + char new_multiple[32] = { '\0' }; + + int i = 0; + int j = 1; + for( ; multiple[j] != '\n' ; i++, j++) { + new_multiple[i] = multiple[j]; + } + + char *endptr; + uint64_t remove_line_contents_position = strtol(new_multiple, &endptr, 10); + errno = 0; + if (errno == ERANGE) { + return 0; + } + if (endptr == new_multiple) { + return 0; + } + + if(check_L_linecount(Flines, remove_line_contents_position, MODE_L) == _INVALID) { + return -1; + } + + if (choice() == 1) { return -1; } + + remove_line_contents(filename, remove_line_contents_position); + + return 0; +} + int call_X(char *multiple, uint64_t focus, uint64_t Flines, char *filename) { int imm = _IMM_NUMBER; if (multiple[1] == '\n') { // X Will remove current line imm = _NA; - remove_line_contents(filename, focus); + + if(choice() == 0) { + remove_line_contents(filename, focus); + } return 0; } - if (multiple[1] == '+') { // X+ Will..... idk + if (multiple[1] == '+') { imm = _NA; if (multiple[2] == '\n') { - // Will have to really think about this + if(choice() == 0) { + remove_line_contents(filename, focus); + } return 0; } - - // X plus continue. Still figuring out how this is gonna work. + call_X_plus_continue(multiple, focus, filename, Flines); return 0; } if (imm == _IMM_NUMBER) { - // X immediate, process immediates - + call_X_immediate(multiple, Flines, filename); return 0; } - fprintf(stdout, "%s %ld\n", multiple, Flines); return 0; } -int call_D(char *multiple) { +uint64_t call_D_plus_continue(char *multiple, uint64_t focus, uint64_t Flines, char *filename) { + + char new_multiple[32] = { '\0' }; + + int i = 0; + int j = 2; + for( ; multiple[j] != '\n' ; i++, j++) { + new_multiple[i] = multiple[j]; + } + + char *endptr; + uint64_t lines_and_newlines_to_remove = strtol(new_multiple, &endptr, 10); + errno = 0; + if (errno == ERANGE) { + return -1; + } + if (endptr == new_multiple) { + return -1; + } + + if(check_L_linecount(Flines, focus+(lines_and_newlines_to_remove-1) /*-1 because we are not doing currentline + amount of lines. That would be 1 too much*/, MODE_L) == _INVALID) { + return -1; + } + + if (choice() == 1) { return -1; } + + for(uint64_t count = 0 ; count < lines_and_newlines_to_remove ; count++) { // This is very inefficient if you are adding thousands of lines but why would you wanna do that anyway? + remove_line_contents_and_newline(filename, focus); + } - fprintf(stdout, "%s\n", multiple); + return 0; + +} + +uint64_t call_D(char *multiple, uint64_t focus, uint64_t Flines, char *filename) { + + int imm = _IMM_NUMBER; + + if (multiple[1] == '\n') { // D Will remove current line + imm = _NA; + + if(choice() == 0) { + remove_line_contents_and_newline(filename, focus); + if (focus == 1) { return 1; } + return focus-1; + } else { + return focus; + } + + } + + if (multiple[1] == '+') { + imm = _NA; + if (multiple[2] == '\n') { + if(choice() == 0) { + remove_line_contents_and_newline(filename, focus); + if (focus == 1) { return 1; } + return focus-1; + } else { + return focus; + } + } + // call_D_plus_continue + + return 0; + } + + if (imm == _IMM_NUMBER) { + //call_D_immediate(multiple, Flines, filename); + return 0; + } + + fprintf(stdout, "%s %ld\n", multiple, Flines); return 0; } diff --git a/process_multiples.h b/process_multiples.h index 6dc4067..137c782 100644 --- a/process_multiples.h +++ b/process_multiples.h @@ -2,5 +2,5 @@ uint64_t call_L(char *multiple, uint64_t focus, uint64_t Flines); uint64_t call_N(char *multiple, uint64_t focus, uint64_t Flines, char *filename); -uint64_t call_X(char *multiple); -uint64_t call_D(char *multiple); \ No newline at end of file +int call_X(char *multiple, uint64_t focus, uint64_t Flines, char *filename); +uint64_t call_D(char *multiple, uint64_t focus, uint64_t Flines, char *filename); \ No newline at end of file diff --git a/startmode.c b/startmode.c index 8dd918d..e7175a2 100644 --- a/startmode.c +++ b/startmode.c @@ -127,11 +127,11 @@ int startmode(char filename[]) { break; case 'x': case 'X': - call_X(multiple); + call_X(multiple, focus, Flines, filename); break; case 'd': case 'D': - call_D(multiple); + focus = call_D(multiple, focus, Flines, filename); break; } -- cgit v1.2.3