static int edit_read_file(const char *path) { struct line *line; struct line *lastline = NULL; char *filebuffer; char *linestr, *lineend; struct stat s; if (!stat(path, &s)) { filebuffer = read_file(path, NULL); if (!filebuffer) { printf("could not read %s: %s\n", path, errno_str()); return -1; } linestr = filebuffer; while (1) { if (!*linestr) break; lineend = strchr(linestr, '\n'); if (!lineend && !*linestr) break; if (lineend) *lineend = 0; line = line_realloc(strlen(linestr) + 1, NULL); if (!buffer) buffer = line; memcpy(line->data, linestr, strlen(linestr) + 1); line->prev = lastline; if (lastline) lastline->next = line; line->next = NULL; lastline = line; if (!lineend) break; linestr = lineend + 1; } free(filebuffer); } if (!buffer) { buffer = line_realloc(0, NULL); buffer->data[0] = 0; } return 0; }
static void merge_line(struct line *line) { struct line *tmp; line_realloc(strlen(line->data) + strlen(line->next->data) + 1, line); tmp = line->next; line->next = line->next->next; if (line->next) line->next->prev = line; strcat(line->data, tmp->data); line_free(tmp); refresh(1); }
static void insert_char(char c) { int pos = textx; char *line; int end = strlen(curline->data); line_realloc(strlen(curline->data) + 2, curline); line = curline->data; while (end >= pos) { line[end + 1] = line[end]; end--; } line[pos] = c; textx++; refresh_line(curline, cursy); }
static void write_char(int key, t_line *line) { if (key >= 256) { write_char(key % 256, line); write_char(key / 256, line); } else if (key != '\n') { if (line->line_len >= (BUFF_LINE * line->realloc_cpt)) line_realloc(line); add_char_in_tab(key, line->line, line->pos); CAP("nd"); line->pos++; line->line_len++; (line->line_len % get_cols()) == 0 ? clear_scr(line) : clear_and_display(line); } }
static void split_line(void) { int length = strlen(curline->data + textx); struct line *newline = line_realloc(length + 1, NULL); struct line *tmp; memcpy(newline->data, curline->data + textx, length + 1); curline->data[textx] = 0; tmp = curline->next; curline->next = newline; newline->prev = curline; newline->next = tmp; if (tmp) tmp->prev = newline; textx = 0; cursy++; curline = curline->next; refresh(1); }