]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.cpp
add debug function which prints the callstack to stderr, disabled by default
[lyx.git] / src / support / filetools.cpp
index 5a9a22b8b7f3dafe42cc1ab76eae690f33aa06c6..d31926556dea3b826f83b5fd41a83af1418b5bea 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <config.h>
 
+#include "LyXRC.h"
+
 #include "support/filetools.h"
 
 #include "support/debug.h"
@@ -36,7 +38,7 @@
 #include <QDir>
 
 #include "support/lassert.h"
-#include <boost/regex.hpp>
+#include "support/regex.h"
 
 #include <fcntl.h>
 
 #include <fstream>
 #include <sstream>
 
+#if defined (_WIN32)
+#include <io.h>
+#include <windows.h>
+#endif
+
 using namespace std;
 
 #define USE_QPROCESS
@@ -55,21 +62,28 @@ using namespace std;
 namespace lyx {
 namespace support {
 
-bool isLyXFilename(string const & filename)
+bool isLyXFileName(string const & filename)
 {
        return suffixIs(ascii_lowercase(filename), ".lyx");
 }
 
 
-bool isSGMLFilename(string const & filename)
+bool isSGMLFileName(string const & filename)
 {
        return suffixIs(ascii_lowercase(filename), ".sgml");
 }
 
 
-bool isValidLaTeXFilename(string const & filename)
+bool isValidLaTeXFileName(string const & filename)
+{
+       string const invalid_chars("#%\"");
+       return filename.find_first_of(invalid_chars) == string::npos;
+}
+
+
+bool isValidDVIFileName(string const & filename)
 {
-       string const invalid_chars("#$%{}()[]\"^");
+       string const invalid_chars("${}()[]^");
        return filename.find_first_of(invalid_chars) == string::npos;
 }
 
@@ -256,7 +270,7 @@ FileName const i18nLibFileSearch(string const & dir, string const & name,
           each po file is able to tell us its name. (JMarc)
        */
 
-       string lang = to_ascii(_("[[Replace with the code of your language]]"));
+       string lang = to_ascii(_(languageTestString()));
        string const language = getEnv("LANGUAGE");
        if (!lang.empty() && !language.empty())
                lang = language;
@@ -295,6 +309,21 @@ FileName const i18nLibFileSearch(string const & dir, string const & name,
 }
 
 
+FileName const imageLibFileSearch(string & dir, string const & name,
+                 string const & ext)
+{
+       if (!lyx::lyxrc.icon_set.empty()) {
+               string const imagedir = addPath(dir, lyx::lyxrc.icon_set);
+               FileName const fn = libFileSearch(imagedir, name, ext);
+               if (fn.exists()) {
+                       dir = imagedir;
+                       return fn;
+               }
+       }
+       return libFileSearch(dir, name, ext);
+}
+
+
 string const libScriptSearch(string const & command_in, quote_style style)
 {
        static string const token_scriptpath = "$$s/";
@@ -350,7 +379,7 @@ FileName const createLyXTmpDir(FileName const & deflt)
        if (deflt.empty() || deflt == package().system_temp_dir())
                return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
 
-       if (deflt.createDirectory(0777)) 
+       if (deflt.createDirectory(0777))
                return deflt;
 
        if (deflt.isDirWritable()) {
@@ -529,29 +558,20 @@ string const replaceEnvironmentPath(string const & path)
        static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
 
        // $VAR is defined as:
-       // $\{[A-Za-z_][A-Za-z_0-9]*\}
+       // $[A-Za-z_][A-Za-z_0-9]*
        static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
 
-       static boost::regex envvar_br_re("(.*)" + envvar_br + "(.*)");
-       static boost::regex envvar_re("(.*)" + envvar + "(.*)");
-       boost::smatch what;
-       string result;
-       string remaining = path;
+       static regex envvar_br_re("(.*)" + envvar_br + "(.*)");
+       static regex envvar_re("(.*)" + envvar + "(.*)");
+       string result = path;
        while (1) {
-               regex_match(remaining, what, envvar_br_re);
-               if (!what[0].matched) {
-                       regex_match(remaining, what, envvar_re);
-                       if (!what[0].matched) {
-                               result += remaining;
+               smatch what;
+               if (!regex_match(result, what, envvar_br_re)) {
+                       if (!regex_match(result, what, envvar_re))
                                break;
-                       }
                }
                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);
+               result = what.str(1) + env_var + what.str(3);
        }
        return result;
 }
@@ -736,9 +756,9 @@ docstring const makeDisplayPath(string const & path, unsigned int threshold)
 }
 
 
+#ifdef HAVE_READLINK
 bool readLink(FileName const & file, FileName & link)
 {
-#ifdef HAVE_READLINK
        char linkbuffer[PATH_MAX + 1];
        string const encoded = file.toFilesystemEncoding();
        int const nRead = ::readlink(encoded.c_str(),
@@ -748,10 +768,13 @@ bool readLink(FileName const & file, FileName & link)
        linkbuffer[nRead] = '\0'; // terminator
        link = makeAbsPath(linkbuffer, onlyPath(file.absFileName()));
        return true;
+}
 #else
+bool readLink(FileName const &, FileName &)
+{
        return false;
-#endif
 }
+#endif
 
 
 cmd_ret const runCommand(string const & cmd)
@@ -766,7 +789,39 @@ cmd_ret const runCommand(string const & cmd)
        // pstream (process stream), with the
        // variants ipstream, opstream
 
-#if defined (HAVE_POPEN)
+#if defined (_WIN32)
+       int fno;
+       STARTUPINFO startup;
+       PROCESS_INFORMATION process;
+       SECURITY_ATTRIBUTES security;
+       HANDLE in, out;
+       FILE * inf = 0;
+
+       security.nLength = sizeof(SECURITY_ATTRIBUTES);
+       security.bInheritHandle = TRUE;
+       security.lpSecurityDescriptor = NULL;
+
+       if (CreatePipe(&in, &out, &security, 0)) {
+               memset(&startup, 0, sizeof(STARTUPINFO));
+               memset(&process, 0, sizeof(PROCESS_INFORMATION));
+
+               startup.cb = sizeof(STARTUPINFO);
+               startup.dwFlags = STARTF_USESTDHANDLES;
+
+               startup.hStdError = GetStdHandle(STD_ERROR_HANDLE);
+               startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
+               startup.hStdOutput = out;
+
+               if (CreateProcess(0, (LPTSTR)cmd.c_str(), &security, &security,
+                       TRUE, CREATE_NO_WINDOW, 0, 0, &startup, &process)) {
+
+                       CloseHandle(process.hThread);
+                       fno = _open_osfhandle((long)in, _O_RDONLY);
+                       CloseHandle(out);
+                       inf = _fdopen(fno, "r");
+               }
+       }
+#elif defined (HAVE_POPEN)
        FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
 #elif defined (HAVE__POPEN)
        FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
@@ -774,7 +829,7 @@ cmd_ret const runCommand(string const & cmd)
 #error No popen() function.
 #endif
 
-       // (Claus Hentschel) Check if popen was succesful ;-)
+       // (Claus Hentschel) Check if popen was successful ;-)
        if (!inf) {
                lyxerr << "RunCommand:: could not start child process" << endl;
                return make_pair(-1, string());
@@ -787,7 +842,11 @@ cmd_ret const runCommand(string const & cmd)
                c = fgetc(inf);
        }
 
-#if defined (HAVE_PCLOSE)
+#if defined (_WIN32)
+       WaitForSingleObject(process.hProcess, INFINITE);
+       CloseHandle(process.hProcess);
+       int const pret = fclose(inf);
+#elif defined (HAVE_PCLOSE)
        int const pret = pclose(inf);
 #elif defined (HAVE__PCLOSE)
        int const pret = _pclose(inf);
@@ -880,13 +939,13 @@ string const readBB_from_PSFile(FileName const & file)
                return string();
        }
 
-       static boost::regex bbox_re(
+       static lyx::regex bbox_re(
                "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
        ifstream is(file_.toFilesystemEncoding().c_str());
        while (is) {
                string s;
                getline(is,s);
-               boost::smatch what;
+               lyx::smatch what;
                if (regex_match(s, what, bbox_re)) {
                        // Our callers expect the tokens in the string
                        // separated by single spaces.
@@ -926,5 +985,32 @@ int compare_timestamps(FileName const & file1, FileName const & file2)
 }
 
 
+bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfuns)
+{
+       FileName const script = libFileSearch("scripts", "prefs2prefs.py");
+       if (script.empty()) {
+               LYXERR0("Could not find bind file conversion "
+                               "script prefs2prefs.py.");
+               return false;
+       }
+
+       ostringstream command;
+       command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
+         << ' ' << (lfuns ? "-l" : "-p") << ' '
+               << quoteName(filename.toFilesystemEncoding())
+               << ' ' << quoteName(tempfile.toFilesystemEncoding());
+       string const command_str = command.str();
+
+       LYXERR(Debug::FILES, "Running `" << command_str << '\'');
+
+       cmd_ret const ret = runCommand(command_str);
+       if (ret.first != 0) {
+               LYXERR0("Could not run file conversion script prefs2prefs.py.");
+               return false;
+       }
+       return true;
+}
+
+
 } //namespace support
 } // namespace lyx