From de19aeb03547773f2ba7d39d27a5ad4b1222d15f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 27 Nov 2011 12:56:38 +0000 Subject: [PATCH] File: extract read_file function which returns file content in a string --- src/file.cc | 12 +++++++++--- src/file.hh | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/file.cc b/src/file.cc index 4da683d7..f6705203 100644 --- a/src/file.cc +++ b/src/file.cc @@ -13,9 +13,9 @@ namespace Kakoune { -Buffer* create_buffer_from_file(const std::string& filename) -{ - int fd = open(filename.c_str(), O_RDONLY); +std::string read_file(const std::string& filename) +{ + int fd = open(filename.c_str(), O_RDONLY); if (fd == -1) { if (errno == ENOENT) @@ -35,6 +35,12 @@ Buffer* create_buffer_from_file(const std::string& filename) content += std::string(buf, size); } close(fd); + return content; +} + +Buffer* create_buffer_from_file(const std::string& filename) +{ + std::string content = read_file(filename); if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) BufferManager::instance().delete_buffer(buffer); diff --git a/src/file.hh b/src/file.hh index 54bb654e..b6b7fc6a 100644 --- a/src/file.hh +++ b/src/file.hh @@ -23,6 +23,7 @@ struct file_not_found : file_access_error }; class Buffer; +std::string read_file(const std::string& filename); Buffer* create_buffer_from_file(const std::string& filename); void write_buffer_to_file(const Buffer& buffer, const std::string& filename);