summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--buffer.cpp22
-rw-r--r--jet.cpp4
2 files changed, 17 insertions, 9 deletions
diff --git a/buffer.cpp b/buffer.cpp
index d1762d0..ff5ae60 100644
--- a/buffer.cpp
+++ b/buffer.cpp
@@ -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);
diff --git a/jet.cpp b/jet.cpp
index c4e997c..1a2e9c9 100644
--- a/jet.cpp
+++ b/jet.cpp
@@ -3,6 +3,8 @@
#include <string.h>
#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
@@ -48,7 +50,7 @@ void parse_command(char *command, Client *client) {
int main() {
Buffer scratch("scratch");
- scratch.read("LICENSE");
+ scratch.read_file("LICENSE");
int listener = create_listener();