summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <[email protected]>2018-04-24 17:09:16 +0100
committerKatolaZ <[email protected]>2018-04-24 17:09:16 +0100
commitd46fc59c8713fdc2805ed613132f5cfabbcca1c2 (patch)
tree25695916cf7e75f0c21e92e79b6e2b7c1d5d4d24
parent2bf7cce2ac5df3a6793c3124da9d7d5639769e04 (diff)
Removed read_lines -- several fixes
-rw-r--r--Makefile4
-rw-r--r--buff.c132
-rw-r--r--buff.h5
-rw-r--r--main.c49
4 files changed, 116 insertions, 74 deletions
diff --git a/Makefile b/Makefile
index 435b4a3..f89c858 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
CC=gcc
-CFLAGS=-Os
+CFLAGS=-g
all: myed
myed: main.c buff.c buff.h
$(CC) $(CFLAGS) -o myed main.c buff.c
- strip myed
+## strip myed
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;
diff --git a/buff.h b/buff.h
index f97ed78..7745518 100644
--- a/buff.h
+++ b/buff.h
@@ -20,8 +20,8 @@ typedef struct line_t {
extern line_t *b_start;
extern line_t *b_end;
extern line_t *cur;
-extern unsigned int num;
-extern unsigned int pos;
+extern int num;
+extern int pos;
extern int addr1, addr2;
extern char *fname;
@@ -35,6 +35,7 @@ void append_lines();
void insert_lines();
void delete_lines();
int write_lines();
+int read_file();
#endif /* __BUFF_H__ */
diff --git a/main.c b/main.c
index 74bd93c..2d2a1eb 100644
--- a/main.c
+++ b/main.c
@@ -9,8 +9,8 @@
line_t *b_start = NULL;
line_t *b_end = NULL;
line_t *cur = NULL;
-unsigned int num = 0;
-unsigned int pos = 0;
+int num = 0;
+int pos = 0;
/* addr1 is the last address, while addr2 is the previous one */
int addr1, addr2;
char *fname;
@@ -87,6 +87,9 @@ int get_addr(const char **cmd){
*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){
@@ -135,11 +138,12 @@ int get_fname(char **cmd){
void main_loop(){
- char *cmd;
+ char *cmd, *c;
int ret;
char p;
cmd = malloc(256 * sizeof(char));
+ c = cmd;
while(1){
p = 0;
@@ -147,7 +151,7 @@ void main_loop(){
ret = get_addr((const char **) &cmd);
- fprintf(stderr,">>> *** pos: %d address: %d ***\n", pos, ret);
+ fprintf(stderr,">>> *** pos: %d ret: %d ***\n", pos, ret);
if (ret) {
fprintf(stderr, ">>> *** addr1: %d, addr2= %d***\n", addr1, addr2);
}
@@ -176,6 +180,20 @@ void main_loop(){
addr2 = addr1;
delete_lines();
break;
+ case 'e':
+ cmd += 1;
+ 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;
@@ -210,15 +228,19 @@ void main_loop(){
move_to_line(addr2,0);
print_lines(p);
}
+ else
+ E;
break;
case '\n':
- if (addr1 > -1)
+ if (addr1 > -1 && addr1 <=pos)
move_to_line(addr1, 1);
- else
+ else if (addr2 <=pos)
print_cur_line(p);
+ else
+ E;
break;
case 'q':
- exit(0);
+ goto cleanup;
break;
case 'w':
cmd +=1;
@@ -238,10 +260,14 @@ void main_loop(){
print_lineno();
break;
default:
- fprintf(stderr, ">>> **** unknown command: '%c'****\n", cmd[0]);
+ if (cmd[0])
+ fprintf(stderr, ">>> **** unknown command: '%c'****\n", cmd[0]);
E;
}
}
+ cleanup:
+ free(c);
+ return;
}
@@ -254,14 +280,15 @@ int main(int argc, char *argv[]){
if (argc > 1){
fname = malloc((strlen(argv[1])+1) * sizeof(char));
memcpy(fname, argv[1], strlen(argv[1]) + 1);
- if (fin = fopen(fname, "r+")){
- read_lines(fin);
+ if (!read_file())
fclose(fin);
- }
else{
perror(fname);
}
}
main_loop();
+ addr1 = pos, addr2 = 1;
+ delete_lines();
+ free(fname);
}