diff options
author | KatolaZ <[email protected]> | 2018-05-30 06:29:18 +0100 |
---|---|---|
committer | KatolaZ <[email protected]> | 2018-05-30 06:29:18 +0100 |
commit | 652ef404582555d9f7fca855424108a57cdea3ad (patch) | |
tree | b9106730be79381027b50fd48bd3b99c5400a6ac /buff.c | |
parent | 8a4e57da301445ae4ad7bf06a7ce18c3091ac9df (diff) |
Reimplemented (and fixed) insert/append
Diffstat (limited to 'buff.c')
-rw-r--r-- | buff.c | 158 |
1 files changed, 71 insertions, 87 deletions
@@ -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; + +} |