3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
8 * \author Ivan Schreter
9 * \author Dirk Niggemann
10 * \author Asger Alstrup
11 * \author Lars Gullik Bjønnes
12 * \author Jean-Marc Lasgouttes
13 * \author Angus Leeming
17 * Full author contact details are available in file CREDITS.
19 * General path-mangling functions
26 #include "support/filetools.h"
28 #include "support/debug.h"
29 #include "support/environment.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/os.h"
33 #include "support/Package.h"
34 #include "support/Path.h"
35 #include "support/Systemcall.h"
36 #include "support/qstring_helpers.h"
40 #include "support/lassert.h"
41 #include "support/regex.h"
66 bool isLyXFileName(string const & filename)
68 return suffixIs(ascii_lowercase(filename), ".lyx");
72 bool isSGMLFileName(string const & filename)
74 return suffixIs(ascii_lowercase(filename), ".sgml");
78 bool isValidLaTeXFileName(string const & filename)
80 string const invalid_chars("#%\"");
81 return filename.find_first_of(invalid_chars) == string::npos;
85 bool isValidDVIFileName(string const & filename)
87 string const invalid_chars("${}()[]^");
88 return filename.find_first_of(invalid_chars) == string::npos;
92 string const latex_path(string const & original_path,
93 latex_path_extension extension,
96 // On cygwin, we may need windows or posix style paths.
97 string path = os::latex_path(original_path);
98 path = subst(path, "~", "\\string~");
99 if (path.find(' ') != string::npos) {
100 // We can't use '"' because " is sometimes active (e.g. if
101 // babel is loaded with the "german" option)
102 if (extension == EXCLUDE_EXTENSION) {
103 // ChangeExtension calls os::internal_path internally
104 // so don't use it to remove the extension.
105 string const ext = getExtension(path);
106 string const base = ext.empty() ?
108 path.substr(0, path.length() - ext.length() - 1);
109 // ChangeExtension calls os::internal_path internally
110 // so don't use it to re-add the extension.
111 path = "\\string\"" + base + "\\string\"." + ext;
113 path = "\\string\"" + path + "\\string\"";
117 return dots == ESCAPE_DOTS ? subst(path, ".", "\\lyxdot ") : path;
121 // Substitutes spaces with underscores in filename (and path)
122 FileName const makeLatexName(FileName const & file)
124 string name = file.onlyFileName();
125 string const path = file.onlyPath().absFileName() + "/";
127 // ok so we scan through the string twice, but who cares.
128 // FIXME: in Unicode time this will break for sure! There is
129 // a non-latin world out there...
130 string const keep = "abcdefghijklmnopqrstuvwxyz"
131 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
132 "@!'()*+,-./0123456789:;<=>?[]`|";
134 string::size_type pos = 0;
135 while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
138 FileName latex_name(path + name);
139 latex_name.changeExtension(".tex");
144 string const quoteName(string const & name, quote_style style)
148 // This does not work on native Windows for filenames
149 // containing the following characters < > : " / \ | ? *
150 // Moreover, it can't be made to work, as, according to
151 // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
152 // those are reserved characters, and thus are forbidden.
153 // Please, also note that the command-line parser in
154 // ForkedCall::generateChild cannot deal with filenames
155 // containing " or ', therefore we don't pass user filenames
156 // to child processes if possible. We store them in a python
157 // script instead, where we don't have these limitations.
159 return (os::shell() == os::UNIX) ?
160 '\'' + subst(name, "'", "\'\\\'\'") + '\'' :
163 // According to the QProcess parser, a single double
164 // quote is represented by three consecutive ones.
165 // Here we simply escape the double quote and let our
166 // simple parser in Systemcall.cpp do the substitution.
167 return '"' + subst(name, "\"", "\\\"") + '"';
170 return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
173 // shut up stupid compiler
179 // Uses a string of paths separated by ";"s to find a file to open.
180 // Can't cope with pathnames with a ';' in them. Returns full path to file.
181 // If path entry begins with $$LyX/, use system_lyxdir
182 // If path entry begins with $$User/, use user_lyxdir
183 // Example: "$$User/doc;$$LyX/doc"
184 FileName const fileOpenSearch(string const & path, string const & name,
189 bool notfound = true;
190 string tmppath = split(path, path_element, ';');
192 while (notfound && !path_element.empty()) {
193 path_element = os::internal_path(path_element);
194 if (!suffixIs(path_element, '/'))
196 path_element = subst(path_element, "$$LyX",
197 package().system_support().absFileName());
198 path_element = subst(path_element, "$$User",
199 package().user_support().absFileName());
201 real_file = fileSearch(path_element, name, ext);
203 if (real_file.empty()) {
205 tmppath = split(tmppath, path_element, ';');
206 } while (!tmppath.empty() && path_element.empty());
216 // Returns the real name of file name in directory path, with optional
218 FileName const fileSearch(string const & path, string const & name,
219 string const & ext, search_mode mode)
221 // if `name' is an absolute path, we ignore the setting of `path'
222 // Expand Environmentvariables in 'name'
223 string const tmpname = replaceEnvironmentPath(name);
224 FileName fullname(makeAbsPath(tmpname, path));
225 // search first without extension, then with it.
226 if (fullname.isReadableFile())
230 return mode == may_not_exist ? fullname : FileName();
231 // Only add the extension if it is not already the extension of
233 if (getExtension(fullname.absFileName()) != ext)
234 fullname = FileName(addExtension(fullname.absFileName(), ext));
235 if (fullname.isReadableFile() || mode == may_not_exist)
241 // Search the file name.ext in the subdirectory dir of
243 // 2) build_lyxdir (if not empty)
245 FileName const libFileSearch(string const & dir, string const & name,
248 FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
250 if (!fullname.empty())
253 if (!package().build_support().empty())
254 fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
256 if (!fullname.empty())
259 return fileSearch(addPath(package().system_support().absFileName(), dir), name, ext);
263 FileName const i18nLibFileSearch(string const & dir, string const & name,
266 /* The highest priority value is the `LANGUAGE' environment
267 variable. But we don't use the value if the currently
268 selected locale is the C locale. This is a GNU extension.
270 Otherwise, w use a trick to guess what support/gettext.has done:
271 each po file is able to tell us its name. (JMarc)
274 string lang = to_ascii(_(languageTestString()));
275 string const language = getEnv("LANGUAGE");
276 if (!lang.empty() && !language.empty())
280 lang = split(lang, l, ':');
283 // First try with the full name
284 tmp = libFileSearch(addPath(dir, l), name, ext);
288 // Then the name without country code
289 string const shortl = token(l, '_', 0);
291 tmp = libFileSearch(addPath(dir, shortl), name, ext);
297 // For compatibility, to be removed later (JMarc)
298 tmp = libFileSearch(dir, token(l, '_', 0) + '_' + name,
301 lyxerr << "i18nLibFileSearch: File `" << tmp
302 << "' has been found by the old method" <<endl;
306 lang = split(lang, l, ':');
309 return libFileSearch(dir, name, ext);
313 FileName const imageLibFileSearch(string & dir, string const & name,
316 if (!lyx::lyxrc.icon_set.empty()) {
317 string const imagedir = addPath(dir, lyx::lyxrc.icon_set);
318 FileName const fn = libFileSearch(imagedir, name, ext);
324 return libFileSearch(dir, name, ext);
328 string const libScriptSearch(string const & command_in, quote_style style)
330 static string const token_scriptpath = "$$s/";
332 string command = command_in;
333 // Find the starting position of "$$s/"
334 string::size_type const pos1 = command.find(token_scriptpath);
335 if (pos1 == string::npos)
337 // Find the end of the "$$s/some_subdir/some_script" word within
338 // command. Assumes that the script name does not contain spaces.
339 string::size_type const start_script = pos1 + 4;
340 string::size_type const pos2 = command.find(' ', start_script);
341 string::size_type const size_script = pos2 == string::npos?
342 (command.size() - start_script) : pos2 - start_script;
344 // Does this script file exist?
345 string const script =
346 libFileSearch(".", command.substr(start_script, size_script)).absFileName();
348 if (script.empty()) {
349 // Replace "$$s/" with ""
350 command.erase(pos1, 4);
352 // Replace "$$s/foo/some_script" with "<path to>/some_script".
353 string::size_type const size_replace = size_script + 4;
354 command.replace(pos1, size_replace, quoteName(script, style));
361 static FileName createTmpDir(FileName const & tempdir, string const & mask)
363 LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
364 << "createTmpDir: mask=`" << mask << '\'');
366 FileName const tmpfl = FileName::tempName(tempdir, mask);
368 if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
369 LYXERR0("LyX could not create temporary directory in " << tempdir
378 FileName const createLyXTmpDir(FileName const & deflt)
380 if (deflt.empty() || deflt == package().system_temp_dir())
381 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
383 if (deflt.createDirectory(0777))
386 if (deflt.isDirWritable()) {
387 // deflt could not be created because it
388 // did exist already, so let's create our own
390 return createTmpDir(deflt, "lyx_tmpdir");
392 // some other error occured.
393 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
398 // Strip filename from path name
399 string const onlyPath(string const & filename)
401 // If empty filename, return empty
402 if (filename.empty())
405 // Find last / or start of filename
406 size_t j = filename.rfind('/');
407 return j == string::npos ? "./" : filename.substr(0, j + 1);
411 // Convert relative path into absolute path based on a basepath.
412 // If relpath is absolute, just use that.
413 // If basepath is empty, use CWD as base.
414 // Note that basePath can be a relative path, in the sense that it may
415 // not begin with "/" (e.g.), but it should NOT contain such constructs
417 // FIXME It might be nice if the code didn't simply assume that.
418 FileName const makeAbsPath(string const & relPath, string const & basePath)
420 // checks for already absolute path
421 if (FileName::isAbsolute(relPath))
422 return FileName(relPath);
424 // Copies given paths
425 string tempRel = os::internal_path(relPath);
426 // Since TempRel is NOT absolute, we can safely replace "//" with "/"
427 tempRel = subst(tempRel, "//", "/");
431 if (FileName::isAbsolute(basePath))
434 tempBase = addPath(FileName::getcwd().absFileName(), basePath);
436 // Handle /./ at the end of the path
437 while (suffixIs(tempBase, "/./"))
438 tempBase.erase(tempBase.length() - 2);
440 // processes relative path
441 string rTemp = tempRel;
444 // Check for a leading "~"
446 rTemp = split(rTemp, temp, '/');
448 tempBase = Package::get_home_dir().absFileName();
453 while (!rTemp.empty()) {
455 rTemp = split(rTemp, temp, '/');
457 if (temp == ".") continue;
459 // Remove one level of TempBase
460 if (tempBase.length() <= 1) {
461 //this is supposed to be an absolute path, so...
465 //erase a trailing slash if there is one
466 if (suffixIs(tempBase, "/"))
467 tempBase.erase(tempBase.length() - 1, string::npos);
469 string::size_type i = tempBase.length() - 1;
470 while (i > 0 && tempBase[i] != '/')
473 tempBase.erase(i, string::npos);
476 } else if (temp.empty() && !rTemp.empty()) {
477 tempBase = os::current_root() + rTemp;
480 // Add this piece to TempBase
481 if (!suffixIs(tempBase, '/'))
487 // returns absolute path
488 return FileName(tempBase);
492 // Correctly append filename to the pathname.
493 // If pathname is '.', then don't use pathname.
494 // Chops any path of filename.
495 string const addName(string const & path, string const & fname)
497 string const basename = onlyFileName(fname);
500 if (path != "." && path != "./" && !path.empty()) {
501 buf = os::internal_path(path);
502 if (!suffixIs(path, '/'))
506 return buf + basename;
510 // Strips path from filename
511 string const onlyFileName(string const & fname)
516 string::size_type j = fname.rfind('/');
517 if (j == string::npos) // no '/' in fname
521 return fname.substr(j + 1);
525 // Create absolute path. If impossible, don't do anything
526 // Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
527 string const expandPath(string const & path)
529 // checks for already absolute path
530 string rTemp = replaceEnvironmentPath(path);
531 if (FileName::isAbsolute(rTemp))
535 string const copy = rTemp;
538 rTemp = split(rTemp, temp, '/');
541 return FileName::getcwd().absFileName() + '/' + rTemp;
544 return Package::get_home_dir().absFileName() + '/' + rTemp;
547 return makeAbsPath(copy).absFileName();
549 // Don't know how to handle this
554 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
555 string const replaceEnvironmentPath(string const & path)
557 // ${VAR} is defined as
558 // $\{[A-Za-z_][A-Za-z_0-9]*\}
559 static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
561 // $VAR is defined as:
562 // $[A-Za-z_][A-Za-z_0-9]*
563 static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
565 static regex envvar_br_re("(.*)" + envvar_br + "(.*)");
566 static regex envvar_re("(.*)" + envvar + "(.*)");
567 string result = path;
570 if (!regex_match(result, what, envvar_br_re)) {
571 if (!regex_match(result, what, envvar_re))
574 string env_var = getEnv(what.str(2));
575 result = what.str(1) + env_var + what.str(3);
581 // Replace current directory in all elements of a path list with a given path.
582 string const replaceCurdirPath(string const & path, string const & pathlist)
584 string const oldpathlist = replaceEnvironmentPath(pathlist);
585 char const sep = os::path_separator();
588 for (size_t i = 0, k = 0; i != string::npos; k = i) {
589 i = oldpathlist.find(sep, i);
590 string p = oldpathlist.substr(k, i - k);
591 if (FileName::isAbsolute(p)) {
597 } else if (prefixIs(p, "./")) {
599 while (p[offset] == '/')
602 newpathlist += addPath(path, p.substr(offset));
603 if (suffixIs(p, "//"))
606 if (i != string::npos) {
608 // Stop here if the last element is empty
609 if (++i == oldpathlist.length())
617 // Make relative path out of two absolute paths
618 docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
619 // Makes relative path out of absolute path. If it is deeper than basepath,
620 // it's easy. If basepath and abspath share something (they are all deeper
621 // than some directory), it'll be rendered using ..'s. If they are completely
622 // different, then the absolute path will be used as relative path.
624 docstring::size_type const abslen = abspath.length();
625 docstring::size_type const baselen = basepath.length();
627 docstring::size_type i = os::common_path(abspath, basepath);
630 // actually no match - cannot make it relative
634 // Count how many dirs there are in basepath above match
635 // and append as many '..''s into relpath
637 docstring::size_type j = i;
638 while (j < baselen) {
639 if (basepath[j] == '/') {
640 if (j + 1 == baselen)
647 // Append relative stuff from common directory to abspath
648 if (abspath[i] == '/')
650 for (; i < abslen; ++i)
653 if (suffixIs(buf, '/'))
654 buf.erase(buf.length() - 1);
655 // Substitute empty with .
662 // Append sub-directory(ies) to a path in an intelligent way
663 string const addPath(string const & path, string const & path_2)
666 string const path2 = os::internal_path(path_2);
668 if (!path.empty() && path != "." && path != "./") {
669 buf = os::internal_path(path);
670 if (path[path.length() - 1] != '/')
674 if (!path2.empty()) {
675 string::size_type const p2start = path2.find_first_not_of('/');
676 string::size_type const p2end = path2.find_last_not_of('/');
677 string const tmp = path2.substr(p2start, p2end - p2start + 1);
684 string const changeExtension(string const & oldname, string const & extension)
686 string::size_type const last_slash = oldname.rfind('/');
687 string::size_type last_dot = oldname.rfind('.');
688 if (last_dot < last_slash && last_slash != string::npos)
689 last_dot = string::npos;
692 // Make sure the extension starts with a dot
693 if (!extension.empty() && extension[0] != '.')
694 ext= '.' + extension;
698 return os::internal_path(oldname.substr(0, last_dot) + ext);
702 string const removeExtension(string const & name)
704 return changeExtension(name, string());
708 string const addExtension(string const & name, string const & extension)
710 if (!extension.empty() && extension[0] != '.')
711 return name + '.' + extension;
712 return name + extension;
716 /// Return the extension of the file (not including the .)
717 string const getExtension(string const & name)
719 string::size_type const last_slash = name.rfind('/');
720 string::size_type const last_dot = name.rfind('.');
721 if (last_dot != string::npos &&
722 (last_slash == string::npos || last_dot > last_slash))
723 return name.substr(last_dot + 1,
724 name.length() - (last_dot + 1));
730 string const unzippedFileName(string const & zipped_file)
732 string const ext = getExtension(zipped_file);
733 if (ext == "gz" || ext == "z" || ext == "Z")
734 return changeExtension(zipped_file, string());
735 return onlyPath(zipped_file) + "unzipped_" + onlyFileName(zipped_file);
739 FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
741 FileName const tempfile = FileName(unzipped_file.empty() ?
742 unzippedFileName(zipped_file.toFilesystemEncoding()) :
745 string const command = "gunzip -c " +
746 zipped_file.toFilesystemEncoding() + " > " +
747 tempfile.toFilesystemEncoding();
749 one.startscript(Systemcall::Wait, command);
750 // test that command was executed successfully (anon)
751 // yes, please do. (Lgb)
756 docstring const makeDisplayPath(string const & path, unsigned int threshold)
760 // If file is from LyXDir, display it as if it were relative.
761 string const system = package().system_support().absFileName();
762 if (prefixIs(str, system) && str != system)
763 return from_utf8("[" + str.erase(0, system.length()) + "]");
765 // replace /home/blah with ~/
766 string const home = Package::get_home_dir().absFileName();
767 if (!home.empty() && prefixIs(str, home))
768 str = subst(str, home, "~");
770 if (str.length() <= threshold)
771 return from_utf8(os::external_path(str));
773 string const prefix = ".../";
776 while (str.length() > threshold)
777 str = split(str, temp, '/');
779 // Did we shorten everything away?
781 // Yes, filename itself is too long.
782 // Pick the start and the end of the filename.
783 str = onlyFileName(path);
784 string const head = str.substr(0, threshold / 2 - 3);
786 string::size_type len = str.length();
788 str.substr(len - threshold / 2 - 2, len - 1);
789 str = head + "..." + tail;
792 return from_utf8(os::external_path(prefix + str));
797 bool readLink(FileName const & file, FileName & link)
799 string const encoded = file.toFilesystemEncoding();
800 #ifdef HAVE_DEF_PATH_MAX
801 char linkbuffer[PATH_MAX + 1];
802 int const nRead = ::readlink(encoded.c_str(),
803 linkbuffer, sizeof(linkbuffer) - 1);
806 linkbuffer[nRead] = '\0'; // terminator
808 vector<char> buf(1024);
812 nRead = ::readlink(encoded.c_str(), &buf[0], buf.size() - 1);
816 if (nRead < buf.size() - 1) {
819 buf.resize(buf.size() * 2);
821 buf[nRead] = '\0'; // terminator
822 const char * linkbuffer = &buf[0];
824 link = makeAbsPath(linkbuffer, onlyPath(file.absFileName()));
828 bool readLink(FileName const &, FileName &)
835 cmd_ret const runCommand(string const & cmd)
837 // FIXME: replace all calls to RunCommand with ForkedCall
838 // (if the output is not needed) or the code in ISpell.cpp
839 // (if the output is needed).
841 // One question is if we should use popen or
842 // create our own popen based on fork, exec, pipe
843 // of course the best would be to have a
844 // pstream (process stream), with the
845 // variants ipstream, opstream
850 PROCESS_INFORMATION process;
851 SECURITY_ATTRIBUTES security;
854 bool err2out = false;
856 string const infile = trim(split(cmd, command, '<'), " \"");
857 command = rtrim(command);
858 if (suffixIs(command, "2>&1")) {
859 command = rtrim(command, "2>&1");
862 string const cmdarg = "/d /c " + command;
863 string const comspec = getEnv("COMSPEC");
865 security.nLength = sizeof(SECURITY_ATTRIBUTES);
866 security.bInheritHandle = TRUE;
867 security.lpSecurityDescriptor = NULL;
869 if (CreatePipe(&in, &out, &security, 0)) {
870 memset(&startup, 0, sizeof(STARTUPINFO));
871 memset(&process, 0, sizeof(PROCESS_INFORMATION));
873 startup.cb = sizeof(STARTUPINFO);
874 startup.dwFlags = STARTF_USESTDHANDLES;
876 startup.hStdError = err2out ? out : GetStdHandle(STD_ERROR_HANDLE);
877 startup.hStdInput = infile.empty()
878 ? GetStdHandle(STD_INPUT_HANDLE)
879 : CreateFile(infile.c_str(), GENERIC_READ,
880 FILE_SHARE_READ, &security, OPEN_EXISTING,
881 FILE_ATTRIBUTE_NORMAL, NULL);
882 startup.hStdOutput = out;
884 if (startup.hStdInput != INVALID_HANDLE_VALUE &&
885 CreateProcess(comspec.c_str(), (LPTSTR)cmdarg.c_str(),
886 &security, &security, TRUE, CREATE_NO_WINDOW,
887 0, 0, &startup, &process)) {
889 CloseHandle(process.hThread);
890 fno = _open_osfhandle((long)in, _O_RDONLY);
892 inf = _fdopen(fno, "r");
895 #elif defined (HAVE_POPEN)
896 FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
897 #elif defined (HAVE__POPEN)
898 FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
900 #error No popen() function.
903 // (Claus Hentschel) Check if popen was successful ;-)
905 lyxerr << "RunCommand:: could not start child process" << endl;
906 return make_pair(-1, string());
912 ret += static_cast<char>(c);
917 WaitForSingleObject(process.hProcess, INFINITE);
919 CloseHandle(startup.hStdInput);
920 CloseHandle(process.hProcess);
921 int const pret = fclose(inf);
922 #elif defined (HAVE_PCLOSE)
923 int const pret = pclose(inf);
924 #elif defined (HAVE__PCLOSE)
925 int const pret = _pclose(inf);
927 #error No pclose() function.
931 perror("RunCommand:: could not terminate child process");
933 return make_pair(pret, ret);
937 FileName const findtexfile(string const & fil, string const & /*format*/)
939 /* There is no problem to extend this function too use other
940 methods to look for files. It could be setup to look
941 in environment paths and also if wanted as a last resort
942 to a recursive find. One of the easier extensions would
943 perhaps be to use the LyX file lookup methods. But! I am
944 going to implement this until I see some demand for it.
948 // If the file can be found directly, we just return a
949 // absolute path version of it.
950 FileName const absfile(makeAbsPath(fil));
951 if (absfile.exists())
954 // Now we try to find it using kpsewhich.
955 // It seems from the kpsewhich manual page that it is safe to use
956 // kpsewhich without --format: "When the --format option is not
957 // given, the search path used when looking for a file is inferred
958 // from the name given, by looking for a known extension. If no
959 // known extension is found, the search path for TeX source files
961 // However, we want to take advantage of the format sine almost all
962 // the different formats has environment variables that can be used
963 // to controll which paths to search. f.ex. bib looks in
964 // BIBINPUTS and TEXBIB. Small list follows:
965 // bib - BIBINPUTS, TEXBIB
967 // graphic/figure - TEXPICTS, TEXINPUTS
968 // ist - TEXINDEXSTYLE, INDEXSTYLE
969 // pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
971 // tfm - TFMFONTS, TEXFONTS
972 // This means that to use kpsewhich in the best possible way we
973 // should help it by setting additional path in the approp. envir.var.
974 string const kpsecmd = "kpsewhich " + fil;
976 cmd_ret const c = runCommand(kpsecmd);
978 LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
979 << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
981 return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
987 void readBB_lyxerrMessage(FileName const & file, bool & zipped,
988 string const & message)
990 LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
991 // FIXME: Why is this func deleting a file? (Lgb)
997 string const readBB_from_PSFile(FileName const & file)
999 // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
1000 // It seems that every command in the header has an own line,
1001 // getline() should work for all files.
1002 // On the other hand some plot programs write the bb at the
1003 // end of the file. Than we have in the header:
1004 // %%BoundingBox: (atend)
1005 // In this case we must check the end.
1006 bool zipped = file.isZippedFile();
1007 FileName const file_ = zipped ? unzipFile(file) : file;
1008 string const format = file_.guessFormatFromContents();
1010 if (format != "eps" && format != "ps") {
1011 readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
1015 static lyx::regex bbox_re(
1016 "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
1017 ifstream is(file_.toFilesystemEncoding().c_str());
1022 if (regex_match(s, what, bbox_re)) {
1023 // Our callers expect the tokens in the string
1024 // separated by single spaces.
1025 // FIXME: change return type from string to something
1028 os << what.str(1) << ' ' << what.str(2) << ' '
1029 << what.str(3) << ' ' << what.str(4);
1030 string const bb = os.str();
1031 readBB_lyxerrMessage(file_, zipped, bb);
1035 readBB_lyxerrMessage(file_, zipped, "no bb found");
1040 int compare_timestamps(FileName const & file1, FileName const & file2)
1042 // If the original is newer than the copy, then copy the original
1043 // to the new directory.
1046 if (file1.exists() && file2.exists()) {
1047 double const tmp = difftime(file1.lastModified(), file2.lastModified());
1049 cmp = tmp > 0 ? 1 : -1;
1051 } else if (file1.exists()) {
1053 } else if (file2.exists()) {
1061 bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfuns)
1063 FileName const script = libFileSearch("scripts", "prefs2prefs.py");
1064 if (script.empty()) {
1065 LYXERR0("Could not find bind file conversion "
1066 "script prefs2prefs.py.");
1070 ostringstream command;
1071 command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
1072 << ' ' << (lfuns ? "-l" : "-p") << ' '
1073 << quoteName(filename.toFilesystemEncoding())
1074 << ' ' << quoteName(tempfile.toFilesystemEncoding());
1075 string const command_str = command.str();
1077 LYXERR(Debug::FILES, "Running `" << command_str << '\'');
1079 cmd_ret const ret = runCommand(command_str);
1080 if (ret.first != 0) {
1081 LYXERR0("Could not run file conversion script prefs2prefs.py.");
1088 } //namespace support