2002-09-25 10:03:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file filetools.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
|
|
|
|
|
*
|
|
|
|
|
* \author Ivan Schreter
|
|
|
|
|
* \author Dirk Niggemann
|
|
|
|
|
* \author Asger Alstrup
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
2003-06-18 09:56:10 +00:00
|
|
|
|
* \author Angus Leeming
|
|
|
|
|
* \author John Levon
|
2003-09-07 01:45:40 +00:00
|
|
|
|
* \author Herbert Vo<EFBFBD>
|
2002-09-25 10:03:41 +00:00
|
|
|
|
*
|
2003-09-08 09:51:40 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 10:03:41 +00:00
|
|
|
|
*
|
|
|
|
|
* General path-mangling functions
|
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
|
#include "support/convert.h"
|
2005-02-15 13:45:41 +00:00
|
|
|
|
#include "support/environment.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include "support/filetools.h"
|
2005-01-31 10:42:26 +00:00
|
|
|
|
#include "support/fs_extras.h"
|
2005-02-15 13:45:41 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
#include "support/os.h"
|
2005-02-15 13:45:41 +00:00
|
|
|
|
#include "support/package.h"
|
|
|
|
|
#include "support/path.h"
|
|
|
|
|
#include "support/systemcall.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
|
// FIXME Interface violation
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "gettext.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-09-09 17:25:35 +00:00
|
|
|
|
#include <boost/assert.hpp>
|
2005-01-31 10:42:26 +00:00
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2003-09-25 23:01:43 +00:00
|
|
|
|
#include <boost/regex.hpp>
|
2003-09-09 17:25:35 +00:00
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
2002-06-10 07:57:39 +00:00
|
|
|
|
#include <cctype>
|
2005-04-21 14:03:30 +00:00
|
|
|
|
#include <cerrno>
|
2002-06-10 07:57:39 +00:00
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <fstream>
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
2002-06-10 07:57:39 +00:00
|
|
|
|
|
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
|
using std::fgetc;
|
|
|
|
|
using std::isalnum;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::isalpha;
|
2002-06-10 07:57:39 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::getline;
|
|
|
|
|
using std::make_pair;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
using std::ifstream;
|
2003-09-05 18:02:24 +00:00
|
|
|
|
using std::ostringstream;
|
2000-11-08 09:39:46 +00:00
|
|
|
|
using std::vector;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
namespace fs = boost::filesystem;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace support {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 09:09:57 +00:00
|
|
|
|
bool isLyXFilename(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2002-07-16 21:17:10 +00:00
|
|
|
|
return suffixIs(ascii_lowercase(filename), ".lyx");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-08 09:09:57 +00:00
|
|
|
|
bool isSGMLFilename(string const & filename)
|
2000-11-13 15:43:36 +00:00
|
|
|
|
{
|
2002-07-16 21:17:10 +00:00
|
|
|
|
return suffixIs(ascii_lowercase(filename), ".sgml");
|
2000-11-13 15:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-07-10 09:31:27 +00:00
|
|
|
|
string const latex_path(string const & original_path,
|
|
|
|
|
latex_path_extension extension,
|
|
|
|
|
latex_path_dots dots)
|
2005-04-18 17:43:11 +00:00
|
|
|
|
{
|
2006-03-24 12:48:37 +00:00
|
|
|
|
// On cygwin, we may need windows or posix style paths.
|
2006-04-05 19:26:08 +00:00
|
|
|
|
string path = os::latex_path(original_path);
|
2005-04-18 17:43:11 +00:00
|
|
|
|
path = subst(path, "~", "\\string~");
|
2005-07-14 22:09:22 +00:00
|
|
|
|
if (path.find(' ') != string::npos) {
|
2005-06-21 09:01:41 +00:00
|
|
|
|
// We can't use '"' because " is sometimes active (e.g. if
|
|
|
|
|
// babel is loaded with the "german" option)
|
2005-07-10 09:31:27 +00:00
|
|
|
|
if (extension == EXCLUDE_EXTENSION) {
|
2006-04-05 19:26:08 +00:00
|
|
|
|
// ChangeExtension calls os::internal_path internally
|
|
|
|
|
// so don't use it to remove the extension.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const ext = getExtension(path);
|
2006-04-05 19:26:08 +00:00
|
|
|
|
string const base = ext.empty() ?
|
|
|
|
|
path :
|
|
|
|
|
path.substr(0, path.length() - ext.length() - 1);
|
2005-07-08 15:43:46 +00:00
|
|
|
|
// ChangeExtension calls os::internal_path internally
|
|
|
|
|
// so don't use it to re-add the extension.
|
|
|
|
|
path = "\\string\"" + base + "\\string\"." + ext;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
} else {
|
2005-07-08 15:43:46 +00:00
|
|
|
|
path = "\\string\"" + path + "\\string\"";
|
2005-07-14 22:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dots == ESCAPE_DOTS ? subst(path, ".", "\\lyxdot ") : path;
|
2005-04-18 17:43:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Substitutes spaces with underscores in filename (and path)
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const makeLatexName(string const & file)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string name = onlyFilename(file);
|
|
|
|
|
string const path = onlyPath(file);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-11-02 21:19:58 +00:00
|
|
|
|
// ok so we scan through the string twice, but who cares.
|
2005-07-14 22:09:22 +00:00
|
|
|
|
string const keep = "abcdefghijklmnopqrstuvwxyz"
|
1999-11-03 12:24:23 +00:00
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2007-02-25 09:00:28 +00:00
|
|
|
|
"@!'()*+,-./0123456789:;<=>?[]`|";
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-11-02 21:19:58 +00:00
|
|
|
|
string::size_type pos = 0;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
|
1999-11-03 12:45:11 +00:00
|
|
|
|
name[pos++] = '_';
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
return addName(path, name);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
|
2006-09-11 16:07:49 +00:00
|
|
|
|
string const quoteName(string const & name, quote_style style)
|
1999-12-03 13:51:01 +00:00
|
|
|
|
{
|
2006-09-11 16:07:49 +00:00
|
|
|
|
switch(style) {
|
|
|
|
|
case quote_shell:
|
|
|
|
|
// This does not work for filenames containing " (windows)
|
|
|
|
|
// or ' (all other OSes). This can't be changed easily, since
|
|
|
|
|
// we would need to adapt the command line parser in
|
|
|
|
|
// Forkedcall::generateChild. Therefore we don't pass user
|
|
|
|
|
// filenames to child processes if possible. We store them in
|
|
|
|
|
// a python script instead, where we don't have these
|
|
|
|
|
// limitations.
|
|
|
|
|
return (os::shell() == os::UNIX) ?
|
|
|
|
|
'\'' + name + '\'':
|
|
|
|
|
'"' + name + '"';
|
|
|
|
|
case quote_python:
|
|
|
|
|
return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
|
|
|
|
|
+ "\"";
|
|
|
|
|
}
|
|
|
|
|
// shut up stupid compiler
|
|
|
|
|
return string();
|
1999-12-03 13:51:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool isFileReadable(FileName const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
std::string const path = filename.toFilesystemEncoding();
|
2005-01-31 10:42:26 +00:00
|
|
|
|
return fs::exists(path) && !fs::is_directory(path) && fs::is_readable(path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-08-01 10:08:53 +00:00
|
|
|
|
//returns true: dir writeable
|
|
|
|
|
// false: not writeable
|
2006-12-27 10:56:11 +00:00
|
|
|
|
bool isDirWriteable(FileName const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-04-08 09:09:57 +00:00
|
|
|
|
lyxerr[Debug::FILES] << "isDirWriteable: " << path << endl;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
FileName const tmpfl(tempName(path, "lyxwritetest"));
|
2001-08-01 10:08:53 +00:00
|
|
|
|
|
|
|
|
|
if (tmpfl.empty())
|
|
|
|
|
return false;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
unlink(tmpfl);
|
2001-08-01 10:08:53 +00:00
|
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-21 12:04:01 +00:00
|
|
|
|
#if 0
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// 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
|
|
|
|
|
// If path entry begins with $$User/, use user_lyxdir
|
|
|
|
|
// Example: "$$User/doc;$$LyX/doc"
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const fileOpenSearch(string const & path, string const & name,
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName real_file;
|
2002-06-26 19:07:34 +00:00
|
|
|
|
string path_element;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
bool notfound = true;
|
1999-11-02 06:42:25 +00:00
|
|
|
|
string tmppath = split(path, path_element, ';');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
while (notfound && !path_element.empty()) {
|
2004-12-20 16:59:33 +00:00
|
|
|
|
path_element = os::internal_path(path_element);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
if (!suffixIs(path_element, '/'))
|
2005-07-14 22:09:22 +00:00
|
|
|
|
path_element += '/';
|
2005-01-10 19:17:43 +00:00
|
|
|
|
path_element = subst(path_element, "$$LyX",
|
|
|
|
|
package().system_support());
|
|
|
|
|
path_element = subst(path_element, "$$User",
|
|
|
|
|
package().user_support());
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
real_file = fileSearch(path_element, name, ext);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (real_file.empty()) {
|
2000-01-20 01:41:55 +00:00
|
|
|
|
do {
|
|
|
|
|
tmppath = split(tmppath, path_element, ';');
|
2001-12-05 08:04:20 +00:00
|
|
|
|
} while (!tmppath.empty() && path_element.empty());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else {
|
2000-01-20 01:41:55 +00:00
|
|
|
|
notfound = false;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return real_file;
|
|
|
|
|
}
|
2006-12-21 12:04:01 +00:00
|
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
/// Returns a vector of all files in directory dir having extension ext.
|
2006-12-21 13:01:47 +00:00
|
|
|
|
vector<FileName> const dirList(FileName const & dir, string const & ext)
|
2000-11-08 09:39:46 +00:00
|
|
|
|
{
|
2005-01-31 10:42:26 +00:00
|
|
|
|
// EXCEPTIONS FIXME. Rewrite needed when we turn on exceptions. (Lgb)
|
2006-12-21 13:01:47 +00:00
|
|
|
|
vector<FileName> dirlist;
|
2005-01-31 10:42:26 +00:00
|
|
|
|
|
2006-12-05 12:57:01 +00:00
|
|
|
|
string const encoded_dir = dir.toFilesystemEncoding();
|
|
|
|
|
if (!(fs::exists(encoded_dir) && fs::is_directory(encoded_dir))) {
|
2002-03-21 17:09:55 +00:00
|
|
|
|
lyxerr[Debug::FILES]
|
2001-12-02 16:18:13 +00:00
|
|
|
|
<< "Directory \"" << dir
|
|
|
|
|
<< "\" does not exist to DirList." << endl;
|
|
|
|
|
return dirlist;
|
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
string extension;
|
|
|
|
|
if (!ext.empty() && ext[0] != '.')
|
|
|
|
|
extension += '.';
|
|
|
|
|
extension += ext;
|
|
|
|
|
|
2006-12-05 12:57:01 +00:00
|
|
|
|
fs::directory_iterator dit(encoded_dir);
|
2005-01-31 10:42:26 +00:00
|
|
|
|
fs::directory_iterator end;
|
|
|
|
|
for (; dit != end; ++dit) {
|
|
|
|
|
string const & fil = dit->leaf();
|
2006-12-21 13:01:47 +00:00
|
|
|
|
if (suffixIs(fil, extension))
|
|
|
|
|
dirlist.push_back(FileName::fromFilesystemEncoding(
|
2006-12-27 10:56:11 +00:00
|
|
|
|
encoded_dir + '/' + fil));
|
2000-11-08 09:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
return dirlist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Returns the real name of file name in directory path, with optional
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// extension ext.
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const fileSearch(string const & path, string const & name,
|
2006-12-21 12:04:01 +00:00
|
|
|
|
string const & ext, search_mode mode)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// if `name' is an absolute path, we ignore the setting of `path'
|
|
|
|
|
// Expand Environmentvariables in 'name'
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const tmpname = replaceEnvironmentPath(name);
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName fullname(makeAbsPath(tmpname, path));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// search first without extension, then with it.
|
2007-01-29 18:37:13 +00:00
|
|
|
|
if (isFileReadable(fullname))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return fullname;
|
2007-01-29 18:37:13 +00:00
|
|
|
|
if (ext.empty())
|
2006-12-21 12:04:01 +00:00
|
|
|
|
// We are done.
|
2007-01-29 18:37:13 +00:00
|
|
|
|
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();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Search the file name.ext in the subdirectory dir of
|
|
|
|
|
// 1) user_lyxdir
|
|
|
|
|
// 2) build_lyxdir (if not empty)
|
|
|
|
|
// 3) system_lyxdir
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const libFileSearch(string const & dir, string const & name,
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName fullname = fileSearch(addPath(package().user_support(), dir),
|
2005-01-10 19:17:43 +00:00
|
|
|
|
name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!fullname.empty())
|
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2005-01-10 19:17:43 +00:00
|
|
|
|
if (!package().build_support().empty())
|
2006-04-08 22:31:11 +00:00
|
|
|
|
fullname = fileSearch(addPath(package().build_support(), dir),
|
2005-01-10 19:17:43 +00:00
|
|
|
|
name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!fullname.empty())
|
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
return fileSearch(addPath(package().system_support(), dir), name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const i18nLibFileSearch(string const & dir, string const & name,
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2004-03-24 12:05:57 +00:00
|
|
|
|
// the following comments are from intl/dcigettext.c. We try
|
|
|
|
|
// to mimick this behaviour here.
|
2001-10-12 16:23:26 +00:00
|
|
|
|
/* The highest priority value is the `LANGUAGE' environment
|
2001-10-24 13:21:20 +00:00
|
|
|
|
variable. But we don't use the value if the currently
|
|
|
|
|
selected locale is the C locale. This is a GNU extension. */
|
2004-03-24 12:05:57 +00:00
|
|
|
|
/* [Otherwise] We have to proceed with the POSIX methods of
|
2004-03-24 13:19:07 +00:00
|
|
|
|
looking to `LC_ALL', `LC_xxx', and `LANG'. */
|
2001-10-12 16:23:26 +00:00
|
|
|
|
|
2005-02-15 13:45:41 +00:00
|
|
|
|
string lang = getEnv("LC_ALL");
|
2004-03-24 12:05:57 +00:00
|
|
|
|
if (lang.empty()) {
|
2005-02-15 13:45:41 +00:00
|
|
|
|
lang = getEnv("LC_MESSAGES");
|
2001-10-12 16:23:26 +00:00
|
|
|
|
if (lang.empty()) {
|
2005-02-15 13:45:41 +00:00
|
|
|
|
lang = getEnv("LANG");
|
2004-03-24 12:05:57 +00:00
|
|
|
|
if (lang.empty())
|
|
|
|
|
lang = "C";
|
2001-10-12 16:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-24 12:05:57 +00:00
|
|
|
|
|
2005-02-15 13:45:41 +00:00
|
|
|
|
string const language = getEnv("LANGUAGE");
|
2004-03-24 13:19:07 +00:00
|
|
|
|
if (lang != "C" && lang != "POSIX" && !language.empty())
|
2004-03-24 12:05:57 +00:00
|
|
|
|
lang = language;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2004-06-29 12:55:09 +00:00
|
|
|
|
string l;
|
|
|
|
|
lang = split(lang, l, ':');
|
|
|
|
|
while (!l.empty() && l != "C" && l != "POSIX") {
|
2007-02-25 18:12:03 +00:00
|
|
|
|
FileName const tmp = libFileSearch(addPath(dir, token(l, '_', 0)),
|
|
|
|
|
name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!tmp.empty())
|
|
|
|
|
return tmp;
|
2007-02-25 18:12:03 +00:00
|
|
|
|
#if 1
|
|
|
|
|
// to be removed later (JMarc)
|
|
|
|
|
FileName const tmpold = libFileSearch(dir,
|
|
|
|
|
token(l, '_', 0) + '_' + name,
|
|
|
|
|
ext);
|
|
|
|
|
if (!tmpold.empty()) {
|
|
|
|
|
lyxerr << "i18nLibFileSearch: File `" << tmpold
|
|
|
|
|
<< "' has been found by the old method" <<endl;
|
|
|
|
|
return tmpold;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2004-06-29 12:55:09 +00:00
|
|
|
|
lang = split(lang, l, ':');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2004-06-29 12:55:09 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
return libFileSearch(dir, name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-11 16:07:49 +00:00
|
|
|
|
string const libScriptSearch(string const & command_in, quote_style style)
|
2002-03-27 00:05:28 +00:00
|
|
|
|
{
|
2005-07-14 22:09:22 +00:00
|
|
|
|
static string const token_scriptpath = "$$s/";
|
2003-05-31 09:24:46 +00:00
|
|
|
|
|
|
|
|
|
string command = command_in;
|
|
|
|
|
// Find the starting position of "$$s/"
|
|
|
|
|
string::size_type const pos1 = command.find(token_scriptpath);
|
|
|
|
|
if (pos1 == string::npos)
|
2002-03-27 00:05:28 +00:00
|
|
|
|
return command;
|
2005-02-01 16:41:24 +00:00
|
|
|
|
// Find the end of the "$$s/some_subdir/some_script" word within
|
|
|
|
|
// command. Assumes that the script name does not contain spaces.
|
2003-05-31 09:24:46 +00:00
|
|
|
|
string::size_type const start_script = pos1 + 4;
|
|
|
|
|
string::size_type const pos2 = command.find(' ', start_script);
|
|
|
|
|
string::size_type const size_script = pos2 == string::npos?
|
|
|
|
|
(command.size() - start_script) : pos2 - start_script;
|
|
|
|
|
|
|
|
|
|
// Does this script file exist?
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const script =
|
|
|
|
|
libFileSearch(".", command.substr(start_script, size_script)).absFilename();
|
2003-05-31 09:24:46 +00:00
|
|
|
|
|
|
|
|
|
if (script.empty()) {
|
|
|
|
|
// Replace "$$s/" with ""
|
|
|
|
|
command.erase(pos1, 4);
|
|
|
|
|
} else {
|
2005-01-20 15:02:16 +00:00
|
|
|
|
// Replace "$$s/foo/some_script" with "<path to>/some_script".
|
2003-05-31 09:24:46 +00:00
|
|
|
|
string::size_type const size_replace = size_script + 4;
|
2006-09-11 16:07:49 +00:00
|
|
|
|
command.replace(pos1, size_replace, quoteName(script, style));
|
2003-05-31 09:24:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return command;
|
2002-03-27 00:05:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const createTmpDir(FileName const & tempdir, string const & mask)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-11-14 02:01:57 +00:00
|
|
|
|
lyxerr[Debug::FILES]
|
2004-02-25 12:00:53 +00:00
|
|
|
|
<< "createTmpDir: tempdir=`" << tempdir << "'\n"
|
|
|
|
|
<< "createTmpDir: mask=`" << mask << '\'' << endl;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
FileName const tmpfl(tempName(tempdir, mask));
|
2000-11-08 09:39:46 +00:00
|
|
|
|
// 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)
|
2006-12-27 10:56:11 +00:00
|
|
|
|
unlink(tmpfl);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
|
2004-02-25 12:00:53 +00:00
|
|
|
|
lyxerr << "LyX could not create the temporary directory '"
|
|
|
|
|
<< tmpfl << "'" << endl;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
return FileName();
|
2004-02-25 12:00:53 +00:00
|
|
|
|
}
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
return tmpfl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-03-29 09:02:08 +00:00
|
|
|
|
} // namespace anon
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-12-05 12:57:01 +00:00
|
|
|
|
bool destroyDir(FileName const & tmpdir)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-10-20 08:42:16 +00:00
|
|
|
|
try {
|
2006-12-05 12:57:01 +00:00
|
|
|
|
return fs::remove_all(tmpdir.toFilesystemEncoding()) > 0;
|
2006-10-20 08:42:16 +00:00
|
|
|
|
} catch (fs::filesystem_error const & fe){
|
|
|
|
|
lyxerr << "Could not delete " << tmpdir << ". (" << fe.what() << ")" << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2001-03-20 01:22:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
string const createBufferTmpDir()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-06-04 23:57:32 +00:00
|
|
|
|
static int count;
|
2001-05-17 15:11:01 +00:00
|
|
|
|
// We are in our own directory. Why bother to mangle name?
|
2005-01-10 19:17:43 +00:00
|
|
|
|
// In fact I wrote this code to circumvent a problematic behaviour
|
|
|
|
|
// (bug?) of EMX mkstemp().
|
|
|
|
|
string const tmpfl =
|
|
|
|
|
package().temp_dir() + "/lyx_tmpbuf" +
|
|
|
|
|
convert<string>(count++);
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (mkdir(FileName(tmpfl), 0777)) {
|
2004-02-25 12:00:53 +00:00
|
|
|
|
lyxerr << "LyX could not create the temporary directory '"
|
|
|
|
|
<< tmpfl << "'" << endl;
|
2001-05-17 15:11:01 +00:00
|
|
|
|
return string();
|
|
|
|
|
}
|
|
|
|
|
return tmpfl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const createLyXTmpDir(FileName const & deflt)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (!deflt.empty() && deflt.absFilename() != "/tmp") {
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (mkdir(deflt, 0777)) {
|
2006-12-27 10:56:11 +00:00
|
|
|
|
if (isDirWriteable(deflt)) {
|
2004-02-25 12:00:53 +00:00
|
|
|
|
// deflt could not be created because it
|
|
|
|
|
// did exist already, so let's create our own
|
|
|
|
|
// dir inside deflt.
|
|
|
|
|
return createTmpDir(deflt, "lyx_tmpdir");
|
2005-01-16 16:53:08 +00:00
|
|
|
|
} else {
|
2004-02-25 12:00:53 +00:00
|
|
|
|
// some other error occured.
|
2006-11-26 21:30:39 +00:00
|
|
|
|
return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
|
2005-01-16 16:53:08 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else
|
2001-10-24 13:21:20 +00:00
|
|
|
|
return deflt;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
bool createDirectory(string const & path, int permission)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2005-07-14 22:09:22 +00:00
|
|
|
|
string temp = rtrim(os::internal_path(path), "/");
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(!temp.empty());
|
2006-11-26 21:30:39 +00:00
|
|
|
|
return mkdir(FileName(temp), permission) == 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Strip filename from path name
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const onlyPath(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// If empty filename, return empty
|
2005-07-14 22:09:22 +00:00
|
|
|
|
if (filename.empty())
|
|
|
|
|
return filename;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Find last / or start of filename
|
2005-07-14 22:09:22 +00:00
|
|
|
|
string::size_type j = filename.rfind('/');
|
|
|
|
|
return j == string::npos ? "./" : filename.substr(0, j + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2006-12-27 10:56:11 +00:00
|
|
|
|
FileName const makeAbsPath(string const & relPath, string const & basePath)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// checks for already absolute path
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (os::is_absolute_path(relPath))
|
2006-12-27 10:56:11 +00:00
|
|
|
|
return FileName(relPath);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Copies given paths
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string tempRel = os::internal_path(relPath);
|
2001-05-17 15:11:01 +00:00
|
|
|
|
// Since TempRel is NOT absolute, we can safely replace "//" with "/"
|
2006-04-08 22:31:11 +00:00
|
|
|
|
tempRel = subst(tempRel, "//", "/");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string tempBase;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (os::is_absolute_path(basePath))
|
|
|
|
|
tempBase = basePath;
|
2001-05-17 15:11:01 +00:00
|
|
|
|
else
|
2006-12-27 10:56:11 +00:00
|
|
|
|
tempBase = addPath(getcwd().absFilename(), basePath);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Handle /./ at the end of the path
|
2006-04-08 22:31:11 +00:00
|
|
|
|
while (suffixIs(tempBase, "/./"))
|
|
|
|
|
tempBase.erase(tempBase.length() - 2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// processes relative path
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string rTemp = tempRel;
|
|
|
|
|
string temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
while (!rTemp.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Split by next /
|
2006-04-08 22:31:11 +00:00
|
|
|
|
rTemp = split(rTemp, temp, '/');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (temp == ".") continue;
|
|
|
|
|
if (temp == "..") {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Remove one level of TempBase
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string::difference_type i = tempBase.length() - 2;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
if (i < 0)
|
|
|
|
|
i = 0;
|
2006-04-08 22:31:11 +00:00
|
|
|
|
while (i > 0 && tempBase[i] != '/')
|
2005-07-14 22:09:22 +00:00
|
|
|
|
--i;
|
1999-11-01 04:22:28 +00:00
|
|
|
|
if (i > 0)
|
2006-04-08 22:31:11 +00:00
|
|
|
|
tempBase.erase(i, string::npos);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
else
|
2006-04-08 22:31:11 +00:00
|
|
|
|
tempBase += '/';
|
|
|
|
|
} else if (temp.empty() && !rTemp.empty()) {
|
|
|
|
|
tempBase = os::current_root() + rTemp;
|
|
|
|
|
rTemp.erase();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Add this piece to TempBase
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (!suffixIs(tempBase, '/'))
|
|
|
|
|
tempBase += '/';
|
|
|
|
|
tempBase += temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns absolute path
|
2006-12-27 10:56:11 +00:00
|
|
|
|
return FileName(os::internal_path(tempBase));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Correctly append filename to the pathname.
|
|
|
|
|
// If pathname is '.', then don't use pathname.
|
|
|
|
|
// Chops any path of filename.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const addName(string const & path, string const & fname)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const basename = onlyFilename(fname);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
if (path != "." && path != "./" && !path.empty()) {
|
2004-12-20 16:59:33 +00:00
|
|
|
|
buf = os::internal_path(path);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
if (!suffixIs(path, '/'))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
buf += '/';
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
return buf + basename;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Strips path from filename
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const onlyFilename(string const & fname)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-10-20 14:35:05 +00:00
|
|
|
|
if (fname.empty())
|
|
|
|
|
return fname;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string::size_type j = fname.rfind('/');
|
|
|
|
|
if (j == string::npos) // no '/' in fname
|
|
|
|
|
return fname;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Strip to basename
|
1999-10-02 16:21:10 +00:00
|
|
|
|
return fname.substr(j + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-10-08 14:09:06 +00:00
|
|
|
|
/// Returns true is path is absolute
|
2006-04-08 22:31:11 +00:00
|
|
|
|
bool absolutePath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-10-08 14:09:06 +00:00
|
|
|
|
return os::is_absolute_path(path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create absolute path. If impossible, don't do anything
|
|
|
|
|
// Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const expandPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// checks for already absolute path
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string rTemp = replaceEnvironmentPath(path);
|
|
|
|
|
if (os::is_absolute_path(rTemp))
|
|
|
|
|
return rTemp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string temp;
|
|
|
|
|
string const copy = rTemp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Split by next /
|
2006-04-08 22:31:11 +00:00
|
|
|
|
rTemp = split(rTemp, temp, '/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (temp == ".")
|
2006-12-27 10:56:11 +00:00
|
|
|
|
return getcwd().absFilename() + '/' + rTemp;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (temp == "~")
|
|
|
|
|
return package().home_dir() + '/' + rTemp;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
if (temp == "..")
|
2006-12-27 10:56:11 +00:00
|
|
|
|
return makeAbsPath(copy).absFilename();
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
2001-10-24 13:21:20 +00:00
|
|
|
|
// Don't know how to handle this
|
|
|
|
|
return copy;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
// Normalize a path. Constracts path/../path
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Can't handle "../../" or "/../" (Asger)
|
2003-06-20 14:03:49 +00:00
|
|
|
|
// Also converts paths like /foo//bar ==> /foo/bar
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const normalizePath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-03-05 18:11:11 +00:00
|
|
|
|
// Normalize paths like /foo//bar ==> /foo/bar
|
|
|
|
|
static boost::regex regex("/{2,}");
|
|
|
|
|
string const tmppath = boost::regex_merge(path, regex, "/");
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-03-05 18:11:11 +00:00
|
|
|
|
fs::path const npath = fs::path(tmppath, fs::no_check).normalize();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-03-05 18:11:11 +00:00
|
|
|
|
if (!npath.is_complete())
|
|
|
|
|
return "./" + npath.string() + '/';
|
2006-04-05 23:39:11 +00:00
|
|
|
|
|
2006-03-05 18:11:11 +00:00
|
|
|
|
return npath.string() + '/';
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const getFileContents(FileName const & fname)
|
2000-08-05 05:17:18 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const encodedname = fname.toFilesystemEncoding();
|
|
|
|
|
if (fs::exists(encodedname)) {
|
|
|
|
|
ifstream ifs(encodedname.c_str());
|
2001-07-13 14:03:48 +00:00
|
|
|
|
ostringstream ofs;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
if (ifs && ofs) {
|
|
|
|
|
ofs << ifs.rdbuf();
|
|
|
|
|
ifs.close();
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return ofs.str();
|
2000-06-12 11:27:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
|
lyxerr << "LyX was not able to read file '" << fname << '\'' << endl;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
return string();
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-09-29 21:44:34 +00:00
|
|
|
|
// Search the string for ${VAR} and $VAR and replace VAR using getenv.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const replaceEnvironmentPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-09-29 21:44:34 +00:00
|
|
|
|
// ${VAR} is defined as
|
2003-09-25 23:01:43 +00:00
|
|
|
|
// $\{[A-Za-z_][A-Za-z_0-9]*\}
|
2003-10-16 16:41:32 +00:00
|
|
|
|
static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
|
2003-09-25 23:01:43 +00:00
|
|
|
|
|
2003-09-29 21:44:34 +00:00
|
|
|
|
// $VAR is defined as:
|
|
|
|
|
// $\{[A-Za-z_][A-Za-z_0-9]*\}
|
2003-10-16 16:41:32 +00:00
|
|
|
|
static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
|
2003-09-29 21:44:34 +00:00
|
|
|
|
|
2003-10-16 16:41:32 +00:00
|
|
|
|
static boost::regex envvar_br_re("(.*)" + envvar_br + "(.*)");
|
|
|
|
|
static boost::regex envvar_re("(.*)" + envvar + "(.*)");
|
2003-09-25 23:01:43 +00:00
|
|
|
|
boost::smatch what;
|
|
|
|
|
|
|
|
|
|
string result = path;
|
|
|
|
|
while (1) {
|
2003-09-29 21:44:34 +00:00
|
|
|
|
regex_match(result, what, envvar_br_re);
|
|
|
|
|
if (!what[0].matched) {
|
|
|
|
|
regex_match(result, what, envvar_re);
|
|
|
|
|
if (!what[0].matched)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-15 13:45:41 +00:00
|
|
|
|
result = what.str(1) + getEnv(what.str(2)) + what.str(3);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
}
|
2003-09-25 23:01:43 +00:00
|
|
|
|
return result;
|
2002-11-06 11:15:24 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make relative path out of two absolute paths
|
2007-03-27 07:24:16 +00:00
|
|
|
|
docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Makes relative path out of absolute path. If it is deeper than basepath,
|
|
|
|
|
// it's easy. If basepath and abspath share something (they are all deeper
|
|
|
|
|
// than some directory), it'll be rendered using ..'s. If they are completely
|
|
|
|
|
// different, then the absolute path will be used as relative path.
|
|
|
|
|
{
|
2007-03-27 07:24:16 +00:00
|
|
|
|
docstring::size_type const abslen = abspath.length();
|
|
|
|
|
docstring::size_type const baselen = basepath.length();
|
2001-05-17 15:11:01 +00:00
|
|
|
|
|
2007-03-27 07:24:16 +00:00
|
|
|
|
docstring::size_type i = os::common_path(abspath, basepath);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
// actually no match - cannot make it relative
|
|
|
|
|
return abspath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count how many dirs there are in basepath above match
|
|
|
|
|
// and append as many '..''s into relpath
|
2007-03-27 07:24:16 +00:00
|
|
|
|
docstring buf;
|
|
|
|
|
docstring::size_type j = i;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
while (j < baselen) {
|
|
|
|
|
if (basepath[j] == '/') {
|
2001-10-24 13:21:20 +00:00
|
|
|
|
if (j + 1 == baselen)
|
|
|
|
|
break;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
buf += "../";
|
|
|
|
|
}
|
|
|
|
|
++j;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Append relative stuff from common directory to abspath
|
2001-10-24 13:21:20 +00:00
|
|
|
|
if (abspath[i] == '/')
|
|
|
|
|
++i;
|
1999-12-07 00:44:53 +00:00
|
|
|
|
for (; i < abslen; ++i)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
buf += abspath[i];
|
|
|
|
|
// Remove trailing /
|
1999-10-02 16:21:10 +00:00
|
|
|
|
if (suffixIs(buf, '/'))
|
|
|
|
|
buf.erase(buf.length() - 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Substitute empty with .
|
|
|
|
|
if (buf.empty())
|
|
|
|
|
buf = '.';
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append sub-directory(ies) to a path in an intelligent way
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const addPath(string const & path, string const & path_2)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string buf;
|
2004-12-20 16:59:33 +00:00
|
|
|
|
string const path2 = os::internal_path(path_2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
if (!path.empty() && path != "." && path != "./") {
|
2004-12-20 16:59:33 +00:00
|
|
|
|
buf = os::internal_path(path);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
if (path[path.length() - 1] != '/')
|
1999-09-27 18:44:28 +00:00
|
|
|
|
buf += '/';
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-17 15:11:01 +00:00
|
|
|
|
if (!path2.empty()) {
|
2002-03-21 17:09:55 +00:00
|
|
|
|
string::size_type const p2start = path2.find_first_not_of('/');
|
2001-05-17 15:11:01 +00:00
|
|
|
|
string::size_type const p2end = path2.find_last_not_of('/');
|
|
|
|
|
string const tmp = path2.substr(p2start, p2end - p2start + 1);
|
|
|
|
|
buf += tmp + '/';
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const changeExtension(string const & oldname, string const & extension)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string::size_type const last_slash = oldname.rfind('/');
|
2000-02-07 20:17:03 +00:00
|
|
|
|
string::size_type last_dot = oldname.rfind('.');
|
|
|
|
|
if (last_dot < last_slash && last_slash != string::npos)
|
|
|
|
|
last_dot = string::npos;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string ext;
|
|
|
|
|
// Make sure the extension starts with a dot
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!extension.empty() && extension[0] != '.')
|
2002-11-27 10:30:28 +00:00
|
|
|
|
ext= '.' + extension;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
else
|
|
|
|
|
ext = extension;
|
2000-05-11 16:12:46 +00:00
|
|
|
|
|
2004-12-20 16:59:33 +00:00
|
|
|
|
return os::internal_path(oldname.substr(0, last_dot) + ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
|
2006-04-01 10:49:58 +00:00
|
|
|
|
string const removeExtension(string const & name)
|
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
return changeExtension(name, string());
|
2006-04-01 10:49:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-21 12:04:01 +00:00
|
|
|
|
string const addExtension(string const & name, string const & extension)
|
|
|
|
|
{
|
|
|
|
|
if (!extension.empty() && extension[0] != '.')
|
|
|
|
|
return name + '.' + extension;
|
|
|
|
|
return name + extension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-08-30 03:40:51 +00:00
|
|
|
|
/// Return the extension of the file (not including the .)
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const getExtension(string const & name)
|
2000-08-30 03:40:51 +00:00
|
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string::size_type const last_slash = name.rfind('/');
|
|
|
|
|
string::size_type const last_dot = name.rfind('.');
|
2000-08-30 03:40:51 +00:00
|
|
|
|
if (last_dot != string::npos &&
|
|
|
|
|
(last_slash == string::npos || last_dot > last_slash))
|
|
|
|
|
return name.substr(last_dot + 1,
|
|
|
|
|
name.length() - (last_dot + 1));
|
|
|
|
|
else
|
|
|
|
|
return string();
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2004-10-29 15:47:55 +00:00
|
|
|
|
|
2002-01-31 14:20:09 +00:00
|
|
|
|
// the different filetypes and what they contain in one of the first lines
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// (dots are any characters). (Herbert 20020131)
|
2002-03-01 09:58:15 +00:00
|
|
|
|
// AGR Grace...
|
|
|
|
|
// BMP BM...
|
2002-01-31 14:20:09 +00:00
|
|
|
|
// EPS %!PS-Adobe-3.0 EPSF...
|
2002-07-04 09:58:16 +00:00
|
|
|
|
// FIG #FIG...
|
2002-03-01 09:58:15 +00:00
|
|
|
|
// FITS ...BITPIX...
|
2002-01-31 14:20:09 +00:00
|
|
|
|
// GIF GIF...
|
|
|
|
|
// JPG JFIF
|
|
|
|
|
// PDF %PDF-...
|
|
|
|
|
// PNG .PNG...
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// PBM P1... or P4 (B/W)
|
2002-03-01 09:58:15 +00:00
|
|
|
|
// PGM P2... or P5 (Grayscale)
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// PPM P3... or P6 (color)
|
2002-02-07 19:37:34 +00:00
|
|
|
|
// PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
|
2002-03-01 09:58:15 +00:00
|
|
|
|
// SGI \001\332... (decimal 474)
|
|
|
|
|
// TGIF %TGIF...
|
|
|
|
|
// TIFF II... or MM...
|
2002-03-04 10:37:25 +00:00
|
|
|
|
// XBM ..._bits[]...
|
2002-04-07 15:31:16 +00:00
|
|
|
|
// XPM /* XPM */ sometimes missing (f.ex. tgif-export)
|
|
|
|
|
// ...static char *...
|
2002-03-04 10:37:25 +00:00
|
|
|
|
// XWD \000\000\000\151 (0x00006900) decimal 105
|
2002-02-05 12:19:32 +00:00
|
|
|
|
//
|
2003-07-28 12:47:20 +00:00
|
|
|
|
// GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
|
2002-02-05 12:19:32 +00:00
|
|
|
|
// ZIP PK... http://www.halyava.ru/document/ind_arch.htm
|
2003-07-28 12:47:20 +00:00
|
|
|
|
// Z \037\235 UNIX compress
|
2002-02-20 16:34:53 +00:00
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const getFormatFromContents(FileName const & filename)
|
2002-04-06 12:42:42 +00:00
|
|
|
|
{
|
|
|
|
|
// paranoia check
|
2006-04-08 09:09:57 +00:00
|
|
|
|
if (filename.empty() || !isFileReadable(filename))
|
2002-03-01 15:28:02 +00:00
|
|
|
|
return string();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
ifstream ifs(filename.toFilesystemEncoding().c_str());
|
2002-03-01 15:28:02 +00:00
|
|
|
|
if (!ifs)
|
|
|
|
|
// Couldn't open file...
|
|
|
|
|
return string();
|
|
|
|
|
|
|
|
|
|
// gnuzip
|
2005-07-14 22:09:22 +00:00
|
|
|
|
static string const gzipStamp = "\037\213";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
// PKZIP
|
2005-07-14 22:09:22 +00:00
|
|
|
|
static string const zipStamp = "PK";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
// compress
|
2005-07-14 22:09:22 +00:00
|
|
|
|
static string const compressStamp = "\037\235";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
// Maximum strings to read
|
|
|
|
|
int const max_count = 50;
|
2002-03-04 10:37:25 +00:00
|
|
|
|
int count = 0;
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2004-11-07 12:44:10 +00:00
|
|
|
|
string str;
|
|
|
|
|
string format;
|
2002-03-01 09:58:15 +00:00
|
|
|
|
bool firstLine = true;
|
2002-03-04 10:37:25 +00:00
|
|
|
|
while ((count++ < max_count) && format.empty()) {
|
2002-01-31 14:20:09 +00:00
|
|
|
|
if (ifs.eof()) {
|
2002-03-01 15:28:02 +00:00
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
2004-10-29 15:47:55 +00:00
|
|
|
|
<< "filetools(getFormatFromContents)\n"
|
2002-03-01 15:28:02 +00:00
|
|
|
|
<< "\tFile type not recognised before EOF!"
|
|
|
|
|
<< endl;
|
2002-01-31 14:20:09 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-05-02 10:46:51 +00:00
|
|
|
|
getline(ifs, str);
|
2005-07-14 22:09:22 +00:00
|
|
|
|
string const stamp = str.substr(0, 2);
|
2002-03-01 15:28:02 +00:00
|
|
|
|
if (firstLine && str.size() >= 2) {
|
|
|
|
|
// at first we check for a zipped file, because this
|
|
|
|
|
// information is saved in the first bytes of the file!
|
|
|
|
|
// also some graphic formats which save the information
|
|
|
|
|
// in the first line, too.
|
2002-04-08 14:55:52 +00:00
|
|
|
|
if (prefixIs(str, gzipStamp)) {
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "gzip";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-04-08 14:55:52 +00:00
|
|
|
|
} else if (stamp == zipStamp) {
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "zip";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-04-08 14:55:52 +00:00
|
|
|
|
} else if (stamp == compressStamp) {
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "compress";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
// the graphics part
|
2002-04-08 14:55:52 +00:00
|
|
|
|
} else if (stamp == "BM") {
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "bmp";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-04-08 14:55:52 +00:00
|
|
|
|
} else if (stamp == "\001\332") {
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "sgi";
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
2002-03-01 15:28:02 +00:00
|
|
|
|
// PBM family
|
2002-03-04 10:37:25 +00:00
|
|
|
|
// Don't need to use str.at(0), str.at(1) because
|
2002-03-01 15:28:02 +00:00
|
|
|
|
// we already know that str.size() >= 2
|
2002-04-08 14:55:52 +00:00
|
|
|
|
} else if (str[0] == 'P') {
|
2002-03-01 15:28:02 +00:00
|
|
|
|
switch (str[1]) {
|
|
|
|
|
case '1':
|
|
|
|
|
case '4':
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "pbm";
|
|
|
|
|
break;
|
2002-03-01 15:28:02 +00:00
|
|
|
|
case '2':
|
|
|
|
|
case '5':
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "pgm";
|
|
|
|
|
break;
|
2002-03-01 15:28:02 +00:00
|
|
|
|
case '3':
|
|
|
|
|
case '6':
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "ppm";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
}
|
2002-03-04 10:37:25 +00:00
|
|
|
|
break;
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
|
|
|
|
} else if ((stamp == "II") || (stamp == "MM")) {
|
|
|
|
|
format = "tiff";
|
|
|
|
|
|
2002-04-08 16:52:33 +00:00
|
|
|
|
} else if (prefixIs(str,"%TGIF")) {
|
2002-04-08 14:55:52 +00:00
|
|
|
|
format = "tgif";
|
|
|
|
|
|
2002-07-04 09:58:16 +00:00
|
|
|
|
} else if (prefixIs(str,"#FIG")) {
|
|
|
|
|
format = "fig";
|
|
|
|
|
|
2002-04-08 14:55:52 +00:00
|
|
|
|
} else if (prefixIs(str,"GIF")) {
|
|
|
|
|
format = "gif";
|
|
|
|
|
|
|
|
|
|
} else if (str.size() > 3) {
|
|
|
|
|
int const c = ((str[0] << 24) & (str[1] << 16) &
|
|
|
|
|
(str[2] << 8) & str[3]);
|
|
|
|
|
if (c == 105) {
|
|
|
|
|
format = "xwd";
|
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
}
|
2002-05-01 22:17:09 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
firstLine = false;
|
2002-02-05 12:19:32 +00:00
|
|
|
|
}
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
if (!format.empty())
|
|
|
|
|
break;
|
|
|
|
|
else if (contains(str,"EPSF"))
|
2002-03-01 15:28:02 +00:00
|
|
|
|
// dummy, if we have wrong file description like
|
2002-04-08 16:52:33 +00:00
|
|
|
|
// %!PS-Adobe-2.0EPSF"
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "eps";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"Grace"))
|
|
|
|
|
format = "agr";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"JFIF"))
|
|
|
|
|
format = "jpg";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"%PDF"))
|
|
|
|
|
format = "pdf";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"PNG"))
|
|
|
|
|
format = "png";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"%!PS-Adobe")) {
|
2002-03-01 15:28:02 +00:00
|
|
|
|
// eps or ps
|
|
|
|
|
ifs >> str;
|
2002-07-28 18:13:51 +00:00
|
|
|
|
if (contains(str,"EPSF"))
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "eps";
|
|
|
|
|
else
|
|
|
|
|
format = "ps";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"_bits[]"))
|
|
|
|
|
format = "xbm";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-04-07 15:31:16 +00:00
|
|
|
|
else if (contains(str,"XPM") || contains(str, "static char *"))
|
2002-03-04 10:37:25 +00:00
|
|
|
|
format = "xpm";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
else if (contains(str,"BITPIX"))
|
|
|
|
|
format = "fits";
|
2002-01-31 14:20:09 +00:00
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2002-03-04 10:37:25 +00:00
|
|
|
|
if (!format.empty()) {
|
2002-06-18 20:47:49 +00:00
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
|
<< "Recognised Fileformat: " << format << endl;
|
|
|
|
|
return format;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-01 15:28:02 +00:00
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
2004-10-29 15:47:55 +00:00
|
|
|
|
<< "filetools(getFormatFromContents)\n"
|
|
|
|
|
<< "\tCouldn't find a known format!\n";
|
2004-11-09 19:08:34 +00:00
|
|
|
|
return string();
|
2002-01-31 14:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
|
2002-02-05 12:19:32 +00:00
|
|
|
|
/// check for zipped file
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool zippedFile(FileName const & name)
|
2002-02-16 18:34:30 +00:00
|
|
|
|
{
|
2004-10-29 15:47:55 +00:00
|
|
|
|
string const type = getFormatFromContents(name);
|
2002-02-16 18:34:30 +00:00
|
|
|
|
if (contains("gzip zip compress", type) && !type.empty())
|
|
|
|
|
return true;
|
2002-02-05 12:19:32 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-16 18:34:30 +00:00
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
|
string const unzippedFileName(string const & zipped_file)
|
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const ext = getExtension(zipped_file);
|
2003-09-03 17:23:38 +00:00
|
|
|
|
if (ext == "gz" || ext == "z" || ext == "Z")
|
2006-04-08 22:31:11 +00:00
|
|
|
|
return changeExtension(zipped_file, string());
|
2003-09-03 17:23:38 +00:00
|
|
|
|
return "unzipped_" + zipped_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
|
2002-02-07 19:37:34 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const tempfile = FileName(unzipped_file.empty() ?
|
|
|
|
|
unzippedFileName(zipped_file.toFilesystemEncoding()) :
|
|
|
|
|
unzipped_file);
|
2002-02-16 18:34:30 +00:00
|
|
|
|
// Run gunzip
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const command = "gunzip -c " +
|
|
|
|
|
zipped_file.toFilesystemEncoding() + " > " +
|
|
|
|
|
tempfile.toFilesystemEncoding();
|
2002-02-18 19:13:48 +00:00
|
|
|
|
Systemcall one;
|
|
|
|
|
one.startscript(Systemcall::Wait, command);
|
2002-02-16 18:34:30 +00:00
|
|
|
|
// test that command was executed successfully (anon)
|
|
|
|
|
// yes, please do. (Lgb)
|
|
|
|
|
return tempfile;
|
2002-02-07 19:37:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const makeDisplayPath(string const & path, unsigned int threshold)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-04-08 00:06:52 +00:00
|
|
|
|
string str = path;
|
2007-02-28 21:48:04 +00:00
|
|
|
|
|
|
|
|
|
// If file is from LyXDir, display it as if it were relative.
|
|
|
|
|
string const system = package().system_support();
|
2007-03-14 00:36:25 +00:00
|
|
|
|
if (prefixIs(str, system) && str != system)
|
2007-02-28 21:48:04 +00:00
|
|
|
|
return from_utf8("[" + str.erase(0, system.length()) + "]");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
// replace /home/blah with ~/
|
2007-02-28 21:48:04 +00:00
|
|
|
|
string const home = package().home_dir();
|
2005-09-07 09:44:58 +00:00
|
|
|
|
if (!home.empty() && prefixIs(str, home))
|
2003-04-08 00:06:52 +00:00
|
|
|
|
str = subst(str, home, "~");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
if (str.length() <= threshold)
|
2007-01-18 20:47:27 +00:00
|
|
|
|
return from_utf8(os::external_path(str));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
string const prefix = ".../";
|
|
|
|
|
string temp;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
while (str.length() > threshold)
|
|
|
|
|
str = split(str, temp, '/');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
// Did we shorten everything away?
|
|
|
|
|
if (str.empty()) {
|
|
|
|
|
// Yes, filename itself is too long.
|
|
|
|
|
// Pick the start and the end of the filename.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
str = onlyFilename(path);
|
2003-04-08 00:06:52 +00:00
|
|
|
|
string const head = str.substr(0, threshold / 2 - 3);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
string::size_type len = str.length();
|
|
|
|
|
string const tail =
|
|
|
|
|
str.substr(len - threshold / 2 - 2, len - 1);
|
|
|
|
|
str = head + "..." + tail;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2003-04-08 00:06:52 +00:00
|
|
|
|
|
2007-01-18 20:47:27 +00:00
|
|
|
|
return from_utf8(os::external_path(prefix + str));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 06:42:25 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
bool readLink(string const & file, string & link, bool resolve)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2005-01-04 13:24:56 +00:00
|
|
|
|
#ifdef HAVE_READLINK
|
2002-07-17 21:43:06 +00:00
|
|
|
|
char linkbuffer[512];
|
1999-12-07 00:44:53 +00:00
|
|
|
|
// Should be PATH_MAX but that needs autconf support
|
2002-07-17 21:43:06 +00:00
|
|
|
|
int const nRead = ::readlink(file.c_str(),
|
|
|
|
|
linkbuffer, sizeof(linkbuffer) - 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (nRead <= 0)
|
|
|
|
|
return false;
|
2002-07-17 21:43:06 +00:00
|
|
|
|
linkbuffer[nRead] = '\0'; // terminator
|
|
|
|
|
if (resolve)
|
2006-12-27 10:56:11 +00:00
|
|
|
|
link = makeAbsPath(linkbuffer, onlyPath(file)).absFilename();
|
2002-07-17 21:43:06 +00:00
|
|
|
|
else
|
|
|
|
|
link = linkbuffer;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return true;
|
2005-01-04 13:24:56 +00:00
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
cmd_ret const runCommand(string const & cmd)
|
1999-11-24 22:14:46 +00:00
|
|
|
|
{
|
2004-04-05 08:52:45 +00:00
|
|
|
|
// FIXME: replace all calls to RunCommand with ForkedCall
|
2004-04-05 06:46:22 +00:00
|
|
|
|
// (if the output is not needed) or the code in ispell.C
|
|
|
|
|
// (if the output is needed).
|
2004-04-20 08:51:15 +00:00
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
// One question is if we should use popen or
|
|
|
|
|
// create our own popen based on fork, exec, pipe
|
|
|
|
|
// of course the best would be to have a
|
|
|
|
|
// pstream (process stream), with the
|
2000-01-06 02:44:26 +00:00
|
|
|
|
// variants ipstream, opstream
|
2002-02-08 11:23:58 +00:00
|
|
|
|
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#if defined (HAVE_POPEN)
|
2002-09-23 16:03:11 +00:00
|
|
|
|
FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#elif defined (HAVE__POPEN)
|
|
|
|
|
FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
|
|
|
|
|
#else
|
|
|
|
|
#error No popen() function.
|
|
|
|
|
#endif
|
2002-02-08 11:23:58 +00:00
|
|
|
|
|
|
|
|
|
// (Claus Hentschel) Check if popen was succesful ;-)
|
2004-04-05 06:46:22 +00:00
|
|
|
|
if (!inf) {
|
|
|
|
|
lyxerr << "RunCommand:: could not start child process" << endl;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
return make_pair(-1, string());
|
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
string ret;
|
|
|
|
|
int c = fgetc(inf);
|
|
|
|
|
while (c != EOF) {
|
|
|
|
|
ret += static_cast<char>(c);
|
|
|
|
|
c = fgetc(inf);
|
|
|
|
|
}
|
2005-05-03 20:32:55 +00:00
|
|
|
|
|
|
|
|
|
#if defined (HAVE_PCLOSE)
|
2000-11-14 02:01:57 +00:00
|
|
|
|
int const pret = pclose(inf);
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#elif defined (HAVE__PCLOSE)
|
|
|
|
|
int const pret = _pclose(inf);
|
|
|
|
|
#else
|
|
|
|
|
#error No pclose() function.
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-04-05 06:46:22 +00:00
|
|
|
|
if (pret == -1)
|
|
|
|
|
perror("RunCommand:: could not terminate child process");
|
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
return make_pair(pret, ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-01 20:09:08 +00:00
|
|
|
|
FileName const findtexfile(string const & fil, string const & /*format*/)
|
1999-11-24 22:14:46 +00:00
|
|
|
|
{
|
|
|
|
|
/* There is no problem to extend this function too 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
|
|
|
|
|
perhaps be to use the LyX file lookup methods. But! I am
|
|
|
|
|
going to implement this until I see some demand for it.
|
|
|
|
|
Lgb
|
|
|
|
|
*/
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2000-03-01 18:15:39 +00:00
|
|
|
|
// If the file can be found directly, we just return a
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// absolute path version of it.
|
2006-12-01 20:09:08 +00:00
|
|
|
|
FileName const absfile(makeAbsPath(fil));
|
|
|
|
|
if (fs::exists(absfile.toFilesystemEncoding()))
|
|
|
|
|
return absfile;
|
2000-03-01 18:15:39 +00:00
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// No we try to find it using kpsewhich.
|
2000-03-02 02:19:43 +00:00
|
|
|
|
// 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
|
|
|
|
|
// from the name given, by looking for a known extension. If no
|
|
|
|
|
// known extension is found, the search path for TeX source files
|
|
|
|
|
// is used."
|
|
|
|
|
// However, we want to take advantage of the format sine almost all
|
|
|
|
|
// the different formats has environment variables that can be used
|
|
|
|
|
// to controll which paths to search. f.ex. bib looks in
|
|
|
|
|
// BIBINPUTS and TEXBIB. Small list follows:
|
|
|
|
|
// bib - BIBINPUTS, TEXBIB
|
|
|
|
|
// bst - BSTINPUTS
|
|
|
|
|
// graphic/figure - TEXPICTS, TEXINPUTS
|
|
|
|
|
// ist - TEXINDEXSTYLE, INDEXSTYLE
|
|
|
|
|
// pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
|
|
|
|
|
// tex - TEXINPUTS
|
|
|
|
|
// tfm - TFMFONTS, TEXFONTS
|
|
|
|
|
// This means that to use kpsewhich in the best possible way we
|
|
|
|
|
// should help it by setting additional path in the approp. envir.var.
|
2001-10-24 13:21:20 +00:00
|
|
|
|
string const kpsecmd = "kpsewhich " + fil;
|
2000-01-12 11:16:01 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
cmd_ret const c = runCommand(kpsecmd);
|
2001-10-24 13:21:20 +00:00
|
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
|
lyxerr[Debug::LATEX] << "kpse status = " << c.first << '\n'
|
2006-12-27 10:56:11 +00:00
|
|
|
|
<< "kpse result = `" << rtrim(c.second, "\n\r")
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< '\'' << endl;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
if (c.first != -1)
|
2007-01-18 20:47:27 +00:00
|
|
|
|
return FileName(os::internal_path(rtrim(to_utf8(from_filesystem8bit(c.second)),
|
|
|
|
|
"\n\r")));
|
2001-06-21 10:55:48 +00:00
|
|
|
|
else
|
2006-12-01 20:09:08 +00:00
|
|
|
|
return FileName();
|
1999-11-24 22:14:46 +00:00
|
|
|
|
}
|
2000-08-08 15:36:25 +00:00
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
|
2000-08-08 15:36:25 +00:00
|
|
|
|
void removeAutosaveFile(string const & filename)
|
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string a = onlyPath(filename);
|
2000-08-08 15:36:25 +00:00
|
|
|
|
a += '#';
|
2006-04-08 22:31:11 +00:00
|
|
|
|
a += onlyFilename(filename);
|
2000-08-08 15:36:25 +00:00
|
|
|
|
a += '#';
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const autosave(a);
|
|
|
|
|
if (fs::exists(autosave.toFilesystemEncoding()))
|
|
|
|
|
unlink(autosave);
|
2000-08-08 15:36:25 +00:00
|
|
|
|
}
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
void readBB_lyxerrMessage(FileName const & file, bool & zipped,
|
2002-07-28 18:13:51 +00:00
|
|
|
|
string const & message)
|
2002-06-18 20:47:49 +00:00
|
|
|
|
{
|
2002-07-28 18:13:51 +00:00
|
|
|
|
lyxerr[Debug::GRAPHICS] << "[readBB_from_PSFile] "
|
2002-06-18 20:47:49 +00:00
|
|
|
|
<< message << std::endl;
|
2004-04-05 09:36:28 +00:00
|
|
|
|
#ifdef WITH_WARNINGS
|
2003-06-30 23:56:22 +00:00
|
|
|
|
#warning Why is this func deleting a file? (Lgb)
|
2004-04-05 09:36:28 +00:00
|
|
|
|
#endif
|
2002-06-18 20:47:49 +00:00
|
|
|
|
if (zipped)
|
2003-06-30 23:56:22 +00:00
|
|
|
|
unlink(file);
|
2002-06-18 20:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const readBB_from_PSFile(FileName const & file)
|
2002-04-08 14:55:52 +00:00
|
|
|
|
{
|
|
|
|
|
// in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
|
|
|
|
|
// It seems that every command in the header has an own line,
|
|
|
|
|
// getline() should work for all files.
|
|
|
|
|
// On the other hand some plot programs write the bb at the
|
|
|
|
|
// end of the file. Than we have in the header:
|
|
|
|
|
// %%BoundingBox: (atend)
|
|
|
|
|
// In this case we must check the end.
|
2002-06-18 20:47:49 +00:00
|
|
|
|
bool zipped = zippedFile(file);
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const file_ = zipped ? unzipFile(file) : file;
|
2004-10-29 15:47:55 +00:00
|
|
|
|
string const format = getFormatFromContents(file_);
|
2002-06-18 20:47:49 +00:00
|
|
|
|
|
|
|
|
|
if (format != "eps" && format != "ps") {
|
2002-07-28 18:13:51 +00:00
|
|
|
|
readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
|
2002-04-08 14:55:52 +00:00
|
|
|
|
return string();
|
2002-06-18 20:47:49 +00:00
|
|
|
|
}
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
2006-04-05 08:19:48 +00:00
|
|
|
|
static boost::regex bbox_re(
|
|
|
|
|
"^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
|
2006-11-26 21:30:39 +00:00
|
|
|
|
std::ifstream is(file_.toFilesystemEncoding().c_str());
|
2002-04-08 14:55:52 +00:00
|
|
|
|
while (is) {
|
|
|
|
|
string s;
|
2002-05-02 10:46:51 +00:00
|
|
|
|
getline(is,s);
|
2006-04-05 08:19:48 +00:00
|
|
|
|
boost::smatch what;
|
|
|
|
|
if (regex_match(s, what, bbox_re)) {
|
|
|
|
|
// Our callers expect the tokens in the string
|
|
|
|
|
// separated by single spaces.
|
|
|
|
|
// FIXME: change return type from string to something
|
|
|
|
|
// sensible
|
|
|
|
|
ostringstream os;
|
|
|
|
|
os << what.str(1) << ' ' << what.str(2) << ' '
|
|
|
|
|
<< what.str(3) << ' ' << what.str(4);
|
|
|
|
|
string const bb = os.str();
|
2002-06-18 20:47:49 +00:00
|
|
|
|
readBB_lyxerrMessage(file_, zipped, bb);
|
|
|
|
|
return bb;
|
|
|
|
|
}
|
2002-04-08 14:55:52 +00:00
|
|
|
|
}
|
2002-06-18 20:47:49 +00:00
|
|
|
|
readBB_lyxerrMessage(file_, zipped, "no bb found");
|
2002-04-08 14:55:52 +00:00
|
|
|
|
return string();
|
|
|
|
|
}
|
2003-06-03 10:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
int compare_timestamps(FileName const & filename1, FileName const & filename2)
|
2003-06-03 10:14:52 +00:00
|
|
|
|
{
|
|
|
|
|
// If the original is newer than the copy, then copy the original
|
|
|
|
|
// to the new directory.
|
2003-09-03 17:23:38 +00:00
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
string const file1 = filename1.toFilesystemEncoding();
|
|
|
|
|
string const file2 = filename2.toFilesystemEncoding();
|
2003-09-03 17:23:38 +00:00
|
|
|
|
int cmp = 0;
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (fs::exists(file1) && fs::exists(file2)) {
|
|
|
|
|
double const tmp = difftime(fs::last_write_time(file1),
|
|
|
|
|
fs::last_write_time(file2));
|
2003-09-03 17:23:38 +00:00
|
|
|
|
if (tmp != 0)
|
|
|
|
|
cmp = tmp > 0 ? 1 : -1;
|
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
} else if (fs::exists(file1)) {
|
2003-09-03 17:23:38 +00:00
|
|
|
|
cmp = 1;
|
2005-01-31 10:42:26 +00:00
|
|
|
|
} else if (fs::exists(file2)) {
|
2003-09-03 17:23:38 +00:00
|
|
|
|
cmp = -1;
|
2003-06-03 10:14:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
|
return cmp;
|
2003-06-03 10:14:52 +00:00
|
|
|
|
}
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
|
|
|
|
} //namespace support
|
|
|
|
|
} // namespace lyx
|