1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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(){}
|