Support sourcing files that use crlf end of lines

This commit is contained in:
Maxime Coste 2015-06-05 13:52:56 +01:00
parent 7bff1b782e
commit be03db9a24
3 changed files with 24 additions and 9 deletions

View File

@ -947,7 +947,7 @@ const CommandDesc source_cmd = {
filename_completer,
[](const ParametersParser& parser, Context& context)
{
String file_content = read_file(parse_filename(parser[0]));
String file_content = read_file(parse_filename(parser[0]), true);
try
{
CommandManager::instance().execute(file_content, context);

View File

@ -110,22 +110,37 @@ String compact_path(StringView filename)
return filename.str();
}
String read_fd(int fd)
String read_fd(int fd, bool text)
{
String content;
char buf[256];
constexpr size_t bufsize = 256;
char buf[bufsize];
while (true)
{
ssize_t size = read(fd, buf, 256);
ssize_t size = read(fd, buf, bufsize);
if (size == -1 or size == 0)
break;
content += StringView(buf, buf + size);
if (text)
{
ssize_t beg = 0;
for (ssize_t pos = 0; pos < size; ++pos)
{
if (buf[pos] == '\r')
{
content += StringView{buf + beg, buf + pos};
beg = pos + 1;
}
}
content += StringView{buf + beg, buf + size};
}
else
content += StringView{buf, buf + size};
}
return content;
}
String read_file(StringView filename)
String read_file(StringView filename, bool text)
{
int fd = open(parse_filename(filename).c_str(), O_RDONLY);
if (fd == -1)
@ -137,7 +152,7 @@ String read_file(StringView filename)
}
auto close_fd = on_scope_end([fd]{ close(fd); });
return read_fd(fd);
return read_fd(fd, text);
}
Buffer* create_buffer_from_file(StringView filename)

View File

@ -37,8 +37,8 @@ std::pair<StringView, StringView> split_path(StringView path);
String get_kak_binary_path();
String read_fd(int fd);
String read_file(StringView filename);
String read_fd(int fd, bool text = false);
String read_file(StringView filename, bool text = false);
Buffer* create_buffer_from_file(StringView filename);