]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.cpp
add onoff support for "inset-modify changetype xxx" in include inset
[lyx.git] / src / support / filetools.cpp
index a7cf272e6ba3cce252bd6b368cbf1c7c18e8774b..810694ec785c74ea8ce2fb7bcfea02bfc63790ba 100644 (file)
@@ -8,11 +8,11 @@
  * \author Ivan Schreter
  * \author Dirk Niggemann
  * \author Asger Alstrup
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
  * \author John Levon
- * \author Herbert Voß
+ * \author Herbert Voß
  *
  * Full author contact details are available in file CREDITS.
  *
@@ -23,7 +23,6 @@
 
 #include "support/filetools.h"
 
-#include "support/convert.h"
 #include "support/debug.h"
 #include "support/environment.h"
 #include "support/gettext.h"
@@ -36,7 +35,7 @@
 
 #include <QDir>
 
-#include <boost/assert.hpp>
+#include "support/lassert.h"
 #include <boost/regex.hpp>
 
 #include <fcntl.h>
@@ -51,6 +50,8 @@
 
 using namespace std;
 
+#define USE_QPROCESS
+
 namespace lyx {
 namespace support {
 
@@ -136,9 +137,13 @@ string const quoteName(string const & name, quote_style style)
                // filenames to child processes if possible. We store them in
                // a python script instead, where we don't have these
                // limitations.
+#ifndef USE_QPROCESS
                return (os::shell() == os::UNIX) ?
                        '\'' + name + '\'':
                        '"' + name + '"';
+#else
+               return '"' + name + '"';
+#endif
        case quote_python:
                return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
                     + "\"";
@@ -200,12 +205,12 @@ FileName const fileSearch(string const & path, string const & name,
                return fullname;
        if (ext.empty())
                // We are done.
-               return mode == allow_unreadable ? fullname : FileName();
+               return mode == may_not_exist ? 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 (fullname.isReadableFile() || mode == allow_unreadable)
+       if (fullname.isReadableFile() || mode == may_not_exist)
                return fullname;
        return FileName();
 }
@@ -321,46 +326,22 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask)
        LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
                << "createTmpDir:    mask=`" << mask << '\'');
 
-       FileName const tmpfl = FileName::tempName(tempdir.absFilename()
-               + "/" + mask);
-       // FileName::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)
-       tmpfl.removeFile();
+       FileName const tmpfl = FileName::tempName(tempdir, mask);
 
        if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
-               lyxerr << "LyX could not create the temporary directory '"
-                      << tmpfl << "'" << endl;
+               LYXERR0("LyX could not create temporary directory in " << tempdir
+                       << "'");
                return FileName();
        }
 
        return tmpfl;
 }
 
-string const createBufferTmpDir()
-{
-       static int count;
-       // We are in our own directory.  Why bother to mangle name?
-       // In fact I wrote this code to circumvent a problematic behaviour
-       // (bug?) of EMX mkstemp().
-       string const tmpfl =
-               package().temp_dir().absFilename() + "/lyx_tmpbuf" +
-               convert<string>(count++);
-
-       if (!FileName(tmpfl).createDirectory(0777)) {
-               lyxerr << "LyX could not create the temporary directory '"
-                      << tmpfl << "'" << endl;
-               return string();
-       }
-       return tmpfl;
-}
-
 
 FileName const createLyXTmpDir(FileName const & deflt)
 {
-       if (deflt.empty() || deflt.absFilename() == "/tmp")
-               return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
+       if (deflt.empty() || deflt == package().system_temp_dir())
+               return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
 
        if (deflt.createDirectory(0777)) 
                return deflt;
@@ -372,7 +353,7 @@ FileName const createLyXTmpDir(FileName const & deflt)
                return createTmpDir(deflt, "lyx_tmpdir");
        } else {
                // some other error occured.
-               return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
+               return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
        }
 }
 
