summaryrefslogtreecommitdiff
path: root/buff.c
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2018-04-20 00:40:47 +0100
committerKatolaZ <katolaz@freaknet.org>2018-04-20 00:40:47 +0100
commit8f936bc6c89163e3984ac619612c0ce76a51a74e (patch)
tree8d936adb262b2098a984e183e47ea9cc2a8d5d03 /buff.c
first commit
Diffstat (limited to 'buff.c')
-rw-r--r--buff.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/buff.c b/buff.c
new file mode 100644
index 0000000..91593bb
--- /dev/null
+++ b/buff.c
@@ -0,0 +1,170 @@
+#include "buff.h"
+
+line_t* __search_pos(int addr){
+
+ int i;
+ line_t *cur = b_start;
+ for(i=1; i<addr; i++){
+ cur = cur -> next;
+ }
+ return cur;
+}
+
+int read_lines(FILE *fin){
+
+ char buff[4096];
+ line_t *line;
+ size_t s;
+
+ while(feof(fin) == 0){
+ if (!fgets(buff, 4095, fin)) break;
+ s = strlen(buff) + 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;
+ }
+ else{
+ b_end->next = line;
+ line->prev = b_end;
+ }
+ b_end = line;
+ num += 1;
+ }
+ cur = b_end;
+ pos = num;
+}
+
+void print_cur_line(char lineno){
+
+ if (!b_start){
+ printf("?\n");
+ return;
+ }
+
+ if (lineno)
+ printf("%d\t", pos);
+ printf("%s", cur->c);
+}
+
+void print_lines(char lineno){
+
+ line_t *p;
+ int i=0;
+ p = b_start;
+
+
+ printf(">>> *** addr1: %d addr2: %d ***\n", addr1, addr2);
+ for (i=1; i<addr2; i++) p = p->next;
+ pos = i;
+ cur = p;
+ while(pos < addr1){
+ if (lineno)
+ printf("%d\t", pos);
+ printf("%s", cur->c);
+ cur = cur->next;
+ pos += 1;
+ }
+ if (lineno)
+ printf("%d\t", pos);
+ printf("%s", cur->c);
+ if (pos != num) {
+ pos += 1;
+ cur = cur -> next;
+ }
+}
+
+int move_to_line(int addr, char print){
+
+ if (addr > num)
+ return -1;
+ else if (addr >= 0){
+ pos = addr;
+ cur = __search_pos(pos);
+ if (print)
+ print_cur_line(0);
+ }
+ return pos;
+}
+
+void print_lineno(){
+ printf("%d\n", addr1);
+}
+
+int move_forward(int n){
+
+ if (pos + n > num){
+ printf("?\n");
+ return -1;
+ }
+ while(n-- >0){
+ cur = cur->next;
+ }
+ pos += n;
+ return 0;
+}
+
+int move_backward(int n){
+
+ if (pos - n < 1){
+ printf("?\n");
+ return -1;
+ }
+ while(n-- >0){
+ cur = cur->prev;
+ }
+ pos -= n;
+ return 0;
+}
+
+
+
+/* add lines after pos */
+void append_lines(){
+
+ char buff[4096];
+ int n,s;
+ line_t *first, *l, *last;
+
+ 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;
+ }
+ if (addr1 == 0){
+ last->next = b_start;
+ b_start -> prev = last;
+ b_start = first;
+ move_to_line(n,0);
+ }
+ else{
+ last -> next = cur -> next;
+ cur->next = first;
+ first->prev = cur;
+ move_forward(n);
+ }
+}
+
+void insert_lines(){}
+
+void delete_lines(){}