@@ -47,14 +47,8 @@ char *append_string_by_copy(darray *array, char *string);
47
47
48
48
char * get_string_at (const darray * array , int index );
49
49
50
- void remove_string_at (darray * array , int index );
51
-
52
50
char * save_and_replace (darray * save_array , darray * write_array , char * new_line , int index );
53
51
54
- void save_and_remove (darray * lines_deleted , darray * text , int index );
55
-
56
- void free_darray (darray * array );
57
-
58
52
bool contains_index (darray * array , int index );
59
53
60
54
bool valid_address (int addr1 );
@@ -76,8 +70,6 @@ void delete(int addr1, int addr2);
76
70
77
71
void delete_without_undo (int addr1 , int addr2 );
78
72
79
- void remove_lines (darray * text , int index_to_delete , int number_of_lines );
80
-
81
73
void undo (int number );
82
74
83
75
void undo_change (stack_node * undo_node );
@@ -136,33 +128,10 @@ char *append_string_by_copy(darray *array, char *string) {
136
128
137
129
}
138
130
139
- void insert_string_by_reference (darray * array , int index , char * string ) {
140
-
141
- if (array -> n == array -> capacity )
142
- enlarge_darray (array );
143
-
144
- array -> n ++ ;
145
-
146
- for (int i = array -> n - 1 ; i > index ; i -- )
147
- array -> strings [i ] = array -> strings [i - 1 ];
148
-
149
- array -> strings [index ] = string ;
150
- }
151
-
152
-
153
131
char * get_string_at (const darray * array , int index ) {
154
132
return array -> strings [index ];
155
133
}
156
134
157
- void remove_string_at (darray * array , int index ) {
158
-
159
- //shift all strings by one and free the deleted deleted_string
160
- for (int i = index + 1 ; i < array -> n ; i ++ )
161
- array -> strings [i - 1 ] = array -> strings [i ];
162
-
163
- array -> n -- ;
164
- }
165
-
166
135
void replace_string_by_reference (darray * array , int index , char * string ) {
167
136
array -> strings [index ] = string ;
168
137
}
@@ -193,38 +162,6 @@ char *save_and_replace(darray *save_array, darray *write_array, char *new_line,
193
162
194
163
}
195
164
196
- void save_and_remove (darray * lines_deleted , darray * text , int index ) {
197
- //equivalent to:
198
- //append_string_by_copy(lines_deleted, get_string_at(text_array, line_to_delete)); //save deleted string to undo stack
199
- //remove_string_at(text_array, line_to_delete);
200
-
201
- if (lines_deleted -> n == lines_deleted -> capacity )
202
- enlarge_darray (lines_deleted );
203
-
204
-
205
- //append to lines deleted
206
- lines_deleted -> strings [lines_deleted -> n ] = text -> strings [index ];
207
- lines_deleted -> n ++ ;
208
-
209
- //shift all strings by one
210
- for (int i = index + 1 ; i < text -> n ; i ++ )
211
- text -> strings [i - 1 ] = text -> strings [i ];
212
-
213
- text -> n -- ;
214
-
215
- }
216
-
217
- void free_darray (darray * array ) {
218
- int n = array -> n ;
219
-
220
- for (int i = 0 ; i < n ; i ++ ) {
221
- if (array -> strings [i ] != NULL )
222
- free (array -> strings [i ]);
223
- }
224
- free (array -> strings );
225
- free (array );
226
- }
227
-
228
165
bool contains_index (darray * array , int index ) {
229
166
return index < array -> n && index >= 0 ;
230
167
}
@@ -305,7 +242,6 @@ void execute_pending_undo() {
305
242
}
306
243
307
244
void clear_redo () {
308
-
309
245
redo_stack -> top = NULL ; //intentional memory leak
310
246
redo_stack -> size = 0 ;
311
247
}
@@ -508,26 +444,25 @@ void print(int addr1, int addr2) {
508
444
509
445
//append \n before each line, except if it's first print
510
446
if (current_index < 0 ) {
511
- if (!first_print ) fputc ('\n' , stdout );
512
- fputc ('.' , stdout );
447
+ if (!first_print ) fputc_unlocked ('\n' , stdout );
448
+ fputc_unlocked ('.' , stdout );
513
449
return ;
514
450
}
515
451
516
452
while (current_index <= addr2 - 1 ) {
517
453
518
- if (!first_print ) fputc ('\n' , stdout );
454
+ if (!first_print ) fputc_unlocked ('\n' , stdout );
519
455
520
456
if (contains_index (text_array , current_index )) {
521
457
fputs (get_string_at (text_array , current_index ), stdout );
522
458
} else
523
- fputc ('.' , stdout );
459
+ fputc_unlocked ('.' , stdout );
524
460
525
461
current_index ++ ;
526
462
first_print = false;
527
463
}
528
464
}
529
465
530
- //todo
531
466
void delete (int addr1 , int addr2 ) {
532
467
533
468
execute_pending_undo ();
@@ -550,11 +485,8 @@ void delete(int addr1, int addr2) {
550
485
return ;
551
486
}
552
487
553
- first_print = false;
554
-
555
488
lines_deleted = new_darray (num_to_delete );
556
489
557
-
558
490
for (int i = first_index ; i <= last_index ; i ++ )
559
491
append_string_by_reference (lines_deleted , text_array -> strings [i ]);
560
492
@@ -623,45 +555,6 @@ void append_node_lines_to_text(darray *text, stack_node *node, int lines_to_add)
623
555
624
556
}
625
557
626
- void remove_lines (darray * text , int index , int num_lines_to_remove ) {
627
-
628
- for (int i = 0 ; i < num_lines_to_remove ; i ++ ) {
629
- //remove_string_at(text, index);
630
-
631
- //shift all strings by one
632
- for (int i = index + 1 ; i < text -> n ; i ++ )
633
- text -> strings [i - 1 ] = text -> strings [i ];
634
-
635
- text -> n -- ;
636
- }
637
- }
638
-
639
- void insert_node_lines_in_text (stack_node * node , int addr1 ) {
640
-
641
- int lines_to_add = node -> lines -> n ;
642
-
643
- for (int j = 0 ; j < lines_to_add ; j ++ ) {
644
- //insert_string_by_reference(text_array, addr1 + j - 1, node->lines->strings[j]);
645
-
646
- int text_index = addr1 + j - 1 ;
647
-
648
- if (text_array -> n == text_array -> capacity )
649
- enlarge_darray (text_array );
650
-
651
- text_array -> n ++ ;
652
-
653
- //shift all elements
654
- for (int i = text_array -> n - 1 ; i > text_index ; i -- )
655
- text_array -> strings [i ] = text_array -> strings [i - 1 ];
656
-
657
- text_array -> strings [text_index ] = node -> lines -> strings [j ];
658
-
659
- }
660
-
661
-
662
- }
663
-
664
-
665
558
void insert (stack_node * node , int addr1 ) {
666
559
667
560
int num_added = node -> lines -> n ;
0 commit comments