diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-18 14:38:30 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-18 14:38:30 +0100 |
commit | 9ad076178a4bb13436e2edf90e62a26fb2cf32ea (patch) | |
tree | 478cedcbcc44e7e98348d67787216c25a318b3dc /Applications/TextEditor | |
parent | 8e3d0a23d555b2464aba5a07a90092535cec19e1 (diff) | |
download | serenity-9ad076178a4bb13436e2edf90e62a26fb2cf32ea.zip |
GIODevice: Add a read_all() that returns a ByteBuffer with all we can read.
Use this to implement file opening in TextEditor.
Diffstat (limited to 'Applications/TextEditor')
-rw-r--r-- | Applications/TextEditor/main.cpp | 27 |
1 files changed, 5 insertions, 22 deletions
diff --git a/Applications/TextEditor/main.cpp b/Applications/TextEditor/main.cpp index 1c3e791f48..4df7073241 100644 --- a/Applications/TextEditor/main.cpp +++ b/Applications/TextEditor/main.cpp @@ -8,6 +8,7 @@ #include <LibGUI/GTextEditor.h> #include <LibGUI/GAction.h> #include <LibGUI/GFontDatabase.h> +#include <LibGUI/GFile.h> #include <AK/StringBuilder.h> #include <unistd.h> #include <stdio.h> @@ -34,31 +35,13 @@ int main(int argc, char** argv) String path = "/tmp/TextEditor.save.txt"; if (argc >= 2) { path = argv[1]; - StringBuilder builder; - int fd = open(path.characters(), O_RDONLY); - if (fd < 0) { - perror("open"); - return 1; - } - for (;;) { - char buffer[BUFSIZ]; - ssize_t nread = read(fd, buffer, sizeof(buffer)); - if (nread < 0) { - perror("read"); - return 1; - } - if (nread == 0) - break; - builder.append(buffer, nread); - } + GFile file(path); - int rc = close(fd); - if (rc < 0) { - perror("close"); + if (!file.open(GIODevice::ReadOnly)) { + fprintf(stderr, "Opening %s: %s\n", path.characters(), file.error_string()); return 1; } - - text_editor->set_text(builder.to_string()); + text_editor->set_text(String::from_byte_buffer(file.read_all())); } auto new_action = GAction::create("New document", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/new.rgb", { 16, 16 }), [] (const GAction&) { |