summaryrefslogtreecommitdiff
path: root/buff.c
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2018-04-24 17:09:16 +0100
committerKatolaZ <katolaz@freaknet.org>2018-04-24 17:09:16 +0100
commitd46fc59c8713fdc2805ed613132f5cfabbcca1c2 (patch)
tree25695916cf7e75f0c21e92e79b6e2b7c1d5d4d24 /buff.c
parent2bf7cce2ac5df3a6793c3124da9d7d5639769e04 (diff)
Removed read_lines -- several fixes
Diffstat (limited to 'buff.c')
-rw-r--r--buff.c132
1 files changed, 73 insertions, 59 deletions
diff --git a/buff.c b/buff.c
index 6462230..93c779b 100644
--- a/buff.c
+++ b/buff.c
@@ -10,41 +10,66 @@ line_t* __search_pos(int addr){
return cur;
}
-int read_lines(FILE *fin){
-
- char buff[4096];
- line_t *line;
- size_t s, tot;
+int __get_lines(FILE *fin, line_t **first, line_t **last, int *tot){
- tot = 0;
+ char buff[4096];
+ int n,s, num_alloc=0;
+ line_t *l;
+
+ n = 0;
+ *tot = 0;
+ *first = *last = NULL;
while(feof(fin) == 0){
if (!fgets(buff, 4095, fin)) break;
+ if (buff[0] == '.')
+ break;
s = strlen(buff) + 1;
- tot += s-1;
- line = malloc(sizeof(line_t));
- line->s = s;
- line->c = malloc(s * sizeof(char));
- memcpy(line->c, buff, s);
- line->next = NULL;
-
- if (b_start == NULL){
- line->prev = NULL;
- b_start = line;
+ *tot += s -1;
+ l = malloc(sizeof(line_t));
+ l->c = malloc(s * sizeof(char));
+ num_alloc +=1;
+ memcpy(l->c, buff, s);
+ l->s = s;
+ if (*first == NULL){
+ *first = *last = l;
+ (*first) -> next = (*last) -> next = NULL;
+ (*first) -> prev = (*last) -> prev = NULL;
}
+
else{
- b_end->next = line;
- line->prev = b_end;
+ (*last) -> next = l;
+ l->prev = *last;
+ l->next = NULL;
+ *last = l;
}
- b_end = line;
- num += 1;
+ n += 1;
}
- cur = b_end;
- pos = num;
- printf("%d\n", tot);
+ fprintf(stderr, " >>>> get_lines: num_alloc: %d\n", num_alloc);
+ return n;
}
+int read_file(){
+ FILE *fin;
+ int tot;
+ fprintf(stderr, " >>> reading file: %s\n", fname);
+
+ if (!(fin = fopen(fname, "r"))){
+ return -1;
+ }
+
+ if (b_start){
+ addr1=num, addr2=1;
+ delete_lines();
+ }
+ num = pos = __get_lines(fin, &b_start, &b_end, &tot);
+ fprintf (stderr, " >>>> read_file: pos: %d num: %d\n", pos, num);
+ cur = b_end;
+ fclose(fin);
+ printf("%d\n", tot);
+ return 0;
+}
void print_cur_line(char lineno){
@@ -125,49 +150,22 @@ int move_backward(int n){
return 0;
}
-int __get_lines(line_t **first, line_t **last){
-
- char buff[4096];
- int n,s;
- line_t *l;
-
- n = 0;
- *first = *last = NULL;
- while(feof(stdin) == 0){
- if (!fgets(buff, 4095, stdin)) break;
- if (buff[0] == '.')
- break;
- s = strlen(buff) + 1;
- l = malloc(sizeof(line_t));
- l->c = malloc(s * sizeof(char));
- memcpy(l->c, buff, s);
- l->s = s;
- if (*first == NULL)
- *first = *last = l;
- else{
- (*last) -> next = l;
- l->prev = *last;
- *last = l;
- }
- n += 1;
- }
- return n;
-}
/* add lines after pos */
void append_lines(){
- int n;
+ int n, tot;
line_t *first, *last;
first = last = NULL;
- n = __get_lines(&first, &last);
+ n = __get_lines(stdin, &first, &last, &tot);
if (addr1 == 0){
fprintf(stderr, " >>> append (zero): pos: %d n:%d\n", pos, n);
last->next = b_start;
- b_start -> prev = last;
+ if (b_start)
+ b_start -> prev = last;
b_start = first;
num += n;
move_to_line(n,0);
@@ -186,11 +184,11 @@ void append_lines(){
void insert_lines(){
- int n;
+ int n, tot;
line_t *first, *last;
first = last = NULL;
- n = __get_lines(&first, &last);
+ n = __get_lines(stdin, &first, &last, &tot);
if (addr1 == 0){
fprintf(stderr, " >>> insert (zero): pos: %d n:%d\n", pos, n);
@@ -216,6 +214,7 @@ void insert_lines(){
void delete_lines(){
line_t *handle;
+ int num_free = 0;
if (addr1 == -1){
addr1 = pos;
@@ -224,18 +223,33 @@ void delete_lines(){
addr2 = addr1;
}
- fprintf(stderr, " >>> delete: addr1: %d addr2: %d\n", addr1, addr2);
+ if (addr1 < addr2){
+ return;
+ }
move_to_line(addr2,0);
+ fprintf(stderr, " >>> delete: addr1: %d addr2: %d pos: %d\n", addr1, addr2, pos);
if (addr2 <= 1){ /* we are deleting the first line */
while(pos <= addr1){
free(cur->c);
- cur = cur -> next;
- if (cur)
+ num_free += 1;
+ if (cur -> next){
+ cur = cur -> next;
free(cur -> prev);
+ }
+ else {
+ free(cur);
+ cur = NULL;
+ pos ++;
+ break;
+ }
pos ++;
}
+
+ fprintf(stderr, " >>>> delete_lines: num_free: %d\n", num_free);
+
+ /* free(b_start); */
b_start = cur;
if (cur){
pos = 1;