blob: 8442bf6aad16bcc0d9973ce724067e8cb8f48d5c (
plain)
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
|
#include "common.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
line_view load_file(const char* path) {
int fd;
struct stat fs;
fd = open(path, O_RDONLY);
if (fd == -1)
return {nullptr, 0};
if (fstat(fd, &fs) == -1)
return {nullptr, 0};
line_view lv;
lv.length = fs.st_size;
lv.line = static_cast<const char*>(mmap(NULL, lv.length, PROT_READ, MAP_PRIVATE, fd, 0));
close(fd);
return lv;
}
line_view next_line(line_view file, size_t* offset) {
const char* p1 = file.line + *offset;
const char* p2 = p1;
const char* end = file.line + file.length;
while (p2 < end && *p2 != '\n') {
p2++;
}
*offset = p2 - file.line + 1;
return {p1, static_cast<size_t>(p2 - p1 + 1)};
}
|