aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common.cpp34
1 files changed, 19 insertions, 15 deletions
diff --git a/src/common.cpp b/src/common.cpp
index c768f77..73fa956 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -1,24 +1,28 @@
#include "common.h"
-
#include <fcntl.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
#include <unistd.h>
+static size_t file_size(FILE* fd, size_t s) {
+ char buf[1024];
+ size_t x = fread(buf, sizeof(char), 1024, fd);
+ if (x < 1024) {
+ return x + s;
+ }
+ else {
+ return file_size(fd, x + s);
+ }
+}
+
+
line_view load_file(const char* path) {
- int fd;
- struct stat fs;
- fd = open(path, O_RDONLY);
- if (fd == -1)
- return {nullptr, size_t(0)};
- if (fstat(fd, &fs) == -1)
+ FILE* fd = fopen(path, "r");
+ if (fd == nullptr) {
return {nullptr, size_t(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;
+ }
+ size_t size = file_size(fd, 0);
+ fseek(fd, 0, SEEK_SET);
+ char* ptr = (char *) malloc(size);
+ return {ptr, size};
}
line_view next_line(line_view file, size_t* offset) {