edit_functions.c - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/edit_functions.c
blob: 7fd79a630b8599e7fdcd388ffc64f9dbe617ea41 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "edit_functions.h"
/**
* Get the actual position of the character in the pos position on the screen
*
* @param ln
* @param rpos
* @return 
*/
int getActualPos(line *ln,int rpos)
{
 int pos_screen=0,pos_real=0;
 character *caux=ln->first_ch;
 while(caux)
 {
 if(caux->c == '\t')
 pos_screen+=SPACES_TAB-1;
 pos_screen++;
 if(pos_screen > rpos)
 return pos_real;
 caux=caux->next;
 pos_real++;
 }
 return --pos_real;
}
/**
* Get the actual position of the character in the pos position on the screen
*
* @param ln
* @param rpos
* @return 
*/
int getScreenPos(line *ln, int rpos)
{
 int cont=0;
 character *aux=ln->first_ch;
 while(aux && cont<rpos)
 {
 if(aux->c=='\t')
 cont+=SPACES_TAB-1;
 cont++;
 aux=aux->next;
 
 }
 if(cont>rpos)
 cont-=SPACES_TAB;
 if(!aux)
 cont--;
 return cont;
 
}
/**
* Get the number of tabs of the line ln, starting in position pos
*
* @param ln
* @param pos
* @return 
*/
int getNumTabs(line *ln, int pos)
{
 character *aux=ln->first_ch;
 int cont=0,num_tbs=0;
 while( aux && cont < pos )
 {
 aux=aux->next;
 cont++;
 }
 
 while(aux)
 {
 
 if(aux->c == '\t')
 {
 num_tbs++;
 }
 aux=aux->next;
 }
 return num_tbs;
}
/**
* Get the position of the character ch in the line ln
*
* @param ln
* @param ch
* @return 
*/
int getPosCh(line *ln, character *ch)
{
 character *aux=ln->first_ch;
 int cont=0;
 while(aux && aux!=ch)
 {
 cont++;
 aux=aux->next;
 }
 if(!aux)
 {
 printw("ERROR ON getPosCH\n");
 return -1;
 }
 return cont;
 
}
/**
* Print SPACES_TAB blank spaces 
*/
void print_tab()
{
 int i;
 for(i=0;i<SPACES_TAB;i++)
 printw(" ");
}
void empty_line(int nline,int size_line)
{
 int i;
 move(nline,0);
 for(i=size_line-1;i>=0;i--)
 delch();
}
/**
* Print the all the options at the bottom of the screen
*/
void print_options()
{
 move(LINES-1,0);
 printw("^F:EXIT ^X:EXECUTE ^H:SAVE ^L:LOAD ^W:CLEAR");
}
/**
* Free all the memory allocated to data structures of the editor
*
*/
void freeEditor()
{
 line *aux_l=first_line,*aux_l2;
 character *aux_c=aux_l->first_ch,*aux_c2;
 while(aux_l)
 {
 aux_l2=aux_l->next;
 while(aux_c)
 {
 aux_c2=aux_c->next;
 freeCharacter(&aux_c);
 aux_c=aux_c2;
 } 
 freeLine(&aux_l);
 aux_l=aux_l2;
 }
 clear();
 move(0,0);
 refresh();
}
/**
 * Initialize the data structures of the editor
 *
 */
void init_editor()
{
 modified=0;//if the user has pressed any key
 ncurrent_line=0;//number of the current line
 num_lines=1; //number of lines
 actual_rows=LINES-2;
 actual_cols=COLS;
 num_lines_scrolled=0;
 init_line(&first_line);
 init_character(&eof);
 eof->c='0円';//EOF
 init_character(&prev_character);
 current_line=first_line;
 current_line->num_line=1;
 current_character=eof;
 prev_character=eof;
 current_line->first_ch=current_character;
 current_line->size=1;
 print_options();
 move(0,0);
}
/**
 * Returns the character in the pos position in the ln line
 * 
 * @param ln 
 * @param pos 
 * @return 
 */
character* getChLine(line *ln,int pos)
{
 character *ret=ln->first_ch;
 int n=0;
 
 while(n<pos && ret)
 {
 ret=ret->next;
 n++;
 }
 
 return ret;
 
}
/**
 * Free the memory allocated to character c
 * @param c
 */
