mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
LibGUI: Add TextEditor::write_to_file(Core::Stream::File&)
This overload use the `Core::Stream` API instead of the now deprecated one `Core::File`.
This commit is contained in:
parent
72b144e9e9
commit
cf1eea27d3
2 changed files with 28 additions and 0 deletions
|
@ -1512,6 +1512,33 @@ bool TextEditor::write_to_file(Core::File& file)
|
|||
return true;
|
||||
}
|
||||
|
||||
ErrorOr<void> TextEditor::write_to_file(Core::Stream::File& file)
|
||||
{
|
||||
off_t file_size = 0;
|
||||
if (line_count() == 1 && line(0).is_empty()) {
|
||||
// Truncate to zero.
|
||||
} else {
|
||||
// Compute the final file size and ftruncate() to make writing fast.
|
||||
// FIXME: Remove this once the kernel is smart enough to do this instead.
|
||||
for (size_t i = 0; i < line_count(); ++i)
|
||||
file_size += line(i).length();
|
||||
file_size += line_count();
|
||||
}
|
||||
|
||||
TRY(file.truncate(file_size));
|
||||
|
||||
if (file_size == 0) {
|
||||
// A size 0 file doesn't need a data copy.
|
||||
} else {
|
||||
for (size_t i = 0; i < line_count(); ++i) {
|
||||
TRY(file.write(line(i).to_utf8().bytes()));
|
||||
TRY(file.write("\n"sv.bytes()));
|
||||
}
|
||||
}
|
||||
document().set_unmodified();
|
||||
return {};
|
||||
}
|
||||
|
||||
DeprecatedString TextEditor::text() const
|
||||
{
|
||||
return document().text();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue