aboutsummaryrefslogtreecommitdiffstats
path: root/io.c
blob: d14a65d43269ab999f65b278ac971ed77d05cd91 (plain) (blame)
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
/* Copyright (c) 2021 nytpu <alex@nytpu.com>
 * SPDX-License-Identifier: GPL-3.0-only
 * For more license details, see LICENSE or <https://www.gnu.org/licenses/gpl-3.0.html>.
 */

#include "io.h"
#include "buffer.h"
#include "common.h"

#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char *
rows_to_string(long *buflen)
{
	long len = 0;
	for (RowNum i = 0; i < E.numrows; ++i) {
		len += E.rows[i].size + 1;
	}
	*buflen = len;

	char *buf = malloc(len);
	char *h = buf;
	for (RowNum i = 0; i < E.numrows; ++i) {
		memcpy(h, E.rows[i].chars, E.rows[i].size);
		h += E.rows[i].size;
		*h = '\n';
		++h;
	}
	return buf;
}

void
open_file(const char *fn, bool force)
{
	if (E.dirty && !force) {
		error("Buffer modified");
	}

	free(E.filename);
	E.filename = strdup(fn);

	FILE *f = fopen(fn, "r");
	if (!f) {
		// filename is already saved so we could potentially write in
		// the future
		if (errno != ENOENT) {
			die("Opening file");
		}
		return;
	}

	char *l = NULL;
	size_t lcap = 0;
	ssize_t llen;
	long tot = 0;
	while ((llen = getline(&l, &lcap, f)) != -1) {
		while (llen > 0 && (l[llen-1] == '\n')) --llen;
		insert_row(E.numrows+1, l, llen);
		tot += llen + 1;
	}
	free(l);
	fclose(f);
	E.crow = E.numrows;
	E.dirty = false;
	if (!E.quiet) printf("%ld\n", tot);
	return;
}

int
write_file(const char *fn)
{
	int fd;
	if (fn == NULL) {
		if (E.filename == NULL) {
			error("No current filename");
			return -1;
		}
		fd = open(E.filename, O_RDWR|O_CREAT, 0644);
	} else {
		fd = open(fn, O_RDWR|O_CREAT, 0644);
	}
	if (fd == -1) {
		error("Failed to open file for writing");
		return -1;
	}

	long len;
	char *buf = rows_to_string(&len);
	int err = ftruncate(fd, len);
	if (err == -1) goto ioerr;
	err = write(fd, buf, len);
	if (err != len) goto ioerr;

	close(fd);
	free(buf);
	E.dirty = false;
	if (!E.quiet) printf("%ld\n", len);
	return 0;

ioerr:
	free(buf);
	if (fd != -1) close(fd);
	error("IO error");
	return -1;
}

int
print_rows(RowNum from, RowNum to)
{
	if (!in_bounds(from, E.numrows)) return -1;
	if (!in_bounds(to, E.numrows)) return -1;
	--from; --to;
	for (; from <= to; ++from) {
		printf("%s\n", E.rows[from].chars);
	}
	E.crow = from + 1;
	return 0;
}

int
print_rows_numbered(RowNum from, RowNum to)
{
	if (!in_bounds(from, E.numrows)) return -1;
	if (!in_bounds(to, E.numrows)) return -1;
	--from; --to;
	for (; from <= to; ++from) {
		printf("%ld\t%s\n", from+1, E.rows[from].chars);
	}
	E.crow = from + 1;
	return 0;
}

int
print_rows_escaped(RowNum from, RowNum to)
{
	// TODO: stubbed
	return print_rows(from, to);
}

void
insert_mode(RowNum at)
{
	if (!in_bounds(at, E.numrows)) return;
	char *l = NULL;
	size_t lcap = 0;
	ssize_t llen;
	while ((llen = getline(&l, &lcap, stdin)) != -1) {
		while (llen > 0 && (l[llen-1] == '\n')) --llen;
		if (llen == 1 && l[0] == '.') break;
		insert_row(at, l, llen);
		++at;
	}
	E.crow = at;
	free(l);
}