summaryrefslogtreecommitdiff
path: root/buffer.cpp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-14 22:19:51 -0300
committerGitHub <noreply@github.com>2020-07-14 22:19:51 -0300
commit2397212c306c63a5887ff67cc56ffae68d055d2f (patch)
tree93931a7e0fb69bd2260deaf1b843545f0a80a28f /buffer.cpp
parentbae527345c58cda7447df767561e8512dc3a9c64 (diff)
parent92ce439907a7892984267341aa0c68c2623b3ce0 (diff)
downloadjet-2397212c306c63a5887ff67cc56ffae68d055d2f.tar.gz
jet-2397212c306c63a5887ff67cc56ffae68d055d2f.zip
Merge pull request #13 from jmtomas/12
Diffstat (limited to 'buffer.cpp')
-rw-r--r--buffer.cpp24
1 files changed, 18 insertions, 6 deletions
diff --git a/buffer.cpp b/buffer.cpp
index ba3f87c..7b4c92d 100644
--- a/buffer.cpp
+++ b/buffer.cpp
@@ -4,18 +4,30 @@ struct Buffer {
Buffer(const char *name) : name(name), storage(new Page()) {}
+ void free_storage() {
+ while (storage) {
+ Page *iter = storage;
+ storage = storage->next;
+ delete iter;
+ }
+ }
+
void read_file(const char *pathname) {
+ free_storage();
int file = open(pathname, O_RDONLY);
+ storage = new Page();
+ int bytes_read = read(file, storage->elements, PAGE_SIZE);
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) {
+ while (bytes_read > 0) {
+ iter->gap_start = bytes_read;
+ iter->element_count = bytes_read;
iter->next = new Page();
+ iter->next->prev = iter;
iter = iter->next;
bytes_read = read(file, iter->elements, PAGE_SIZE);
- iter->gap_start = bytes_read;
- iter->element_count = bytes_read;
+ }
+ if (iter->element_count == 0) {
+ delete iter;
}
close(file);
}