summaryrefslogtreecommitdiff
path: root/buff.c
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2018-04-20 18:23:24 +0100
committerKatolaZ <katolaz@freaknet.org>2018-04-20 18:23:24 +0100
commitb10f9522863c937c08d41e2d6da48174cdb3906a (patch)
tree96afc00c5ac6918767302b67829584fcf84bde2c /buff.c
parent99c9a9e20f80f5d04dfddaf19e18d1179c65c91e (diff)
Implemented 'c' command
Diffstat (limited to 'buff.c')
-rw-r--r--buff.c147
1 files changed, 128 insertions, 19 deletions
diff --git a/buff.c b/buff.c
index f0ef72d..96cb12f 100644
--- a/buff.c
+++ b/buff.c
@@ -14,11 +14,13 @@ int read_lines(FILE *fin){
char buff[4096];
line_t *line;
- size_t s;
+ size_t s, tot;
+ tot = 0;
while(feof(fin) == 0){
if (!fgets(buff, 4095, fin)) break;
s = strlen(buff) + 1;
+ tot += s-1;
line = malloc(sizeof(line_t));
line->s = s;
line->c = malloc(s * sizeof(char));
@@ -38,12 +40,17 @@ int read_lines(FILE *fin){
}
cur = b_end;
pos = num;
+ printf("%d\n", tot);
}
+
+
+
+
void print_cur_line(char lineno){
if (!b_start){
- printf("?\n");
+ E;
return;
}
@@ -95,10 +102,10 @@ void print_lineno(){
int move_forward(int n){
if (pos + n > num){
- printf("?\n");
+ E;
return -1;
}
- while(n-- >0){
+ while(n-- > 0){
cur = cur->next;
}
pos += n;
@@ -108,7 +115,7 @@ int move_forward(int n){
int move_backward(int n){
if (pos - n < 1){
- printf("?\n");
+ E;
return -1;
}
while(n-- >0){
@@ -118,17 +125,14 @@ int move_backward(int n){
return 0;
}
-
-
-/* add lines after pos */
-void append_lines(){
+int __get_lines(line_t **first, line_t **last){
char buff[4096];
int n,s;
- line_t *first, *l, *last;
+ line_t *l;
n = 0;
- first = last = NULL;
+ *first = *last = NULL;
while(feof(stdin) == 0){
if (!fgets(buff, 4095, stdin)) break;
if (buff[0] == '.')
@@ -138,29 +142,134 @@ void append_lines(){
l->c = malloc(s * sizeof(char));
memcpy(l->c, buff, s);
l->s = s;
- if (first == NULL)
- first = last = l;
+ if (*first == NULL)
+ *first = *last = l;
else{
- last -> next = l;
- l->prev = last;
- last = l;
+ (*last) -> next = l;
+ l->prev = *last;
+ *last = l;
}
n += 1;
}
+ return n;
+}
+
+
+/* add lines after pos */
+void append_lines(){
+
+ int n;
+ line_t *first, *last;
+
+ first = last = NULL;
+ n = __get_lines(&first, &last);
+
if (addr1 == 0){
+ fprintf(stderr, " >>> append (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, " >>> append (non-zero): pos: %d n:%d\n", pos, n);
last -> next = cur -> next;
cur->next = first;
first->prev = cur;
- move_forward(n);
+ num += n;
+ //move_forward(n);
+ pos += n;
+ cur = last;
+ }
+}
+
+void insert_lines(){
+
+ int n;
+ line_t *first, *last;
+
+ first = last = NULL;
+ n = __get_lines(&first, &last);
+
+ 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 -=1 ;
+ cur = last;
}
}
-void insert_lines(){}
-void delete_lines(){}
+void delete_lines(){
+
+ line_t *handle;
+
+ if (addr1 == -1){
+ addr1 = pos;
+ }
+ if (addr2 == -1){
+ addr2 = addr1;
+ }
+
+ fprintf(stderr, " >>> delete: addr1: %d addr2: %d\n", addr1, addr2);
+
+ move_to_line(addr2,0);
+
+ if (addr2 <= 1){ /* we are deleting the first line */
+ while(pos <= addr1){
+ free(cur->c);
+ cur = cur -> next;
+ if (cur)
+ free(cur -> prev);
+ pos ++;
+ }
+ 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;
+}
+
+
+void change_lines(){
+ delete_lines();
+ addr2 = addr1 =pos;
+ append_lines();
+}