use parse_filename when opening files or completing filenames so that ~ and env vars are handled

This commit is contained in:
Maxime Coste 2012-09-12 19:42:12 +02:00
parent a7e807f773
commit a467d73115
2 changed files with 7 additions and 5 deletions

View File

@ -2,6 +2,7 @@
#include "buffer_manager.hh" #include "buffer_manager.hh"
#include "utils.hh" #include "utils.hh"
#include "file.hh"
#include <dirent.h> #include <dirent.h>
#include <algorithm> #include <algorithm>
@ -13,7 +14,7 @@ CandidateList complete_filename(const Context& context,
const String& prefix, const String& prefix,
CharCount cursor_pos) CharCount cursor_pos)
{ {
String real_prefix = prefix.substr(0, cursor_pos); String real_prefix = parse_filename(prefix.substr(0, cursor_pos));
String dirname = "./"; String dirname = "./";
String dirprefix; String dirprefix;
String fileprefix = real_prefix; String fileprefix = real_prefix;

View File

@ -21,7 +21,7 @@ bool isidentifier(char c)
String parse_filename(const String& filename) String parse_filename(const String& filename)
{ {
if (filename.length() > 2 and filename[0] == '~' and filename[1] == '/') if (filename.length() >= 2 and filename[0] == '~' and filename[1] == '/')
return parse_filename("$HOME/" + filename.substr(2)); return parse_filename("$HOME/" + filename.substr(2));
CharCount pos = 0; CharCount pos = 0;
@ -50,7 +50,7 @@ String parse_filename(const String& filename)
String read_file(const String& filename) String read_file(const String& filename)
{ {
int fd = open(filename.c_str(), O_RDONLY); int fd = open(parse_filename(filename).c_str(), O_RDONLY);
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
@ -75,7 +75,7 @@ String read_file(const String& filename)
Buffer* create_buffer_from_file(const String& filename) Buffer* create_buffer_from_file(const String& filename)
{ {
int fd = open(filename.c_str(), O_RDONLY); int fd = open(parse_filename(filename).c_str(), O_RDONLY);
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
@ -165,7 +165,8 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename)
eolformat = "\n"; eolformat = "\n";
auto eoldata = eolformat.data(); auto eoldata = eolformat.data();
int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); int fd = open(parse_filename(filename).c_str(),
O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1) if (fd == -1)
throw file_access_error(filename, strerror(errno)); throw file_access_error(filename, strerror(errno));