-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.c
More file actions
203 lines (170 loc) · 5.88 KB
/
editor.c
File metadata and controls
203 lines (170 loc) · 5.88 KB
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
#include "./editor.h"
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#define LINE_INIT_CAPACITY 1024
#define EDITOR_INIT_CAPACITY 128
#define CHUNK_INIT_CAPACITY 1024
static void line_grow(Line* line, size_t n){
size_t new_capacity= line->capacity;
assert(new_capacity >= line->size);
while (new_capacity < line->size + n){
if (new_capacity==0){
new_capacity = LINE_INIT_CAPACITY;
}
else{
new_capacity*=2;
}
}
if (new_capacity != line->capacity){
line->chars= realloc(line->chars, new_capacity);
line->capacity = new_capacity;
}
}
void line_insert_sized_before(Line *line, const char *text, size_t *col, size_t text_size){
if (*col > line->size){
*col=line->size;
}
line_grow(line,text_size);
memmove(line->chars+ *col+text_size,line->chars+*col,line->size-*col);
memcpy(line->chars + *col, text, text_size);
line->size += text_size;
*col += text_size;
}
void line_insert_before(Line *line, const char *text, size_t *col){
size_t text_size = strlen(text);
line_insert_sized_before(line, text, col, text_size);
}
void line_append_sized(Line *line, const char *text, size_t text_size){
size_t col= line->size;
line_insert_sized_before(line, text, &col, text_size);
}
void line_append(Line *line, const char *text){
size_t text_size = strlen(text);
line_append_sized(line, text, text_size);
}
void line_backspace(Line *line, size_t *col){
if (*col > line->size){
*col=line->size;
}
if (line->size > 0 && *col>0){
memmove(line->chars+*col -1, line->chars+*col, line->size - *col);
line->size -= 1;
*col-=1;
}
}
void line_delete(Line *line, size_t *col){
if (*col > line->size){
*col=line->size;
}
if (line->size > 0 && *col<line->size){
memmove(line->chars+*col, line->chars+*col +1 , line->size - *col);
line->size -= 1;
}
}
static void editor_grow(Editor* editor, size_t n){
size_t new_capacity= editor->capacity;
assert(new_capacity >= editor->size);
while (new_capacity < editor->size + n){
if (new_capacity==0){
new_capacity = EDITOR_INIT_CAPACITY;
}
else{
new_capacity*=2;
}
}
if (new_capacity != editor->capacity){
editor->lines= realloc(editor->lines, new_capacity * sizeof(editor->lines[0]));
editor->capacity = new_capacity;
}
}
void editor_push_new_line(Editor* editor){
editor_grow(editor, 1);
memset(&editor->lines[editor->size], 0, sizeof(editor->lines[0]));
editor->size +=1;
}
void editor_check_line(Editor* editor){
if (editor->cursor_row >= editor->size){
if (editor->size > 0){
editor->cursor_row = editor->size -1;
}
else{
editor_push_new_line(editor);
}
}
}
void editor_insert_new_line(Editor* editor){
editor_check_line(editor);
size_t line_size = sizeof(editor->lines[0]);
editor_grow(editor,1);
memmove(editor->lines+ (editor->cursor_row +1),editor->lines + (editor->cursor_row) ,(editor->size - editor->cursor_row) * line_size); //[UNDERSTAND} no need to multiply by line size, causes crash on inserting between text
editor->cursor_row+=1;
memset(&editor->lines[editor->cursor_row], 0, line_size);
editor->size += 1;
editor->cursor_col=0;
}
void editor_insert_before_cursor(Editor* editor, const char* text){
editor_check_line(editor);
line_insert_before(&editor->lines[editor->cursor_row], text, &editor->cursor_col);
}
void editor_backspace(Editor* editor){
editor_check_line(editor);
line_backspace(&editor->lines[editor->cursor_row], &editor->cursor_col);
}
void editor_delete(Editor* editor){
editor_check_line(editor);
line_delete(&editor->lines[editor->cursor_row],&editor->cursor_col);
}
const char* editor_char_under_cursor(Editor* editor){
if (editor->size == 0){
editor_push_new_line(editor);
}
if (editor->cursor_row < editor->size){
if (editor->cursor_col < editor->lines[editor->cursor_row].size){
return &editor->lines[editor->cursor_row].chars[editor->cursor_col];
}
}
return NULL;
}
void editor_save_to_file( Editor *editor, const char *file_path){
FILE *f=fopen(file_path, "w");
if (f ==NULL){
fprintf(stdout, "ERROR: Couldn't open file %s because of %s\n",file_path,strerror(errno));
}
editor_check_line(editor);
for (size_t row=0; (row< editor->size-1) && (editor->size>1); row++){
fwrite(editor->lines[row].chars, 1, editor->lines[row].size,f);
fputc('\n',f);
}
fwrite(editor->lines[editor->size-1].chars, 1, editor->lines[editor->size-1].size,f); //write last line without adding an extra new line
fclose(f);
fprintf(stdout, "SUCCESS: Content saved to %s\n",file_path);
}
void editor_load_from_file( Editor *editor, FILE* f){
assert(editor->lines == NULL && "The editor should be empty");
static char chunk[CHUNK_INIT_CAPACITY];
while(!feof(f)){
size_t n = fread(chunk, 1, sizeof(chunk), f); //reading by chunks for large files
size_t count=0;
char append[CHUNK_INIT_CAPACITY +1];
memset(append, 0, sizeof(append));
//fwrite(chunk, 1, n, stdout);
for (size_t i=0; i<n; i++){
if (chunk[i]=='\n'){
count=0;
editor_insert_before_cursor(editor, append);
editor_insert_new_line(editor);
memset(append, 0, sizeof(append));
}
else{
append[count++]=chunk[i];
}
}
editor_insert_before_cursor(editor, append); // final append to add caracters in the last line which may not have been picked up due to lack of \n
editor->cursor_row=0;
}
}
editor->cursor_row=0;
}