]> git.lyx.org Git - features.git/blobdiff - src/support/filetools.cpp
Remove redundant declarations reported by GCC with -Wredundant-decls option
[features.git] / src / support / filetools.cpp
index 6d6674dce915773081eac0e445a050d12b808699..41e508b47f9a54dbde32d604b2b3c4099b5a73bf 100644 (file)
@@ -42,7 +42,6 @@
 #include <QDir>
 
 #include "support/lassert.h"
-#include "support/regex.h"
 
 #include <fcntl.h>
 #ifdef HAVE_MAGIC_H
@@ -59,6 +58,7 @@
 
 #include <utility>
 #include <fstream>
+#include <regex>
 #include <sstream>
 #include <vector>
 
@@ -335,12 +335,16 @@ FileName const fileSearch(string const & path, string const & name,
 //   2) build_lyxdir (if not empty)
 //   3) system_lyxdir
 FileName const libFileSearch(string const & dir, string const & name,
-                          string const & ext, search_mode mode)
+                          string const & ext, search_mode mode,
+                          bool const only_global)
 {
-       FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
-                                    name, ext, mode);
-       if (!fullname.empty())
-               return fullname;
+       FileName fullname;
+       if (!only_global) {
+               fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
+                                            name, ext, mode);
+               if (!fullname.empty())
+                       return fullname;
+       }
 
        if (!package().build_support().empty())
                fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
@@ -442,7 +446,7 @@ string const commandPrep(string const & command_in)
 }
 
 
-FileName const tempFileName(FileName tempdir, string const & mask, bool const dir)
+FileName const tempFileName(FileName const & tempdir, string const & mask, bool const dir)
 {
        return tempFileName(TempFile(tempdir, mask).name(), dir);
 }
@@ -654,6 +658,19 @@ string const addName(string const & path, string const & fname)
 }
 
 
+string const addPathName(std::string const & path, std::string const & fname)
+{
+       string const pathpart = onlyPath(fname);
+       string const namepart = onlyFileName(fname);
+       string newpath = path;
+       if (!pathpart.empty())
+               newpath = addPath(newpath, pathpart);
+       if (!namepart.empty())
+               newpath = addName(newpath, namepart);
+       return newpath;
+}
+
+
 // Strips path from filename
 string const onlyFileName(string const & fname)
 {
@@ -670,8 +687,12 @@ string const onlyFileName(string const & fname)
 
 
 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
+// If VAR does not exist, ${VAR} and $VAR are left as is in the string.
 string const replaceEnvironmentPath(string const & path)
 {
+       if (!contains(path, '$'))
+               return path;
+
        // ${VAR} is defined as
        // $\{[A-Za-z_][A-Za-z_0-9]*\}
        static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
@@ -689,14 +710,23 @@ string const replaceEnvironmentPath(string const & path)
                string result = path;
                while (1) {
                        smatch what;
+                       bool brackets = true;
                        if (!regex_match(result, what, envvar_br_re)) {
+                               brackets = false;
                                if (!regex_match(result, what, envvar_re))
                                        break;
                        }
                        string env_var = getEnv(what.str(2));
+                       if (env_var.empty()) {
+                               // temporarily use alert/bell (0x07) in place of $
+                               if (brackets)
+                                       env_var = "\a{" + what.str(2) + '}';
+                               else
+                                       env_var = "\a" + what.str(2);
+                       }
                        result = what.str(1) + env_var + what.str(3);
                }
-               return result;
+               return subst(result, '\a', '$');
        } catch (exception const & e) {
                LYXERR0("Something is very wrong: " << e.what());
                return path;
@@ -1134,7 +1164,7 @@ cmd_ret const runCommand(string const & cmd)
 FileName const findtexfile(string const & fil, string const & /*format*/,
                                                   bool const onlykpse)
 {
-       /* There is no problem to extend this function too use other
+       /* There is no problem to extend this function to use other
           methods to look for files. It could be setup to look
           in environment paths and also if wanted as a last resort
           to a recursive find. One of the easier extensions would