From ae6c183475dbc3211b063ed72ad38961fe6e765e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 5 Dec 2018 01:59:36 +0100 Subject: [PATCH] Add basic "write to file" support. --- Editor/Document.cpp | 2 +- Editor/Document.h | 5 ++++- Editor/Editor.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- Editor/Editor.h | 2 ++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Editor/Document.cpp b/Editor/Document.cpp index e17be061ca..696bd9de4e 100644 --- a/Editor/Document.cpp +++ b/Editor/Document.cpp @@ -3,7 +3,7 @@ OwnPtr Document::create_from_file(const std::string& path) { - auto document = make(); + auto document = make(path); FileReader reader(path); while (reader.can_read()) { diff --git a/Editor/Document.h b/Editor/Document.h index ec67584516..7e474e484f 100644 --- a/Editor/Document.h +++ b/Editor/Document.h @@ -8,9 +8,11 @@ class Document { public: - Document() { } + explicit Document(const std::string& path) : m_path(path) { } ~Document() { } + std::string path() const { return m_path; } + Line& line(size_t index) { return *m_lines[index]; } const Line& line(size_t index) const { return *m_lines[index]; } size_t line_count() const { return m_lines.size(); } @@ -25,4 +27,5 @@ public: private: std::deque> m_lines; + std::string m_path; }; diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 0d66d5f88e..f464dfbead 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -3,15 +3,14 @@ #include "InsertOperation.h" #define _XOPEN_SOURCE_EXTENDED -#include #include +#include static int statusbar_attributes; static int ruler_attributes; Editor::Editor() { - setlocale(LC_ALL, ""); initscr(); start_color(); use_default_colors(); @@ -160,6 +159,28 @@ int Editor::exec() return 0; } +void Editor::write_to_file() +{ + FILE* fp = fopen(m_document->path().c_str(), "w"); + if (!fp) { + set_status_text("Failed to open %s for writing", m_document->path().c_str()); + return; + } + + size_t bytes = 0; + for (size_t i = 0; i < m_document->line_count(); ++i) { + fwrite(m_document->line(i).data().c_str(), sizeof(char), m_document->line(i).length(), fp); + bytes += m_document->line(i).length(); + if (i != m_document->line_count() - 1) { + fputc('\n', fp); + ++bytes; + } + } + + fclose(fp); + set_status_text("Wrote %zu bytes across %zu lines", bytes, m_document->line_count()); +} + void Editor::move_left() { if (m_cursor.column() == 0) @@ -329,6 +350,16 @@ void Editor::set_status_text(const std::string& text) m_status_text = text; } +void Editor::set_status_text(const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + char buf[128]; + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + m_status_text = buf; +} + void Editor::exec_command() { if (m_command == "q") { @@ -336,6 +367,11 @@ void Editor::exec_command() return; } + if (m_command == "w") { + write_to_file(); + return; + } + if (m_command == "about") { set_status_text("cuki editor!"); return; diff --git a/Editor/Editor.h b/Editor/Editor.h index fc3f528b54..db5530ecbc 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -30,6 +30,7 @@ public: bool is_idle() const { return m_mode == Idle; } void set_status_text(const std::string&); + void set_status_text(const char* fmt, ...); bool insert_text_at_cursor(const std::string&); bool remove_text_at_cursor(const std::string&); @@ -57,6 +58,7 @@ private: void exec_command(); void coalesce_current_line(); + void write_to_file(); OwnPtr m_document;