diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-10 21:24:20 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-10 21:24:20 -0300 |
commit | 5c8326ae953eee1cd36711a7769cff624f312864 (patch) | |
tree | 90df61ea0eb7ef6aae606fd031e2509545a1efba /buffer.cpp | |
parent | f333658aa8be007128472b66291124c348e935ea (diff) | |
download | jet-5c8326ae953eee1cd36711a7769cff624f312864.tar.gz jet-5c8326ae953eee1cd36711a7769cff624f312864.zip |
Implement file reading using read syscall
Diffstat (limited to 'buffer.cpp')
-rw-r--r-- | buffer.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -4,17 +4,23 @@ struct Buffer { Buffer(const char *name) : name(name), storage(new Page()) {} - void read(const char *file) { - FILE *f = fopen(file, "r"); - char c; - Point p(storage, 0); - while ((c = fgetc(f)) != EOF) { - p.push(c); + void read_file(const char *pathname) { + int file = open(pathname, O_RDONLY); + Page *iter = storage; + int bytes_read = read(file, iter->elements, PAGE_SIZE); + iter->gap_start = bytes_read; + iter->element_count = bytes_read; + while (bytes_read == PAGE_SIZE) { + iter->next = new Page(); + iter = iter->next; + bytes_read = read(file, iter->elements, PAGE_SIZE); + iter->gap_start = bytes_read; + iter->element_count = bytes_read; } - fclose(f); + close(file); } - void write(const char *file) { + void write_file(const char *file) { FILE *f = fopen(file, "w+"); for (Point p(storage, 0); !p.at_end(); p++) { fputc(p.element(), f); |