@@ -393,12 +374,15 @@ 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.
+// Note that basePath can be a relative path, in the sense that it may
+// not begin with "/" (e.g.), but it should NOT contain such constructs
+// as "/../".
+// FIXME It might be nice if the code didn't simply assume that.
 FileName const makeAbsPath(string const & relPath, string const & basePath)
 {
-       FileName relative_path(relPath);
        // checks for already absolute path
-       if (relative_path.isAbsolute())
-               return relative_path;
+       if (FileName::isAbsolute(relPath))
+               return FileName(relPath);
 
        // Copies given paths
        string tempRel = os::internal_path(relPath);
@@ -407,8 +391,7 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
 
        string tempBase;
 
-       FileName base_path(basePath);
-       if (base_path.isAbsolute())
+       if (FileName::isAbsolute(basePath))
                tempBase = basePath;
        else
                tempBase = addPath(FileName::getcwd().absFilename(), basePath);
@@ -421,6 +404,15 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
        string rTemp = tempRel;
        string temp;
 
+       // Check for a leading "~"
+       // Split by first /
+       rTemp = split(rTemp, temp, '/');
+       if (temp == "~") {
+               tempBase = package().home_dir().absFilename();
+               tempRel = rTemp;
+       }
+
+       rTemp = tempRel;
        while (!rTemp.empty()) {
                // Split by next /
                rTemp = split(rTemp, temp, '/');
@@ -428,15 +420,22 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
                if (temp == ".") continue;
                if (temp == "..") {
                        // Remove one level of TempBase
-                       string::difference_type i = tempBase.length() - 2;
-                       if (i < 0)
-                               i = 0;
+                       if (tempBase.length() <= 1) {
+                               //this is supposed to be an absolute path, so...
+                               tempBase = "/";
+                               continue;
+                       }
+                       //erase a trailing slash if there is one
+                       if (suffixIs(tempBase, "/"))
+                               tempBase.erase(tempBase.length() - 1, string::npos);
+
+                       string::size_type i = tempBase.length() - 1;
                        while (i > 0 && tempBase[i] != '/')
                                --i;
                        if (i > 0)
                                tempBase.erase(i, string::npos);
                        else
-                               tempBase += '/';
+                               tempBase = '/';
                } else if (temp.empty() && !rTemp.empty()) {
                                tempBase = os::current_root() + rTemp;
                                rTemp.erase();
@@ -492,8 +491,7 @@ string const expandPath(string const & path)
 {
        // checks for already absolute path
        string rTemp = replaceEnvironmentPath(path);
-       FileName abs_path(rTemp);
-       if (abs_path.isAbsolute())
+       if (FileName::isAbsolute(rTemp))
                return rTemp;
 
        string temp;
@@ -530,16 +528,23 @@ string const replaceEnvironmentPath(string const & path)
        static boost::regex envvar_br_re("(.*)" + envvar_br + "(.*)");
        static boost::regex envvar_re("(.*)" + envvar + "(.*)");
        boost::smatch what;
-
-       string result = path;
+       string result;
+       string remaining = path;
        while (1) {
-               regex_match(result, what, envvar_br_re);
+               regex_match(remaining, what, envvar_br_re);
                if (!what[0].matched) {
-                       regex_match(result, what, envvar_re);
-                       if (!what[0].matched)
+                       regex_match(remaining, what, envvar_re);
+                       if (!what[0].matched) {
+                               result += remaining;
                                break;
+                       }
                }
-               result = what.str(1) + getEnv(what.str(2)) + what.str(3);
+               string env_var = getEnv(what.str(2));
+               if (!env_var.empty())
+                       result += what.str(1) + env_var;
+               else
+                       result += what.str(1) + "$" + what.str(2);
+               remaining = what.str(3);
        }
        return result;
 }
@@ -663,7 +668,7 @@ string const unzippedFileName(string const & zipped_file)
        string const ext = getExtension(zipped_file);
        if (ext == "gz" || ext == "z" || ext == "Z")
                return changeExtension(zipped_file, string());
-       return "unzipped_" + zipped_file;
+       return onlyPath(zipped_file) + "unzipped_" + onlyFilename(zipped_file);
 }
 
 
@@ -808,7 +813,7 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
        if (absfile.exists())
                return absfile;
 
-       // No we try to find it using kpsewhich.
+       // Now we try to find it using kpsewhich.
        // It seems from the kpsewhich manual page that it is safe to use
        // kpsewhich without --format: "When the --format option is not
        // given, the search path used when looking for a file is inferred
@@ -841,18 +846,6 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
 }
 
 
-void removeAutosaveFile(string const & filename)
-{
-       string a = onlyPath(filename);
-       a += '#';
-       a += onlyFilename(filename);
-       a += '#';
-       FileName const autosave(a);
-       if (autosave.exists())
-               autosave.removeFile();
-}
-
-
 void readBB_lyxerrMessage(FileName const & file, bool & zipped,
        string const & message)
 {