1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4
5 #include "Elementary.h"
6
7 #include "elm_code_private.h"
8
_elm_code_file_line_blank_create(Elm_Code_File * file,int line,void * data)9 static Elm_Code_Line *_elm_code_file_line_blank_create(Elm_Code_File *file, int line, void *data)
10 {
11 Elm_Code_Line *ecl;
12
13 ecl = calloc(1, sizeof(Elm_Code_Line));
14 if (!ecl) return NULL;
15
16 ecl->file = file;
17 ecl->number = line;
18 ecl->status = ELM_CODE_STATUS_TYPE_DEFAULT;
19 ecl->data = data;
20
21 return ecl;
22 }
23
_elm_code_line_ending_get(const char * ending)24 static Elm_Code_File_Line_Ending _elm_code_line_ending_get(const char *ending)
25 {
26 switch (*ending)
27 {
28 case '\r':
29 return ELM_CODE_FILE_LINE_ENDING_WINDOWS;
30 default:
31 return ELM_CODE_FILE_LINE_ENDING_UNIX;
32 }
33 }
34
_elm_code_file_line_insert_data(Elm_Code_File * file,const char * content,unsigned int length,unsigned int row,Eina_Bool mapped,void * data)35 static void _elm_code_file_line_insert_data(Elm_Code_File *file, const char *content, unsigned int length,
36 unsigned int row, Eina_Bool mapped, void *data)
37 {
38 Elm_Code_Line *line, *after;
39
40 line = _elm_code_file_line_blank_create(file, row, data);
41 if (!line) return;
42
43 if (mapped)
44 {
45 line->content = content;
46 line->length = length;
47 }
48 else
49 {
50 line->modified = malloc(sizeof(char)*(length+1));
51 strncpy(line->modified, content, length);
52 line->modified[length] = 0;
53 line->length = length;
54 }
55
56 if (row == 1)
57 file->lines = eina_list_prepend(file->lines, line);
58 else if (row == eina_list_count(file->lines) + 1)
59 file->lines = eina_list_append(file->lines, line);
60 else
61 {
62 after = eina_list_nth(file->lines, row - 2);
63 file->lines = eina_list_append_relative(file->lines, line, after);
64 }
65
66 if (file->parent)
67 {
68 _elm_code_parse_line(file->parent, line);
69 elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
70 }
71 }
72
elm_code_file_filename_get(Elm_Code_File * file)73 EAPI const char *elm_code_file_filename_get(Elm_Code_File *file)
74 {
75 if (!file->file)
76 return NULL;
77
78 return ecore_file_file_get(eina_file_filename_get(file->file));
79 }
80
elm_code_file_path_get(Elm_Code_File * file)81 EAPI const char *elm_code_file_path_get(Elm_Code_File *file)
82 {
83 if (!file->file)
84 return NULL;
85
86 return eina_file_filename_get(file->file);
87 }
88
_elm_code_file_tmp_path_get(Elm_Code_File * file)89 EAPI char *_elm_code_file_tmp_path_get(Elm_Code_File *file)
90 {
91 const char *name, *path;
92 char *tmp;
93 size_t dirlen;
94
95 path = elm_code_file_path_get(file);
96 name = elm_code_file_filename_get(file);
97 dirlen = strlen(path) - strlen(name);
98
99 tmp = malloc(sizeof(char) * (strlen(path) + 6));
100 if (!tmp) return NULL;
101 snprintf(tmp, dirlen + 1, "%s", path);
102 snprintf(tmp + dirlen, strlen(name) + 6, ".%s.tmp", name);
103
104 return tmp;
105 }
106
elm_code_file_new(Elm_Code * code)107 EAPI Elm_Code_File *elm_code_file_new(Elm_Code *code)
108 {
109 Elm_Code_File *ret;
110
111 if (code->file)
112 elm_code_file_free(code->file);
113
114 ret = calloc(1, sizeof(Elm_Code_File));
115 if (!ret) return NULL;
116 code->file = ret;
117 ret->parent = code;
118
119 return ret;
120 }
121
elm_code_file_open(Elm_Code * code,const char * path)122 EAPI Elm_Code_File *elm_code_file_open(Elm_Code *code, const char *path)
123 {
124 EINA_SAFETY_ON_NULL_RETURN_VAL(code, NULL);
125
126 Elm_Code_File *ret;
127 Eina_File *file;
128 Eina_File_Line *line;
129 Eina_Iterator *it;
130 unsigned int lastindex;
131
132 ret = elm_code_file_new(code);
133 file = eina_file_open(path, EINA_FALSE);
134 ret->file = file;
135 ret->mime = efreet_mime_type_get(path);
136 lastindex = 1;
137
138 ret->map = eina_file_map_all(file, EINA_FILE_POPULATE);
139 it = eina_file_map_lines(file);
140 EINA_ITERATOR_FOREACH(it, line)
141 {
142 Elm_Code_Line *ecl;
143
144 if (lastindex == 1)
145 ret->line_ending = _elm_code_line_ending_get(line->start + line->length);
146
147 /* Working around the issue that eina_file_map_lines does not trigger an item for empty lines */
148 /* This was fixed in 1.13.99 so once we depend on 1.14 minimum this can go */
149 while (lastindex < line->index - 1)
150 {
151 ecl = _elm_code_file_line_blank_create(ret, ++lastindex, NULL);
152 if (!ecl) continue;
153
154 ret->lines = eina_list_append(ret->lines, ecl);
155 }
156
157 _elm_code_file_line_insert_data(ret, line->start, line->length, lastindex = line->index, EINA_TRUE, NULL);
158 }
159 eina_iterator_free(it);
160
161 if (ret->parent)
162 {
163 _elm_code_parse_file(ret->parent, ret);
164 elm_code_callback_fire(ret->parent, &ELM_CODE_EVENT_FILE_LOAD_DONE, ret);
165 }
166 return ret;
167 }
168
elm_code_file_save(Elm_Code_File * file)169 EAPI void elm_code_file_save(Elm_Code_File *file)
170 {
171 Eina_List *item;
172 Elm_Code *code;
173 Elm_Code_Line *line_item;
174 FILE *out;
175 const char *path, *content, *crchars;
176 char *tmp;
177 unsigned int length;
178 short crlength;
179 struct stat st;
180 mode_t mode;
181 Eina_Bool have_mode = EINA_FALSE;
182
183 code = file->parent;
184 path = elm_code_file_path_get(file);
185 tmp = _elm_code_file_tmp_path_get(file);
186 crchars = elm_code_file_line_ending_chars_get(file, &crlength);
187
188 out = fopen(tmp, "w");
189 if (out == NULL)
190 {
191 free(tmp);
192 return;
193 }
194 if (fstat(fileno(out), &st) != -1)
195 {
196 mode = st.st_mode;
197 have_mode = EINA_TRUE;
198 }
199
200 EINA_LIST_FOREACH(file->lines, item, line_item)
201 {
202 if (code && code->config.trim_whitespace &&
203 !elm_code_line_contains_widget_cursor(line_item))
204 elm_code_line_text_trailing_whitespace_strip(line_item);
205 content = elm_code_line_text_get(line_item, &length);
206
207 if (fwrite(content, sizeof(char), length, out) != (size_t)length)
208 break;
209 if (fwrite(crchars, sizeof(char), crlength, out) != (size_t)crlength)
210 break;
211 }
212 fclose(out);
213
214 ecore_file_mv(tmp, path);
215 free(tmp);
216
217 if (have_mode)
218 {
219 if(chmod(path, mode) < 0)
220 {
221 ERR("Error in chmod(%s, %d) - %d(%s)\n", path, mode, errno, strerror(errno));
222 return;
223 }
224 }
225
226 if (file->parent)
227 {
228 _elm_code_parse_reset_file(file->parent, file);
229 _elm_code_parse_file(file->parent, file);
230 elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_FILE_LOAD_DONE, file);
231 }
232 }
233
elm_code_file_free(Elm_Code_File * file)234 EAPI void elm_code_file_free(Elm_Code_File *file)
235 {
236 Elm_Code_Line *l;
237
238 EINA_LIST_FREE(file->lines, l)
239 {
240 elm_code_line_free(l);
241 }
242
243 elm_code_file_close(file);
244 free(file);
245 }
246
elm_code_file_close(Elm_Code_File * file)247 EAPI void elm_code_file_close(Elm_Code_File *file)
248 {
249 if (!file->file)
250 return;
251
252 if (file->map)
253 eina_file_map_free(file->file, file->map);
254
255 eina_file_close(file->file);
256 file->file = NULL;
257 }
258
elm_code_file_line_ending_get(Elm_Code_File * file)259 EAPI Elm_Code_File_Line_Ending elm_code_file_line_ending_get(Elm_Code_File *file)
260 {
261 return file->line_ending;
262 }
263
elm_code_file_line_ending_chars_get(Elm_Code_File * file,short * length)264 EAPI const char *elm_code_file_line_ending_chars_get(Elm_Code_File *file, short *length)
265 {
266 if (length)
267 {
268 if (elm_code_file_line_ending_get(file) == ELM_CODE_FILE_LINE_ENDING_WINDOWS)
269 *length = 2;
270 else
271 *length = 1;
272 }
273
274 if (elm_code_file_line_ending_get(file) == ELM_CODE_FILE_LINE_ENDING_WINDOWS)
275 return "\r\n";
276 return "\n";
277 }
278
elm_code_file_clear(Elm_Code_File * file)279 EAPI void elm_code_file_clear(Elm_Code_File *file)
280 {
281 Elm_Code_Line *l;
282
283 EINA_LIST_FREE(file->lines, l)
284 {
285 elm_code_line_free(l);
286 }
287
288 if (file->parent)
289 elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_FILE_LOAD_DONE, file);
290 }
291
elm_code_file_lines_get(Elm_Code_File * file)292 EAPI unsigned int elm_code_file_lines_get(Elm_Code_File *file)
293 {
294 return eina_list_count(file->lines);
295 }
296
297
elm_code_file_line_append(Elm_Code_File * file,const char * line,int length,void * data)298 EAPI void elm_code_file_line_append(Elm_Code_File *file, const char *line, int length, void *data)
299 {
300 int row;
301
302 row = elm_code_file_lines_get(file) + 1;
303 _elm_code_file_line_insert_data(file, line, length, row, EINA_FALSE, data);
304 }
305
elm_code_file_line_insert(Elm_Code_File * file,unsigned int row,const char * line,int length,void * data)306 EAPI void elm_code_file_line_insert(Elm_Code_File *file, unsigned int row, const char *line, int length, void *data)
307 {
308 Eina_List *item;
309 Elm_Code_Line *line_item;
310 unsigned int r;
311
312 _elm_code_file_line_insert_data(file, line, length, row, EINA_FALSE, data);
313
314 r = row;
315 EINA_LIST_FOREACH(file->lines, item, line_item)
316 {
317 if (line_item->number < row)
318 continue;
319
320 line_item->number = r++;
321 }
322 }
323
elm_code_file_line_remove(Elm_Code_File * file,unsigned int row)324 EAPI void elm_code_file_line_remove(Elm_Code_File *file, unsigned int row)
325 {
326 Eina_List *item, *next;
327 Elm_Code_Line *line_item, *tofree = NULL;
328 unsigned int r;
329
330 r = row;
331 EINA_LIST_FOREACH_SAFE(file->lines, item, next, line_item)
332 {
333 if (line_item->number < row)
334 continue;
335 else if (line_item->number == row)
336 {
337 tofree = line_item;
338 file->lines = eina_list_remove_list(file->lines, item);
339 continue;
340 }
341
342 line_item->number = r++;
343 }
344
345 if (tofree)
346 elm_code_line_free(tofree);
347 }
348
elm_code_file_line_get(Elm_Code_File * file,unsigned int number)349 EAPI Elm_Code_Line *elm_code_file_line_get(Elm_Code_File *file, unsigned int number)
350 {
351 return eina_list_nth(file->lines, number - 1);
352 }
353