diff options
-rw-r--r-- | buffer.cpp | 24 |
1 files changed, 18 insertions, 6 deletions
@@ -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); } |