+/Femto

toy text editor

git clone https://git.julia.blue/+/Femto

Joseph RyanFixup973c5b9

master
2.7 KiB103 linesraw
1#include "../inc/fileio.h"
2
3extern int ENABLE_LINE_NUMS;
4extern int LINE_NUM_WIDTH;
5
6void openFile(char* filename) {
7    free(E.filename);
8    LINE_NUM_WIDTH = 0;
9    E.filename = strdup(filename);
10    FILE* fp = fopen(filename, "r");
11    if (!fp) { return; }
12    detectLang();
13    char* line = NULL;
14    size_t linecap = 0;
15    ssize_t linelen;
16    while ((linelen = getline(&line, &linecap, fp)) != -1) {
17        while (linelen > 0 &&
18               (line[linelen - 1] == '\n' || line[linelen - 1] == '\r')) {
19            --linelen;
20        }
21        char* temp = NULL;
22        linelen = removeTabs(line, linelen, &temp);
23        insertRow(E.numrows, temp, linelen);
24    }
25    free(line);
26    fclose(fp);
27    E.dirty = 0;
28}
29
30void saveFile() {
31    if (E.filename == NULL) {
32        if ((E.filename = prompt("Save as: %s (ESC to cancel)", NULL)) ==
33            NULL) {
34            setStatusMessage("Save aborted");
35            return;
36        }
37        detectLang();
38    }
39    int len;
40    char* buf = joinRows(&len);
41    int fd = open(E.filename, O_RDWR | O_CREAT, 0644);
42    if (fd != -1) {
43        if (ftruncate(fd, len) != -1 && write(fd, buf, len) == len) {
44            close(fd);
45            free(buf);
46            E.dirty = 0;
47            setStatusMessage("Wrote %d bytes to disk", len);
48            return;
49        }
50        close(fd);
51    }
52    free(buf);
53    setStatusMessage("Save failed! I/O error: %s", strerror(errno));
54}
55
56char* joinRows(int* buflen) {
57    int len = 0;
58    for (int i = 0; i < E.numrows; ++i) { len += E.row[i].size + 1; }
59    *buflen = len;
60
61    char* buf = malloc(len);
62    char* p = buf;
63    for (int i = 0; i < E.numrows; ++i) {
64        memcpy(p, E.row[i].text, E.row[i].size);
65        p += E.row[i].size;
66        *p = '\n';
67        ++p;
68    }
69    return buf;
70}
71
72void detectLang() {
73    E.syntax = NULL;
74    if (E.filename == NULL) { return; }
75    char* ext = strrchr(E.filename, '.');
76    if (ext == NULL) { return; }
77    ++ext;
78    for (int i = 0; (unsigned)i < NUM_SYNTAX; ++i) {
79        struct editorSyntax* s = &HL_SETTINGS[i];
80        if (strstr(s->filematch, ext) != NULL) {
81            E.syntax = s;
82            for (int i = 0; i < E.numrows; ++i) { renderRow(&E.row[i]); }
83            return;
84        }
85    }
86}
87
88ssize_t removeTabs(char* in, ssize_t len, char** out) {
89    int tabs = 0;
90    for (int i = 0; i < len; ++i) { tabs += in[i] == '\t'; }
91    *out = malloc(len + tabs * (E.tabSize - 1) + 1);
92    int j = 0;
93    for (int i = 0; i < len; ++i) {
94        if (in[i] == '\t') {
95            (*out)[j++] = ' ';
96            while (j % E.tabSize != 0) { (*out)[j++] = ' '; }
97        } else {
98            (*out)[j++] = in[i];
99        }
100    }
101    (*out)[j] = '\0';
102    return j;
103}