]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
fix compiler warnings about unused parameter
[lyx.git] / src / support / filetools.C
index 825c508a364f871b801d956ece6dde1ac7a968c4..7a8448df49dda1c3233fc3685ea566999977ab1a 100644 (file)
@@ -165,20 +165,21 @@ bool isFileReadable(FileName const & filename)
 
 //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
@@ -213,15 +214,17 @@ FileName const fileOpenSearch(string const & path, string const & name,
        }
        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;
@@ -233,13 +236,13 @@ vector<string> const dirList(string const & dir, string const & ext)
                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;
 }
@@ -248,7 +251,7 @@ vector<string> const dirList(string const & dir, string const & ext)
 // 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'
@@ -258,9 +261,15 @@ FileName const fileSearch(string const & path, string const & 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();
 }
 
 
@@ -367,29 +376,29 @@ FileName const createTmpDir(FileName const & tempdir, string const & mask)
                << "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;
@@ -420,7 +429,7 @@ FileName const createLyXTmpDir(FileName const & deflt)
 {
        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.
@@ -461,11 +470,11 @@ string const onlyPath(string const & filename)
 // 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);
@@ -477,7 +486,7 @@ string const makeAbsPath(string const & relPath, string const & basePath)
        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, "/./"))
@@ -515,7 +524,7 @@ string const makeAbsPath(string const & relPath, string const & basePath)
        }
 
        // returns absolute path
-       return os::internal_path(tempBase);
+       return FileName(os::internal_path(tempBase));
 }
 
 
@@ -575,13 +584,13 @@ string const expandPath(string const & path)
        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;
@@ -719,11 +728,6 @@ string const addPath(string const & path, string const & path_2)
 }
 
 
-/*
- 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('/');
@@ -748,6 +752,14 @@ string const removeExtension(string const & name)
 }
 
 
+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)
 {
@@ -987,7 +999,7 @@ docstring const makeDisplayPath(string const & path, unsigned int threshold)
                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;
@@ -1008,7 +1020,7 @@ docstring const makeDisplayPath(string const & path, unsigned int threshold)
                str = head + "..." + tail;
        }
 
-       return lyx::from_utf8(os::external_path(prefix + str));
+       return from_utf8(os::external_path(prefix + str));
 }
 
 
@@ -1023,7 +1035,7 @@ bool readLink(string const & file, string & link, bool resolve)
                return false;
        linkbuffer[nRead] = '\0'; // terminator
        if (resolve)
-               link = makeAbsPath(linkbuffer, onlyPath(file));
+               link = makeAbsPath(linkbuffer, onlyPath(file)).absFilename();
        else
                link = linkbuffer;
        return true;
@@ -1123,10 +1135,11 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
        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();
 }