-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_io.c
More file actions
126 lines (107 loc) · 2.53 KB
/
file_io.c
File metadata and controls
126 lines (107 loc) · 2.53 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
#include "marisa.h"
extern editorConfig E;
wchar_t *
editorRowsToString (int *buflen)
{
int totlen = 0;
wchar_t *buf, *p;
if (BUFFER->numrows == 0)
{
return NULL;
}
else
{
for (int i = 0; i < BUFFER->numrows; i++)
totlen += BUFFER->row[i].size + 1; /* need to add one to add the new line character at the end of each row */
}
*buflen = totlen;
buf = (wchar_t *) malloc(totlen * sizeof(wchar_t));
p = buf;
for (int i = 0; i < BUFFER->numrows; i++)
{
wmemcpy(p, BUFFER->row[i].chars, BUFFER->row[i].size);
p += BUFFER->row[i].size;
*p = '\n';
p++;
}
/* return the pointer to the start of the string buffer */
return buf;
}
void
editorOpen (char *filename)
{
FILE *fp;
wchar_t *line= NULL;
char *fileLine = NULL;
size_t linecap = 0;
ssize_t linelen;
free(BUFFER->filename);
BUFFER->filename = strdup(filename);
if ((fp = fopen(filename, "r")) == NULL)
die(L"fopen");
/* copy filename into memory so we can do operations on it. */
char *newBufferName = strdup(filename);
newBuffer(&newBufferName, filename);
switchBuffer(newBufferName);
while ((linelen = getline(&fileLine, &linecap, fp)) != -1)
{
line = (wchar_t *) malloc(linelen * sizeof(wchar_t));
line = stows(fileLine, linelen);
linelen = wcslen(line);
while (linelen > 0 && (line[linelen - 1] == '\n' ||
line[linelen - 1] == '\r'))
linelen--;
bufferInsertRow(BUFFER->numrows, line, linelen);
free(line);
}
free(fileLine);
fclose(fp);
BUFFER->flags.dirty = CLEAN;
}
void
editorSave (void)
{
int len;
int fd;
wchar_t *buf;
if (BUFFER->filename == NULL)
BUFFER->filename = editorPrompt(L"Save as: %s");
if (BUFFER->filename == NULL)
{
editorSetStatusMessage(L"Save aborted");
return;
}
/* TODO: editorRowsToString is setting len to a wrong value */
if ((buf = editorRowsToString(&len)) == NULL || BUFFER->flags.dirty == CLEAN)
{
editorSetStatusMessage(L"Nothing to save...");
return;
}
if ((fd = open(BUFFER->filename, O_RDWR | O_CREAT, 0644)) == -1)
{
free(buf);
editorSetStatusMessage(L"Can't save! I/O error: %s", strerror(errno));
}
else if (ftruncate(fd, len) == -1)
{
close(fd);
free(buf);
editorSetStatusMessage(L"Can't save! I/O error: %s", strerror(errno));
}
char *buf_char = wstos(buf, len);
len = strlen(buf_char);
if (write(fd, buf_char, len) == len)
{
close(fd);
free(buf);
BUFFER->flags.dirty = CLEAN;
editorSetStatusMessage(L"%d bytes written to disk", len);
return;
}
else
{
close(fd);
free(buf);
editorSetStatusMessage(L"Can't save! I/O error: %s", strerror(errno));
}
}