summaryrefslogtreecommitdiff
path: root/buffer.cpp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-10 21:59:52 -0300
committerGitHub <noreply@github.com>2020-07-10 21:59:52 -0300
commit7908b37141271f671a31f5dc7dd6341a037a4b27 (patch)
treea642972e425533e91e3d46eb6fb71d2bc276f076 /buffer.cpp
parentf333658aa8be007128472b66291124c348e935ea (diff)
parentc4c5be557abb30166dbc49770a0151671ab1bf07 (diff)
downloadjet-7908b37141271f671a31f5dc7dd6341a037a4b27.tar.gz
jet-7908b37141271f671a31f5dc7dd6341a037a4b27.zip
Merge pull request #7 from jmtomas/6
6
Diffstat (limited to 'buffer.cpp')
-rw-r--r--buffer.cpp33
1 files changed, 21 insertions, 12 deletions
diff --git a/buffer.cpp b/buffer.cpp
index d1762d0..ba3f87c 100644
--- a/buffer.cpp
+++ b/buffer.cpp
@@ -4,21 +4,30 @@ 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) {
- FILE *f = fopen(file, "w+");
- for (Point p(storage, 0); !p.at_end(); p++) {
- fputc(p.element(), f);
+ void write_file(const char *pathname) {
+ int file = open(pathname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ Page *iter = storage;
+ while (iter) {
+ write(file, iter->elements, iter->gap_start);
+ write(file, iter->elements + iter->gap_end, PAGE_SIZE - iter->gap_end);
+ iter = iter->next;
}
- fclose(f);
+ close(file);
}
};