1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-07 05:57:35 +00:00

GTextEditor: Add write_to_file(String path) :^)

This commit is contained in:
Andreas Kling 2019-03-07 17:06:11 +01:00
parent a5bc20c733
commit 187d7cb400
3 changed files with 40 additions and 2 deletions

View file

@ -3,6 +3,9 @@
#include <LibGUI/GFontDatabase.h>
#include <SharedGraphics/Painter.h>
#include <Kernel/KeyCode.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
GTextEditor::GTextEditor(GWidget* parent)
: GWidget(parent)
@ -429,3 +432,35 @@ void GTextEditor::Line::truncate(int length)
m_text.resize(length + 1);
m_text.last() = 0;
}
bool GTextEditor::write_to_file(const String& path)
{
int fd = open(path.characters(), O_WRONLY | O_CREAT, 0666);
if (fd < 0) {
perror("open");
return false;
}
for (int i = 0; i < m_lines.size(); ++i) {
auto& line = *m_lines[i];
if (line.length()) {
ssize_t nwritten = write(fd, line.characters(), line.length());
if (nwritten < 0) {
perror("write");
close(fd);
return false;
}
}
if (i != m_lines.size() - 1) {
char ch = '\n';
ssize_t nwritten = write(fd, &ch, 1);
if (nwritten != 1) {
perror("write");
close(fd);
return false;
}
}
}
close(fd);
return true;
}