File: extract read_file function which returns file content in a string

This commit is contained in:
Maxime Coste 2011-11-27 12:56:38 +00:00
parent a2dfcc232c
commit de19aeb035
2 changed files with 10 additions and 3 deletions

View File

@ -13,9 +13,9 @@
namespace Kakoune namespace Kakoune
{ {
Buffer* create_buffer_from_file(const std::string& filename) std::string read_file(const std::string& filename)
{ {
int fd = open(filename.c_str(), O_RDONLY); int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
@ -35,6 +35,12 @@ Buffer* create_buffer_from_file(const std::string& filename)
content += std::string(buf, size); content += std::string(buf, size);
} }
close(fd); 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)) if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
BufferManager::instance().delete_buffer(buffer); BufferManager::instance().delete_buffer(buffer);

View File

@ -23,6 +23,7 @@ struct file_not_found : file_access_error
}; };
class Buffer; class Buffer;
std::string read_file(const std::string& filename);
Buffer* create_buffer_from_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); void write_buffer_to_file(const Buffer& buffer, const std::string& filename);