void freeCharacter(character **c)
{
 free(*c);
 *c=NULL;
}
/**
 * Free the memory allocated to line l
 * @param l
 */
void freeLine(line **l)
{
 free(*l);
 *l=NULL;
}
/**
 * Print the text written in the editor, from the line ln, in the Window wn
 * to the end of the text
 * @param ln
 * @param wn
 */
void print_ln(const line *ln, WINDOW *wn)
{
 character *aux=ln->first_ch;
 while(aux)
 {
 if(aux->c != '\n' && aux->c !='0円' && aux->c!='\t')
 wprintw(wn,"%c",aux->c);
 else if(aux->c == '\t')
 print_tab();
 aux=aux->next;
 }
}
/**
 * Allocates memory to the character ch
 * @param ch
 */
void init_character(character **ch)
{
 *ch=malloc(sizeof(struct character));
 (*ch)->next=NULL;
 (*ch)->prev=NULL;
}
/**
 * Allocates memory to the line l
 * @param l
 */
void init_line(line **l)
{
 *l=malloc(sizeof(struct line));
 (*l)->next=NULL;
 (*l)->prev=NULL; 
 (*l)->first_ch=NULL;
 (*l)->size=0;
 (*l)->num_tabs=0;
}
/**
 * Print a subset of the written text in the editor, taking into account the 
 * number of lines scrolled and the number of rows of the screen
 * @param ln_scr number of lines scrolled
 * @param num_rows number of rows of the screen
 */
void print_all(int ln_scr, int num_rows)
{
 int n=0;
 line *aux=first_line;
 while(n<ln_scr && aux)
 {
 aux=aux->next;
 n++;
 }
 clear();
 n=0;
 while(aux && n<num_rows)
 {
 move(n,0);
 print_ln(aux,stdscr);
 aux=aux->next;
 n++;
 } 
 print_options();
 refresh();
}
/**
* Loads the file file
* @param file The descriptor of the file
*/
void loadFile(int file)
{
 char buff[1];
 line *prev_line=NULL,*curr_line=first_line;
 character *curr_c=NULL,*prev_character=NULL;
 curr_line->size=0;int cont=0;
 while(read(file,buff,1))
 {
 if(buff[0] == '\t')
 curr_line->num_tabs++;
 init_character(&curr_c);
 curr_c->c=buff[0];
 if(prev_character)
 prev_character->next=curr_c;
 curr_c->prev=prev_character;
 if(curr_line->size == 0)
 curr_line->first_ch=curr_c;
 curr_line->size++;
 prev_character=curr_c;
 if(buff[0] == '\n')
 {
 num_lines++;
 curr_c->next=NULL;
 prev_character=NULL;
 prev_line=curr_line;
 init_line(&curr_line);
 prev_line->next=curr_line;
 curr_line->prev=prev_line;
 curr_line->size=0;
 curr_line->num_line=num_lines;
 curr_line->first_ch=eof;
 }
 
 //printw("c[%d]=%c;\n",cont,buff[0]);
 cont++;
 }
 //the last character is eof
 if(curr_line->first_ch != eof)
 {
 curr_c->next=eof;
 eof->prev=curr_c;
 }
 curr_line->size++;//the eof character
 current_character=first_line->first_ch;
 current_line=first_line;
}
/**
 * Save the contents of the editor to the file file, 
 * @param file The descriptor of the file
 * @return 
 */
int saveToFile(int file)
{
 int i,last=0;
 character *caux;
 char *buffer;
 line *aux=first_line;
 do
 {
 buffer=malloc(aux->size);
 caux=aux->first_ch;
 for(i=0;i<aux->size && caux;i++)
 {
 if(caux -> c != '0円')
 {
	buffer[i]=caux->c;
 }
 else
	last=1;
 caux=caux->next;
 }
 write(file,buffer, last==0 ? aux->size: aux->size-1);
 aux=aux->next;
 }
 while(aux);
 return 1;
}
generated by cgit v1.2.3 (git 2.25.1) at 2025年09月13日 02:40:11 +0000

AltStyle によって変換されたページ (->オリジナル) /