From 90ab7c4cc4edaedb9180b7ec73570c0cc06fa0ab Mon Sep 17 00:00:00 2001 From: Oskar Date: Sat, 20 Apr 2024 13:11:26 +0200 Subject: new_line seems to work now --- 7ed.h | 2 +- TODO | 4 +--- editmode.c | 23 +++++++++++++++++------ process_multiples.c | 10 ++++------ test/nl.sh | 11 +++++++++++ test/testgrounds/newlinetest | 5 ----- 6 files changed, 34 insertions(+), 21 deletions(-) create mode 100755 test/nl.sh diff --git a/7ed.h b/7ed.h index 28cdcef..e14a9cc 100644 --- a/7ed.h +++ b/7ed.h @@ -35,7 +35,7 @@ int get_line(char *filename, long focus, char **line, size_t *start); int editmode(char filename[], uint64_t focus); -int new_line(char filename[], long long new_line_pos_temp); +int new_line(char filename[], long long new_line_pos_temp, uint64_t amount_of_lines); int remove_line_contents(char filename[], uint64_t focus); diff --git a/TODO b/TODO index 6edd88a..d2cc8d9 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,4 @@ TODO list. This is mostly so i can plan more granularly and remember small stuff i was working on last time and what i should prioritise first. This is not my overall big planning board. (1 highest prio, 10 lowest) -1 - Add some quality of life features -2 - Add a way to specify default Y or N. -2 - Make new_line and remove_line_contents more efficient. Maybe allow specifying amount of lines to add so we dont need to call the function too many times. +1 - Make new_line and remove_line_contents more efficient. Maybe allow specifying amount of lines to add so we dont need to call the function too many times. diff --git a/editmode.c b/editmode.c index 72b1650..13e8c97 100644 --- a/editmode.c +++ b/editmode.c @@ -152,7 +152,7 @@ int check_end_newline(char filename[]) { // function that checks if a file ends } -int new_line(char filename[], long long new_line_pos_temp) { // creates a new line within a file after a specified line number +int new_line(char filename[], long long new_line_pos_temp, uint64_t amount_of_lines) { // creates a new line within a file after a specified line number long long new_line_pos; if (new_line_pos_temp < 0) { @@ -193,13 +193,24 @@ int new_line(char filename[], long long new_line_pos_temp) { // creates a new li } size_t len = strlen(line); - char to_be_written[len+2]; - to_be_written[0] = '\n'; - to_be_written[1] = '\0'; + + // Get length of line + // Get length of amount of newlines + // put them both in a buffer + // Input the buffer into write_line + size_t total_size = amount_of_lines+len+2; + char *to_be_written = malloc(total_size); + uint64_t i = 0; + for ( ; i < amount_of_lines ; i++) { + to_be_written[i] = '\n'; + } + to_be_written[i+1] = '\0'; + strcat(to_be_written, line); - write_line(filename, new_line_pos+1, to_be_written, len+1); + write_line(filename, new_line_pos+1, to_be_written, total_size-1); free(line); + free(to_be_written); } return 0; @@ -222,7 +233,7 @@ int remove_line_contents(char *filename, uint64_t focus) { // removes contents o 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, 1); } free(line); return 0; diff --git a/process_multiples.c b/process_multiples.c index f98bad4..8b5cd31 100644 --- a/process_multiples.c +++ b/process_multiples.c @@ -197,7 +197,7 @@ int call_N_immediate(char *multiple, uint64_t Flines, char *filename) { return -1; } - new_line(filename, newline_insert_position); + new_line(filename, newline_insert_position, 1); return 0; @@ -229,9 +229,7 @@ int call_N_plus_continue(char *multiple, uint64_t focus, char *filename) { } if (choice_yesno == 1) { return -1; } - for(uint64_t count = 0 ; count < newlines_to_add ; count++) { // This is very inefficient if you are adding thousands of lines but why would you wanna do that anyway? - new_line(filename, focus); - } + new_line(filename, focus, newlines_to_add); return 0; @@ -243,14 +241,14 @@ uint64_t call_N(char *multiple, uint64_t focus, uint64_t Flines, char *filename) if (multiple[1] == '\n') { // N will add just 1 line. imm = _NA; - new_line(filename, focus); + new_line(filename, focus, 1); return 0; } if (multiple[1] == '+') { // N+ is the same as N. It will only add 1 line. imm = _NA; if (multiple[2] == '\n') { - new_line(filename, focus); + new_line(filename, focus, 1); return 0; } diff --git a/test/nl.sh b/test/nl.sh new file mode 100755 index 0000000..7080345 --- /dev/null +++ b/test/nl.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +filename="testgrounds/newlinetest" + +rm -f $filename + +for ((i=1; i<=10; i++)) +do + echo "$i" >> "$filename" +done + diff --git a/test/testgrounds/newlinetest b/test/testgrounds/newlinetest index 97b3d1a..f00c965 100644 --- a/test/testgrounds/newlinetest +++ b/test/testgrounds/newlinetest @@ -8,8 +8,3 @@ 8 9 10 -11 -12 -13 -14 -15 -- cgit v1.2.3