From 5c8326ae953eee1cd36711a7769cff624f312864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Fri, 10 Jul 2020 21:24:20 -0300 Subject: Implement file reading using read syscall --- buffer.cpp | 22 ++++++++++++++-------- jet.cpp | 4 +++- 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 #include +#include +#include #include #include #include @@ -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(); -- cgit v1.2.3 From c4c5be557abb30166dbc49770a0151671ab1bf07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Fri, 10 Jul 2020 21:54:04 -0300 Subject: Implement file writing using the write syscall --- buffer.cpp | 13 ++++++++----- jet.cpp | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/buffer.cpp b/buffer.cpp index ff5ae60..ba3f87c 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -20,11 +20,14 @@ struct Buffer { close(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); + 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); } }; diff --git a/jet.cpp b/jet.cpp index 1a2e9c9..3d45c75 100644 --- a/jet.cpp +++ b/jet.cpp @@ -64,7 +64,7 @@ int main() { epoll_event events[MAX_EVENTS]; Client *clients[1024] = {}; - for (;;) { + while (true) { int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); for (int i = 0; i < nfds; i++) { -- cgit v1.2.3