From 652ef404582555d9f7fca855424108a57cdea3ad Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Wed, 30 May 2018 06:29:18 +0100 Subject: Reimplemented (and fixed) insert/append --- TODO.org | 21 ++++--- buff.c | 158 +++++++++++++++++++++++---------------------------- buff.h | 2 +- main.c | 42 +++++++++++--- tests/myed_test.sh | 33 +++++++++++ tests/test_addr.ed | 12 ++++ tests/test_incr.ed | 18 ++++++ tests/test_insapp.ed | 33 +++++++++++ tests/testlist.txt | 4 ++ 9 files changed, 219 insertions(+), 104 deletions(-) create mode 100755 tests/myed_test.sh create mode 100644 tests/test_addr.ed create mode 100644 tests/test_incr.ed create mode 100644 tests/test_insapp.ed create mode 100644 tests/testlist.txt diff --git a/TODO.org b/TODO.org index 9d20a5f..6a72e1a 100644 --- a/TODO.org +++ b/TODO.org @@ -2,6 +2,9 @@ * IN-PROGRESS FIXME +** TODO Implement check on file modified +** DONE Re-check insert/append (test_insapp failing with a double-free) +** TODO Re-check delete as well (maybe related to insert/append?!?!?) ** DONE check file name input in 'f' and 'w' ** DONE Change read_lines to use __get_lines (let fin point to the file to read from...) @@ -13,7 +16,7 @@ ** TODO E ** TODO E ! ** TODO j -** TODO t +** IN-PROGRESS t ** TODO m ** TODO r ** TODO # @@ -33,9 +36,11 @@ ** DONE f -* IN-PROGRESS Addresses [11/14] -** TODO Probably rethink (and/or rewrite) addressing -- see below -** TODO Reimplement "," (assign addr2=addr1 in there!!!!) +* IN-PROGRESS Addresses [14/15] +** TODO Test addressing extensively +*** TODO test "-,+n" (not working) +** DONE Probably rethink (and/or rewrite) addressing -- see below +** DONE Reimplement "," (assign addr2=addr1 in there!!!!) ** DONE Reimplement offsets [3/3] *** DONE "," does not work as expected in "1,+10n" *** DONE addresses like "+1,+2" do not work properly @@ -51,7 +56,7 @@ ** DONE $ ** DONE . ** DONE ; -** IN-PROGRESS Offsets (+-) [0/3] -*** TODO Test offsets like 1++++n -*** TODO Test offsets like 1+10+10 -*** TODO test offsets like 10+-++--10 +** DONE Offsets (+-) [3/3] +*** DONE Test offsets like 1++++n +*** DONE Test offsets like 1+10+10 +*** DONE test offsets like 10+-++--10 diff --git a/buff.c b/buff.c index 78bee0a..48c5a80 100644 --- a/buff.c +++ b/buff.c @@ -37,7 +37,7 @@ int __get_lines(FILE *fin, line_t **first, line_t **last, int *tot){ *first = *last = NULL; while(feof(fin) == 0){ if (!fgets(buff, 4095, fin)) break; - if (*buff == '.') + if (*buff == '.' && buff[1] == '\n') break; s = strlen(buff) + 1; *tot += s -1; @@ -64,6 +64,41 @@ int __get_lines(FILE *fin, line_t **first, line_t **last, int *tot){ return n; } +void __append_lines(int addr, int n, line_t *first, line_t *last){ + + line_t *handle = NULL; + + if (addr > 0) + handle = __search_pos(addr); + fprintf(stderr, " >>>> __append_lines -- addr: %d handle: %p\n", addr, handle); + if (!handle){ + /* we are appending at the beginning... */ + last->next = b_start; + b_start -> prev = last; + b_start = first; + } + else{ + /* we are appending somewhere else */ + if (handle -> next) { + last -> next = handle -> next; + last -> next -> prev = last; + } + else + last -> next = NULL; + first->prev = handle; + handle->next = first; + } + cur = last; + pos = addr + n; + num += n; + return; +} + + +void __copy_lines(line_t **first, line_t **last){ + +} + int read_file(){ @@ -84,7 +119,7 @@ int read_file(){ fprintf (stderr, " >>>> read_file: pos: %d num: %d\n", pos, num); cur = b_end; fclose(fin); - printf("%d\n", tot); + fprintf(stderr, "%d\n", tot); return 0; } @@ -179,26 +214,8 @@ void append_lines(){ first = last = NULL; n = __get_lines(stdin, &first, &last, &tot); + __append_lines(addr1, n, first, last); - if (addr1 == 0){ - fprintf(stderr, " >>> append (zero): pos: %d n:%d\n", pos, n); - last->next = b_start; - if (b_start) - b_start -> prev = last; - b_start = first; - num += n; - move_to_line(n,0); - } - else{ - fprintf(stderr, " >>> append (non-zero): pos: %d n:%d\n", pos, n); - last -> next = cur -> next; - cur->next = first; - first->prev = cur; - num += n; - //move_forward(n); - pos += n; - cur = last; - } } void insert_lines(){ @@ -208,25 +225,11 @@ void insert_lines(){ first = last = NULL; n = __get_lines(stdin, &first, &last, &tot); - - if (addr1 == 0){ - fprintf(stderr, " >>> insert (zero): pos: %d n:%d\n", pos, n); - last->next = b_start; - b_start -> prev = last; - b_start = first; - num += n; - move_to_line(n,0); - } - else{ - fprintf(stderr, " >>> insert (non-zero): pos: %d n:%d\n", pos, n); - - cur -> prev -> next = first; - first -> prev = cur -> prev; - last -> next = cur; - num += n; - pos-- ; - cur = last; - } + if (addr1 == 0) + __append_lines(addr1, n, first, last); + else + __append_lines(addr1-1, n, first, last); + } @@ -249,54 +252,11 @@ void delete_lines(){ move_to_line(addr2,0); fprintf(stderr, " >>> delete: addr1: %d addr2: %d pos: %d\n", addr1, addr2, pos); + + + /* FIXME: REIMPLEMENT delete_lines() */ - if (addr2 <= 1){ /* we are deleting the first line */ - while(pos <= addr1){ - pos ++; - free(cur->c); - num_free++; - if (cur -> next){ - cur = cur -> next; - free(cur -> prev); - } - else { - free(cur); - cur = NULL; - break; - } - } - - fprintf(stderr, " >>>> delete_lines: num_free: %d\n", num_free); - - /* free(b_start); */ - b_start = cur; - if (cur){ - pos = 1; - cur = b_start; - } - else - pos = 0; - } - else{ - handle = cur->prev; - while(pos < addr1){ - free(cur->c); - cur = cur -> next; - free(cur->prev); - pos ++; - } - handle->next = cur -> next; - free(cur->c); - free(cur); - if (handle -> next){ - pos = addr2 ; - cur = handle->next; - } - else{ - pos = addr2 - 1; - cur = handle; - } - } + num -= addr1 - addr2 + 1; } @@ -393,3 +353,27 @@ int mark(char *c){ return 0; } + +int transfer_lines(int addr, char move){ + + line_t *first, *last; + + if (addr2 < addr && addr < addr1){ + E; + return -1; + } + + first = last = NULL; + + __copy_lines(&first, &last); + + if (move){ + if (addr1 < addr) + addr -= (addr1 - addr2 + 1); + delete_lines(); + } + /* make a generic function to append lines after an addr */ + + return 0; + +} diff --git a/buff.h b/buff.h index 5d58ef4..020e3b2 100644 --- a/buff.h +++ b/buff.h @@ -40,7 +40,7 @@ int read_file(); int match(char **, char); int mark(char *c); int get_lineno(line_t*); - +int transfer_lines(int, char); #endif /* __BUFF_H__ */ diff --git a/main.c b/main.c index 20e52d0..5dce812 100644 --- a/main.c +++ b/main.c @@ -58,10 +58,18 @@ int get_addr(const char **cmd){ case ',': /* range 1 */ /* refactor to set addr2 = addr1 */ fprintf(stderr, " >>>> comma: addr1: %d addr2: %d pos: %d\n", addr1, addr2, pos); comma = 1; + if (addr1 == -1){ + (*cmd)++; + return -1; + } addr2 = addr1; + addr1 = -1; break; case ';': /* range 2*/ semic = 1; + if (addr1 == -1){ + return -1; + } addr2 = addr1; break; case '?': DIR = BW; @@ -133,8 +141,9 @@ int get_addr(const char **cmd){ if (semic){ addr1 = num; addr2 = pos; - } return 2; + } + return 1; } if (addr1 == -1){ addr1 = addr2 = pos; @@ -167,13 +176,13 @@ void main_loop(){ char *cmd, *c; int ret; - char p; + char p, move; cmd = malloc(256 * sizeof(char)); c = cmd; while(1){ - p = 0; + p = 0; move = 0; fgets(cmd, 255, stdin); ret = get_addr((const char **) &cmd); @@ -187,19 +196,20 @@ void main_loop(){ case 'a': if (addr1 > -1) move_to_line(addr1, 0); + else + addr1 = pos; append_lines(); break; case 'c': if (addr1 > -1) move_to_line(addr1,0); + CHECKADDR; delete_lines(); addr1 = addr2 = pos; append_lines(); case 'd': - if (ret < 0){ - E; - break; - } + if (ret ==-1 ) + addr2 = 1, addr1=num; CHECKADDR; delete_lines(); break; @@ -233,6 +243,9 @@ void main_loop(){ case 'i': if (addr1 > -1) move_to_line(addr1, 0); + else + addr1 = pos; + CHECKADDR; insert_lines(); break; case 'k': @@ -252,6 +265,10 @@ void main_loop(){ move_to_line(addr1, 0); print_cur_line(p); } + else if (ret == -1){ + addr2=1, addr1=num; + print_lines(p); + } else E; break; @@ -272,6 +289,15 @@ void main_loop(){ case 'q': goto cleanup; break; + case 'm': move = 1; + case 't': + cmd +=1; + CHECKADDR; + if (get_addr((const char**)&cmd) == 1) + transfer_lines(addr1, move); + else + E; + break; case 'w': cmd +=1; get_fname(&cmd); @@ -314,7 +340,7 @@ int main(int argc, char *argv[]){ perror(fname); } main_loop(); - addr1 = pos, addr2 = 1; + addr1 = num, addr2 = 1; delete_lines(); free(fname); } diff --git a/tests/myed_test.sh b/tests/myed_test.sh new file mode 100755 index 0000000..4c551a4 --- /dev/null +++ b/tests/myed_test.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +##set -x + +FIN=${1:-"/dev/stdin"} + +while read EDFILE EDSOURCE; do + + + IGN=$(echo ${EDFILE} | grep -c "^#") + if [ $IGN = "0" ]; then + printf "test: $EDFILE..." + + FILE1=$(tempfile) + FILE2=$(tempfile) + + cat ${EDFILE} | /bin/ed ${EDSOURCE} > ${FILE1} 2>/dev/null + cat ${EDFILE} | ../myed ${EDSOURCE} > ${FILE2} 2>/dev/null + + ret=$(diff $FILE1 $FILE2 | tee ${EDFILE}.last_diff | wc -l) + + [ "$ret" = "0" ] && echo " PASSED" && rm ${EDFILE}.last_diff + + [ "$ret" != "0" ] && echo " FAILED (log in ${EDFILE}.last_diff)" + rm $FILE1 $FILE2 + else + echo "${EDFILE}: SKIPPED" + fi + + + + +done <$FIN diff --git a/tests/test_addr.ed b/tests/test_addr.ed new file mode 100644 index 0000000..04a7d83 --- /dev/null +++ b/tests/test_addr.ed @@ -0,0 +1,12 @@ +1 +1p +10n +2,20n +2,20p +,p +,n +,2n +115 +-,+n +-,+1n +q diff --git a/tests/test_incr.ed b/tests/test_incr.ed new file mode 100644 index 0000000..7e7df29 --- /dev/null +++ b/tests/test_incr.ed @@ -0,0 +1,18 @@ +1 ++p +++n +20+ +21+p +30++--n +15+10,15++20n +++--,+++++p +117 ++++ +--- ++++10 ++++---5 +1++++++n +31+10+10 +110+-++--10 +q +q diff --git a/tests/test_insapp.ed b/tests/test_insapp.ed new file mode 100644 index 0000000..fffc3cd --- /dev/null +++ b/tests/test_insapp.ed @@ -0,0 +1,33 @@ +10i +this is a new line +. +1,20n +$i +this should be towards +the +end +. +$-10,$n +0i +head +. +1,3n +25a +another line here +and here +. +20,30n +0a +at +the beginning +. +1,10n +$ +a +...and at +the +end +. +$-10,$n +q +q diff --git a/tests/testlist.txt b/tests/testlist.txt new file mode 100644 index 0000000..df9ca98 --- /dev/null +++ b/tests/testlist.txt @@ -0,0 +1,4 @@ +test_addr.ed pippo.c +#test_pippo.ed oejn +test_incr.ed pippo.c +test_insapp.ed pippo.c -- cgit v1.2.3