aboutsummaryrefslogtreecommitdiff
path: root/src/common.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.cpp')
-rw-r--r--src/common.cpp25
1 files changed, 22 insertions, 3 deletions
diff --git a/src/common.cpp b/src/common.cpp
index 8ac3a96..d4457c6 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -5,9 +5,28 @@
#include <sys/stat.h>
line_view load_file(const char* path) {
- return {nullptr, 0};
+ 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));
+ return lv;
}
-line_view next_line(const char* file, size_t offset) {
- return {nullptr, 0};
+line_view next_line(line_view file) {
+ static size_t offset = 0;
+ line_view lv;
+ lv.line = file.line + offset;
+ lv.length = 0;
+ while (offset < file.length && file.line[offset] != '\n') {
+ lv.length += 1;
+ offset++;
+ }
+ return lv;
}