diff options
author | KatolaZ <[email protected]> | 2018-05-30 16:42:16 +0100 |
---|---|---|
committer | KatolaZ <[email protected]> | 2018-05-30 16:42:16 +0100 |
commit | 00d893c8f8e742acc15e6675dec20c946dfb6fd7 (patch) | |
tree | 06325441928ab711afb550339c7aa837dd2ac223 | |
parent | 169cd80426f3a5e1462913f24b47e3f3ac39dbce (diff) |
implemented 'm' and 't'
-rw-r--r-- | TODO.org | 7 | ||||
-rw-r--r-- | buff.c | 71 | ||||
-rw-r--r-- | buff.h | 2 | ||||
-rw-r--r-- | main.c | 15 | ||||
-rw-r--r-- | tests/pippo.c | 294 | ||||
-rw-r--r-- | tests/test_del.ed | 2 | ||||
-rw-r--r-- | tests/test_tm.ed | 15 | ||||
-rw-r--r-- | tests/testlist.txt | 1 |
8 files changed, 356 insertions, 51 deletions
@@ -3,6 +3,7 @@ * IN-PROGRESS FIXME ** TODO Implement check on file modified +** TODO Check for marks in delete_lines ** DONE Reimplement delete_lines ** DONE Re-check insert/append (test_insapp failing with a double-free) ** DONE Re-check delete as well (maybe related to insert/append?!?!?) @@ -10,15 +11,15 @@ ** DONE Change read_lines to use __get_lines (let fin point to the file to read from...) -* IN-PROGRESS Commands [5/23] +* IN-PROGRESS Commands [7/23] ** DONE e ** TODO e ! ** TODO W ** TODO E ** TODO E ! ** TODO j -** IN-PROGRESS t -** TODO m +** DONE t +** DONE m ** TODO r ** TODO # ** TODO s @@ -95,8 +95,26 @@ void __append_lines(int addr, int n, line_t *first, line_t *last){ } -void __copy_lines(line_t **first, line_t **last){ - +int __copy_lines(line_t **first, line_t **last){ + + line_t *new, *l, *p; + int n = addr2; + + move_to_line(addr2,0); + l = malloc(sizeof(line_t)); + l-> c = malloc((strlen(cur->c) + 1) * sizeof(char)); + strcpy(l->c, cur->c); + p = *first = l; + while(++n <= addr1){ + cur = cur -> next, pos++; + l = malloc(sizeof(line_t)); + l->c = malloc((strlen(cur->c) + 1) * sizeof(char)); + strcpy(l->c, cur->c); + p->next = l; + p = l; + } + *last = p; + return n - addr2; } @@ -178,32 +196,6 @@ void print_lineno(){ printf("%d\n", addr1); } -int move_forward(int n){ - - if (pos + n > num){ - E; - return -1; - } - while(n-- > 0){ - cur = cur->next; - } - pos += n; - return 0; -} - -int move_backward(int n){ - - if (pos - n < 1){ - E; - return -1; - } - while(n-- >0){ - cur = cur->prev; - } - pos -= n; - return 0; -} - /* add lines after pos */ @@ -248,8 +240,7 @@ void delete_lines(){ if (addr2 > 1){ handle = cur -> prev; } - while(pos <= addr1){ - pos ++; + while(pos++ <= addr1){ next = cur -> next; prev = cur -> prev; free(cur -> c); @@ -259,7 +250,6 @@ void delete_lines(){ else break; } - if (!handle){ /* first line deleted */ b_start = cur; @@ -274,7 +264,6 @@ void delete_lines(){ pos = addr2 - 1; cur = prev; } - num -= addr1 - addr2 + 1; } @@ -372,26 +361,24 @@ int mark(char *c){ } -int transfer_lines(int addr, char move){ +void transfer_lines(int addr, char move){ line_t *first, *last; - + int n; + if (addr2 < addr && addr < addr1){ E; - return -1; + return; } first = last = NULL; - - __copy_lines(&first, &last); - + n = __copy_lines(&first, &last); + fprintf(stderr, " >>>> transfer_lines -- addr: %d n: %d\n", addr, n); if (move){ if (addr1 < addr) addr -= (addr1 - addr2 + 1); delete_lines(); } - /* make a generic function to append lines after an addr */ - - return 0; - + __append_lines(addr, n, first, last); + move_to_line(addr+n,0); } @@ -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); +void transfer_lines(int, char); #endif /* __BUFF_H__ */ @@ -175,7 +175,7 @@ int get_fname(char **cmd){ void main_loop(){ char *cmd, *c; - int ret; + int ret, addr, tmp1, tmp2; char p, move; cmd = malloc(256 * sizeof(char)); @@ -295,14 +295,21 @@ void main_loop(){ case 'm': move = 1; case 't': cmd +=1; + if (ret <2) + addr2 = addr1; CHECKADDR; - if (get_addr((const char**)&cmd) == 1) - transfer_lines(addr1, move); + tmp1=addr1, tmp2=addr2; + if(get_addr((const char**)&cmd) == 1){ + addr = addr1; + addr1 = tmp1, addr2 = tmp2; + transfer_lines(addr, move); + } else E; + move = 0; break; case 'w': - cmd +=1; + cmd += 1; get_fname(&cmd); if (!fname) E; diff --git a/tests/pippo.c b/tests/pippo.c new file mode 100644 index 0000000..1718a5b --- /dev/null +++ b/tests/pippo.c @@ -0,0 +1,294 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "buff.h" +#include <ctype.h> + + + +line_t *b_start = NULL; +line_t *b_end = NULL; +line_t *cur = NULL; +int num = 0; +int pos = 0; +/* addr1 is the last address, while addr2 is the previous one */ +int addr1, addr2; +char *fname; + + +const char* skip_blank(const char **c){ + + while (isblank(**c)) (*c)++; + return *c; +} + + +int get_addr(const char **cmd){ + + int addr, n, got; + char sign = 0, comma = 0, semic = 0; + const char *c; + + addr1 = addr2 = -1; + c = *cmd; + while(*c){ + c = skip_blank(&c); + n = 0; + got = 0; + if (isdigit(*c)){ + while (isdigit(*c)){ + got = 1; + n = n * 10 + *c - '0', c++; + } + if (got){ + if (addr1 > -1 ) + addr2 = addr1; + addr1 = n; + } + } + else { + switch(*c){ + case '.': + if (addr1 > -1) + addr2 = addr1; + addr1 = pos; + break; + case '$': + if (addr1 > -1) + addr2 = addr1; + addr1 = num; + break; + case ',': + comma = 1; + if (addr1 ==-1) + addr2 = 1, addr1=num; + else if (addr1 < addr2){ + *cmd = c++; + return -1; + } + break; + case ';': + semic = 1; + if (addr1 == -1) + addr2 = pos, addr1=num; + else if (addr1 < addr2){ + *cmd = c++; + return -1; + } + break; + default: + goto check; + } + c++; + } + } + + check: + *cmd = c; + + fprintf(stderr, " >>> get_addr: addr1: %d addr2: %d\n", addr1, addr2); + + if (addr1 > num || addr2 > num) + return -2; + + if (addr1 == -1){ + if (comma){ + addr1 = num; + addr2 = 1; + return 2; + } + if (semic){ + addr1 = num; + addr2 = pos; + return 2; + } + addr1 = addr2 = pos; + return 0; + } + if (addr2 == -1){ + if (comma || semic){ + addr2 = addr1; + return 2; + } + addr2 = pos; + return 1; + } + return 2; +} + + +int get_fname(char **cmd){ + + static char b[4096]; + int s, n; + + n = sscanf(*cmd, "%s", b); + if (n <1 ) + return 0; + fprintf(stderr, " >>> read fname: %s\n", b); + s = strlen(b); + if (s){ + fname = realloc(fname, (s+1) * sizeof(char)); + memcpy(fname, b, s+1); + return 1; + } + return -1; +} + + +void main_loop(){ + + char *cmd, *c; + int ret; + char p; + + cmd = malloc(256 * sizeof(char)); + c = cmd; + + while(1){ + p = 0; + fgets(cmd, 255, stdin); + + ret = get_addr((const char **) &cmd); + + fprintf(stderr,">>> *** pos: %d ret: %d ***\n", pos, ret); + if (ret) { + fprintf(stderr, ">>> *** addr1: %d, addr2= %d***\n", addr1, addr2); + } + + switch(cmd[0]){ + case 'a': + if (addr1 > -1) + move_to_line(addr1, 0); + append_lines(); + break; + case 'c': + if (addr1 > -1) + move_to_line(addr1,0); + delete_lines(); + addr1 = addr2 = pos; + append_lines(); + case 'd': + if (ret == -1){ + E; + break; + } + if (ret == 2){ + CHECKADDR; + } + if (ret == 1) + addr2 = addr1; + delete_lines(); + break; + case 'e': + cmd++; + skip_blank((const char**)&cmd); + if (cmd[0] == '!') + //read_command(); + ; + else + if(get_fname(&cmd) >=0) + read_file(); + else{ + perror(fname); + E; + } + break; + case 'f': + if (ret > 0) + E; + else{ + cmd += 1; + if (get_fname(&cmd)>=0) + printf("%s\n", fname); + else{ + perror(fname); + E; + } + } + break; + case 'i': + if (addr1 > -1) + move_to_line(addr1, 0); + insert_lines(); + break; + case 'n': p = 1; + case 'p': + if (ret == 2){ + CHECKADDR; + print_lines(p); + } + else if (ret == 1) + move_to_line(addr2, 1); + else if (ret == 0) + print_cur_line(p); + else if (ret == -1){ + addr1 = num; + addr2 = 1; + move_to_line(addr2,0); + print_lines(p); + } + else + E; + break; + case '\n': + if (addr1 > -1 && addr1 <=pos) + move_to_line(addr1, 1); + else if (addr2 <=pos) + print_cur_line(p); + else + E; + break; + case 'q': + goto cleanup; + break; + case 'w': + cmd +=1; + get_fname(&cmd); + if (!fname) + E; + else{ + if (write_lines()){ + perror(fname); + E; + } + } + break; + case '=': + if (addr1 == -1) + addr1 = num; + print_lineno(); + break; + default: + if (cmd[0]) + fprintf(stderr, ">>> **** unknown command: '%c'****\n", cmd[0]); + E; + } + } + cleanup: + free(c); + return; +} + + + +int main(int argc, char *argv[]){ + + FILE *fin; + + fname = NULL; + if (argc > 1){ + fname = malloc((strlen(argv[1])+1) * sizeof(char)); + memcpy(fname, argv[1], strlen(argv[1]) + 1); + if (!read_file()) + fclose(fin); + else{ + perror(fname); + } + } + main_loop(); + addr1 = pos, addr2 = 1; + delete_lines(); + free(fname); +} + diff --git a/tests/test_del.ed b/tests/test_del.ed index 820d673..5add2f4 100644 --- a/tests/test_del.ed +++ b/tests/test_del.ed @@ -17,6 +17,6 @@ $-3,$n 0d 1,5n 1,2,2,4,6d -1,10n +1,$n q q diff --git a/tests/test_tm.ed b/tests/test_tm.ed new file mode 100644 index 0000000..29603c5 --- /dev/null +++ b/tests/test_tm.ed @@ -0,0 +1,15 @@ +10t1 +. +1,15n +2,5t15 +.= +1,25n +22m10 +1,30n +130,135m10 +2,50n +$m0 +,n +5,7m31,2 +q +q diff --git a/tests/testlist.txt b/tests/testlist.txt index d55ef3a..0756905 100644 --- a/tests/testlist.txt +++ b/tests/testlist.txt @@ -3,3 +3,4 @@ test_addr.ed pippo.c test_incr.ed pippo.c test_insapp.ed pippo.c test_del.ed pippo.c +test_tm.ed pippo.c |