//returns true: dir writeable
// false: not writeable
-bool isDirWriteable(string const & path)
+bool isDirWriteable(FileName const & path)
{
lyxerr[Debug::FILES] << "isDirWriteable: " << path << endl;
- string const tmpfl = tempName(path, "lyxwritetest");
+ FileName const tmpfl(tempName(path, "lyxwritetest"));
if (tmpfl.empty())
return false;
- unlink(FileName(tmpfl));
+ unlink(tmpfl);
return true;
}
+#if 0
// Uses a string of paths separated by ";"s to find a file to open.
// Can't cope with pathnames with a ';' in them. Returns full path to file.
// If path entry begins with $$LyX/, use system_lyxdir
}
return real_file;
}
+#endif
/// Returns a vector of all files in directory dir having extension ext.
-vector<string> const dirList(string const & dir, string const & ext)
+vector<FileName> const dirList(FileName const & dir, string const & ext)
{
// EXCEPTIONS FIXME. Rewrite needed when we turn on exceptions. (Lgb)
- vector<string> dirlist;
+ vector<FileName> dirlist;
- if (!(fs::exists(dir) && fs::is_directory(dir))) {
+ string const encoded_dir = dir.toFilesystemEncoding();
+ if (!(fs::exists(encoded_dir) && fs::is_directory(encoded_dir))) {
lyxerr[Debug::FILES]
<< "Directory \"" << dir
<< "\" does not exist to DirList." << endl;
extension += '.';
extension += ext;
- fs::directory_iterator dit(dir);
+ fs::directory_iterator dit(encoded_dir);
fs::directory_iterator end;
for (; dit != end; ++dit) {
string const & fil = dit->leaf();
- if (suffixIs(fil, extension)) {
- dirlist.push_back(fil);
- }
+ if (suffixIs(fil, extension))
+ dirlist.push_back(FileName::fromFilesystemEncoding(
+ encoded_dir + '/' + fil));
}
return dirlist;
}
// Returns the real name of file name in directory path, with optional
// extension ext.
FileName const fileSearch(string const & path, string const & name,
- string const & ext)
+ string const & ext, search_mode mode)
{
// if `name' is an absolute path, we ignore the setting of `path'
// Expand Environmentvariables in 'name'
if (isFileReadable(fullname))
return fullname;
if (ext.empty())
- return FileName();
- fullname = FileName(changeExtension(fullname.absFilename(), ext));
- return isFileReadable(fullname) ? fullname : FileName();
+ // We are done.
+ return mode == allow_unreadable ? fullname : FileName();
+ // Only add the extension if it is not already the extension of
+ // fullname.
+ if (getExtension(fullname.absFilename()) != ext)
+ fullname = FileName(addExtension(fullname.absFilename(), ext));
+ if (isFileReadable(fullname) || mode == allow_unreadable)
+ return fullname;
+ return FileName();
}
<< "createTmpDir: tempdir=`" << tempdir << "'\n"
<< "createTmpDir: mask=`" << mask << '\'' << endl;
- string const tmpfl = tempName(tempdir.absFilename(), mask);
+ FileName const tmpfl(tempName(tempdir, mask));
// lyx::tempName actually creates a file to make sure that it
// stays unique. So we have to delete it before we can create
// a dir with the same name. Note also that we are not thread
// safe because of the gap between unlink and mkdir. (Lgb)
- unlink(FileName(tmpfl));
+ unlink(tmpfl);
- if (tmpfl.empty() || mkdir(FileName(tmpfl), 0700)) {
+ if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
lyxerr << "LyX could not create the temporary directory '"
<< tmpfl << "'" << endl;
return FileName();
}
- return FileName(tmpfl);
+ return tmpfl;
}
} // namespace anon
-bool destroyDir(string const & tmpdir)
+bool destroyDir(FileName const & tmpdir)
{
try {
- return fs::remove_all(tmpdir) > 0;
+ return fs::remove_all(tmpdir.toFilesystemEncoding()) > 0;
} catch (fs::filesystem_error const & fe){
lyxerr << "Could not delete " << tmpdir << ". (" << fe.what() << ")" << std::endl;
return false;
{
if (!deflt.empty() && deflt.absFilename() != "/tmp") {
if (mkdir(deflt, 0777)) {
- if (isDirWriteable(deflt.absFilename())) {
+ if (isDirWriteable(deflt)) {
// deflt could not be created because it
// did exist already, so let's create our own
// dir inside deflt.
// Convert relative path into absolute path based on a basepath.
// If relpath is absolute, just use that.
// If basepath is empty, use CWD as base.
-string const makeAbsPath(string const & relPath, string const & basePath)
+FileName const makeAbsPath(string const & relPath, string const & basePath)
{
// checks for already absolute path
if (os::is_absolute_path(relPath))
- return relPath;
+ return FileName(relPath);
// Copies given paths
string tempRel = os::internal_path(relPath);
if (os::is_absolute_path(basePath))
tempBase = basePath;
else
- tempBase = addPath(getcwd(), basePath);
+ tempBase = addPath(getcwd().absFilename(), basePath);
// Handle /./ at the end of the path
while (suffixIs(tempBase, "/./"))
}
// returns absolute path
- return os::internal_path(tempBase);
+ return FileName(os::internal_path(tempBase));
}
rTemp = split(rTemp, temp, '/');
if (temp == ".")
- return getcwd() + '/' + rTemp;
+ return getcwd().absFilename() + '/' + rTemp;
if (temp == "~")
return package().home_dir() + '/' + rTemp;
if (temp == "..")
- return makeAbsPath(copy);
+ return makeAbsPath(copy).absFilename();
// Don't know how to handle this
return copy;
}
-/*
- Change extension of oldname to extension.
- Strips path off if no_path == true.
- If no extension on oldname, just appends.
- */
string const changeExtension(string const & oldname, string const & extension)
{
string::size_type const last_slash = oldname.rfind('/');
}
+string const addExtension(string const & name, string const & extension)
+{
+ if (!extension.empty() && extension[0] != '.')
+ return name + '.' + extension;
+ return name + extension;
+}
+
+
/// Return the extension of the file (not including the .)
string const getExtension(string const & name)
{
str = subst(str, home, "~");
if (str.length() <= threshold)
- return lyx::from_utf8(os::external_path(str));
+ return from_utf8(os::external_path(str));
string const prefix = ".../";
string temp;
str = head + "..." + tail;
}
- return lyx::from_utf8(os::external_path(prefix + str));
+ return from_utf8(os::external_path(prefix + str));
}
return false;
linkbuffer[nRead] = '\0'; // terminator
if (resolve)
- link = makeAbsPath(linkbuffer, onlyPath(file));
+ link = makeAbsPath(linkbuffer, onlyPath(file)).absFilename();
else
link = linkbuffer;
return true;
cmd_ret const c = runCommand(kpsecmd);
lyxerr[Debug::LATEX] << "kpse status = " << c.first << '\n'
- << "kpse result = `" << rtrim(c.second, "\n")
+ << "kpse result = `" << rtrim(c.second, "\n\r")
<< '\'' << endl;
if (c.first != -1)
- return FileName(os::internal_path(rtrim(c.second, "\n\r")));
+ return FileName(os::internal_path(rtrim(to_utf8(from_filesystem8bit(c.second)),
+ "\n\r")));
else
return FileName();
}