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>
|
|
|
|
|
|
2003-05-13 14:36:24 +00:00
|
|
|
|
#include "support/tostr.h"
|
2002-02-18 19:13:48 +00:00
|
|
|
|
#include "support/systemcall.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/FileInfo.h"
|
|
|
|
|
#include "support/forkedcontr.h"
|
|
|
|
|
#include "support/path.h"
|
|
|
|
|
#include "support/path_defines.h"
|
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
#include "support/os.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>
|
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>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <fstream>
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
2002-06-10 07:57:39 +00:00
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Which part of this is still necessary? (JMarc).
|
|
|
|
|
#if HAVE_DIRENT_H
|
|
|
|
|
# include <dirent.h>
|
|
|
|
|
# define NAMLEN(dirent) strlen((dirent)->d_name)
|
|
|
|
|
#else
|
|
|
|
|
# define dirent direct
|
|
|
|
|
# define NAMLEN(dirent) (dirent)->d_namlen
|
|
|
|
|
# if HAVE_SYS_NDIR_H
|
|
|
|
|
# include <sys/ndir.h>
|
|
|
|
|
# endif
|
|
|
|
|
# if HAVE_SYS_DIR_H
|
|
|
|
|
# include <sys/dir.h>
|
|
|
|
|
# endif
|
|
|
|
|
# if HAVE_NDIR_H
|
|
|
|
|
# include <ndir.h>
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
1999-10-02 16:21:10 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-11-13 15:43:36 +00:00
|
|
|
|
bool IsSGMLFilename(string const & filename)
|
|
|
|
|
{
|
2002-07-16 21:17:10 +00:00
|
|
|
|
return suffixIs(ascii_lowercase(filename), ".sgml");
|
2000-11-13 15:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Substitutes spaces with underscores in filename (and path)
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const MakeLatexName(string const & file)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string name = OnlyFilename(file);
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const path = OnlyPath(file);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
for (string::size_type i = 0; i < name.length(); ++i) {
|
1999-11-02 06:42:25 +00:00
|
|
|
|
name[i] &= 0x7f; // set 8th bit to 0
|
1999-11-02 21:19:58 +00:00
|
|
|
|
};
|
1999-11-03 12:24:23 +00:00
|
|
|
|
|
1999-11-02 21:19:58 +00:00
|
|
|
|
// ok so we scan through the string twice, but who cares.
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const keep("abcdefghijklmnopqrstuvwxyz"
|
1999-11-03 12:24:23 +00:00
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
"@!\"'()*+,-./0123456789:;<=>?[]`|");
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-11-02 21:19:58 +00:00
|
|
|
|
string::size_type pos = 0;
|
1999-11-03 12:26:16 +00:00
|
|
|
|
while ((pos = name.find_first_not_of(keep, pos)) != string::npos) {
|
1999-11-03 12:45:11 +00:00
|
|
|
|
name[pos++] = '_';
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
1999-11-03 12:24:23 +00:00
|
|
|
|
return AddName(path, name);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
|
1999-12-03 13:51:01 +00:00
|
|
|
|
// Substitutes spaces with underscores in filename (and path)
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const QuoteName(string const & name)
|
1999-12-03 13:51:01 +00:00
|
|
|
|
{
|
2001-05-17 15:11:01 +00:00
|
|
|
|
return (os::shell() == os::UNIX) ?
|
2002-11-27 10:30:28 +00:00
|
|
|
|
'\'' + name + '\'':
|
|
|
|
|
'"' + name + '"';
|
1999-12-03 13:51:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Is a file readable ?
|
2002-06-26 19:07:34 +00:00
|
|
|
|
bool IsFileReadable(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
FileInfo file(path);
|
2002-06-26 19:07:34 +00:00
|
|
|
|
return file.isOK() && file.isRegular() && file.readable();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Is a file read_only?
|
|
|
|
|
// return 1 read-write
|
|
|
|
|
// 0 read_only
|
2002-03-21 17:09:55 +00:00
|
|
|
|
// -1 error (doesn't exist, no access, anything else)
|
2002-06-26 19:07:34 +00:00
|
|
|
|
int IsFileWriteable(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-01-11 08:23:07 +00:00
|
|
|
|
FileInfo fi(path);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2000-01-11 08:23:07 +00:00
|
|
|
|
if (fi.access(FileInfo::wperm|FileInfo::rperm)) // read-write
|
|
|
|
|
return 1;
|
|
|
|
|
if (fi.readable()) // read-only
|
|
|
|
|
return 0;
|
|
|
|
|
return -1; // everything else.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-08-01 10:08:53 +00:00
|
|
|
|
//returns true: dir writeable
|
|
|
|
|
// false: not writeable
|
2002-06-26 19:07:34 +00:00
|
|
|
|
bool IsDirWriteable(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-08-01 10:08:53 +00:00
|
|
|
|
lyxerr[Debug::FILES] << "IsDirWriteable: " << path << endl;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
string 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
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
unlink(tmpfl);
|
2001-08-01 10:08:53 +00:00
|
|
|
|
return true;
|
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"
|
2002-06-26 19:07:34 +00:00
|
|
|
|
string 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
|
|
|
|
{
|
2002-06-26 19:07:34 +00:00
|
|
|
|
string real_file;
|
|
|
|
|
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, '/'))
|
1999-11-15 10:54:16 +00:00
|
|
|
|
path_element+= '/';
|
2003-07-27 21:04:02 +00:00
|
|
|
|
path_element = subst(path_element, "$$LyX", system_lyxdir());
|
2003-07-27 22:13:29 +00:00
|
|
|
|
path_element = subst(path_element, "$$User", user_lyxdir());
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#ifdef __EMX__
|
|
|
|
|
if (ext.empty() && notfound) {
|
|
|
|
|
real_file = FileOpenSearch(path, name, "exe");
|
|
|
|
|
if (notfound) real_file = FileOpenSearch(path, name, "cmd");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return real_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
/// Returns a vector of all files in directory dir having extension ext.
|
2000-11-14 02:01:57 +00:00
|
|
|
|
vector<string> const DirList(string const & dir, string const & ext)
|
2000-11-08 09:39:46 +00:00
|
|
|
|
{
|
|
|
|
|
// This is a non-error checking C/system implementation
|
2002-11-27 14:55:00 +00:00
|
|
|
|
string extension;
|
|
|
|
|
if (!ext.empty() && ext[0] != '.')
|
|
|
|
|
extension += '.';
|
|
|
|
|
extension += ext;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
vector<string> dirlist;
|
|
|
|
|
DIR * dirp = ::opendir(dir.c_str());
|
2001-12-02 16:18:13 +00:00
|
|
|
|
if (!dirp) {
|
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
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
dirent * dire;
|
|
|
|
|
while ((dire = ::readdir(dirp))) {
|
2000-11-14 02:01:57 +00:00
|
|
|
|
string const fil = dire->d_name;
|
2000-12-10 05:20:36 +00:00
|
|
|
|
if (suffixIs(fil, extension)) {
|
2000-11-08 09:39:46 +00:00
|
|
|
|
dirlist.push_back(fil);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
::closedir(dirp);
|
|
|
|
|
return dirlist;
|
2000-11-14 02:01:57 +00:00
|
|
|
|
/* I would have prefered to take a vector<string>& as parameter so
|
|
|
|
|
that we could avoid the copy of the vector when returning.
|
|
|
|
|
Then we would use:
|
|
|
|
|
dirlist.swap(argvec);
|
|
|
|
|
to avoid the copy. (Lgb)
|
|
|
|
|
*/
|
2000-11-08 09:39:46 +00:00
|
|
|
|
/* A C++ implementaion will look like this:
|
2000-11-14 02:01:57 +00:00
|
|
|
|
string extension(ext);
|
2002-11-27 10:30:28 +00:00
|
|
|
|
if (extension[0] != '.') extension.insert(0, 1, '.');
|
2000-11-14 02:01:57 +00:00
|
|
|
|
vector<string> dirlist;
|
2000-11-08 09:39:46 +00:00
|
|
|
|
directory_iterator dit("dir");
|
|
|
|
|
while (dit != directory_iterator()) {
|
2002-03-21 17:09:55 +00:00
|
|
|
|
string fil = dit->filename;
|
2000-11-14 02:01:57 +00:00
|
|
|
|
if (prefixIs(fil, extension)) {
|
2002-03-21 17:09:55 +00:00
|
|
|
|
dirlist.push_back(fil);
|
2000-11-08 09:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
++dit;
|
|
|
|
|
}
|
2000-11-14 02:01:57 +00:00
|
|
|
|
dirlist.swap(argvec);
|
|
|
|
|
return;
|
2000-11-08 09:39:46 +00:00
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
string const FileSearch(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
|
|
|
|
{
|
|
|
|
|
// if `name' is an absolute path, we ignore the setting of `path'
|
|
|
|
|
// Expand Environmentvariables in 'name'
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const tmpname = ReplaceEnvironmentPath(name);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string fullname = MakeAbsPath(tmpname, path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// search first without extension, then with it.
|
|
|
|
|
if (IsFileReadable(fullname))
|
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
else if (ext.empty())
|
1999-10-02 16:21:10 +00:00
|
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
else { // Is it not more reasonable to use ChangeExtension()? (SMiyata)
|
|
|
|
|
fullname += '.';
|
|
|
|
|
fullname += ext;
|
|
|
|
|
if (IsFileReadable(fullname))
|
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
else
|
1999-10-02 16:21:10 +00:00
|
|
|
|
return string();
|
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
|
2002-03-21 17:09:55 +00:00
|
|
|
|
string 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
|
|
|
|
{
|
2003-07-27 22:13:29 +00:00
|
|
|
|
string fullname = FileSearch(AddPath(user_lyxdir(), dir), name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!fullname.empty())
|
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2003-07-27 20:17:31 +00:00
|
|
|
|
if (!build_lyxdir().empty())
|
|
|
|
|
fullname = FileSearch(AddPath(build_lyxdir(), dir), name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!fullname.empty())
|
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2003-07-27 21:04:02 +00:00
|
|
|
|
return FileSearch(AddPath(system_lyxdir(), dir), name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const
|
2002-03-21 17:09:55 +00:00
|
|
|
|
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
|
|
|
|
|
2004-03-24 12:05:57 +00:00
|
|
|
|
string lang = GetEnv("LC_ALL");
|
|
|
|
|
if (lang.empty()) {
|
|
|
|
|
lang = GetEnv("LC_MESSAGES");
|
2001-10-12 16:23:26 +00:00
|
|
|
|
if (lang.empty()) {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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") {
|
2004-07-24 10:55:30 +00:00
|
|
|
|
string const tmp = LibFileSearch(dir,
|
2004-06-29 12:55:09 +00:00
|
|
|
|
token(l, '_', 0) + '_' + name,
|
2000-09-26 13:54:57 +00:00
|
|
|
|
ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!tmp.empty())
|
|
|
|
|
return tmp;
|
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
|
|
|
|
|
|
|
|
|
return LibFileSearch(dir, name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-31 09:24:46 +00:00
|
|
|
|
string const LibScriptSearch(string const & command_in)
|
2002-03-27 00:05:28 +00:00
|
|
|
|
{
|
2003-05-31 09:24:46 +00:00
|
|
|
|
string const token_scriptpath("$$s/");
|
|
|
|
|
|
|
|
|
|
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;
|
2003-05-31 09:24:46 +00:00
|
|
|
|
// Find the end of the "$$s/some_script" word within command
|
|
|
|
|
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?
|
|
|
|
|
string const script =
|
|
|
|
|
LibFileSearch("scripts", command.substr(start_script, size_script));
|
|
|
|
|
|
|
|
|
|
if (script.empty()) {
|
|
|
|
|
// Replace "$$s/" with ""
|
|
|
|
|
command.erase(pos1, 4);
|
|
|
|
|
} else {
|
|
|
|
|
// Replace "$$s/some_script" with "$LYX_SCRIPT_PATH/some_script"
|
|
|
|
|
string::size_type const size_replace = size_script + 4;
|
2004-09-14 10:20:38 +00:00
|
|
|
|
command.replace(pos1, size_replace, QuoteName(script));
|
2003-05-31 09:24:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return command;
|
2002-03-27 00:05:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const GetEnv(string const & envname)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
{
|
2001-10-24 13:21:20 +00:00
|
|
|
|
// f.ex. what about error checking?
|
|
|
|
|
char const * const ch = getenv(envname.c_str());
|
|
|
|
|
string const envstr = !ch ? "" : ch;
|
|
|
|
|
return envstr;
|
1999-10-02 16:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const GetEnvPath(string const & name)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
{
|
|
|
|
|
#ifndef __EMX__
|
2001-10-24 13:21:20 +00:00
|
|
|
|
string const pathlist = subst(GetEnv(name), ':', ';');
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#else
|
2004-12-20 16:59:33 +00:00
|
|
|
|
string const pathlist = os::internal_path(GetEnv(name));
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#endif
|
2002-07-28 22:50:13 +00:00
|
|
|
|
return rtrim(pathlist, ";");
|
1999-10-02 16:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2002-06-26 19:07:34 +00:00
|
|
|
|
int DeleteAllFilesInDir(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-01-20 01:41:55 +00:00
|
|
|
|
// I have decided that we will be using parts from the boost
|
|
|
|
|
// library. Check out http://www.boost.org/
|
|
|
|
|
// For directory access we will then use the directory_iterator.
|
|
|
|
|
// Then the code will be something like:
|
2000-08-05 05:17:18 +00:00
|
|
|
|
// directory_iterator dit(path);
|
|
|
|
|
// directory_iterator dend;
|
|
|
|
|
// if (dit == dend) {
|
2000-01-20 01:41:55 +00:00
|
|
|
|
// return -1;
|
|
|
|
|
// }
|
2000-08-05 05:17:18 +00:00
|
|
|
|
// for (; dit != dend; ++dit) {
|
|
|
|
|
// string filename(*dit);
|
|
|
|
|
// if (filename == "." || filename == "..")
|
2000-01-20 01:41:55 +00:00
|
|
|
|
// continue;
|
2000-08-05 05:17:18 +00:00
|
|
|
|
// string unlinkpath(AddName(path, filename));
|
2003-03-29 09:02:08 +00:00
|
|
|
|
// lyx::unlink(unlinkpath);
|
2000-01-20 01:41:55 +00:00
|
|
|
|
// }
|
|
|
|
|
// return 0;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
DIR * dir = ::opendir(path.c_str());
|
2003-03-29 09:02:08 +00:00
|
|
|
|
if (!dir)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return -1;
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
struct dirent * de;
|
2000-09-11 15:42:17 +00:00
|
|
|
|
int return_value = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
while ((de = readdir(dir))) {
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const temp = de->d_name;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
if (temp == "." || temp == "..")
|
1999-09-27 18:44:28 +00:00
|
|
|
|
continue;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const unlinkpath = AddName (path, temp);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
|
lyxerr[Debug::FILES] << "Deleting file: " << unlinkpath
|
2001-06-27 14:10:35 +00:00
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2000-09-11 15:42:17 +00:00
|
|
|
|
bool deleted = true;
|
2002-01-07 10:17:44 +00:00
|
|
|
|
FileInfo fi(unlinkpath);
|
2004-04-01 21:03:52 +00:00
|
|
|
|
if (fi.isOK() && fi.isDir()) {
|
2000-09-11 15:42:17 +00:00
|
|
|
|
deleted = (DeleteAllFilesInDir(unlinkpath) == 0);
|
2004-04-01 21:03:52 +00:00
|
|
|
|
deleted &= (rmdir(unlinkpath) == 0);
|
|
|
|
|
} else
|
|
|
|
|
deleted &= (unlink(unlinkpath) == 0);
|
2003-03-29 09:02:08 +00:00
|
|
|
|
if (!deleted)
|
2000-09-11 15:42:17 +00:00
|
|
|
|
return_value = -1;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
}
|
1999-12-07 00:44:53 +00:00
|
|
|
|
closedir(dir);
|
2000-09-11 15:42:17 +00:00
|
|
|
|
return return_value;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
string const createTmpDir(string 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
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
string 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)
|
2003-06-30 23:56:22 +00:00
|
|
|
|
unlink(tmpfl);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
|
|
|
|
|
lyxerr << "LyX could not create the temporary directory '"
|
|
|
|
|
<< tmpfl << "'" << endl;
|
1999-10-02 16:21:10 +00:00
|
|
|
|
return string();
|
2004-02-25 12:00:53 +00:00
|
|
|
|
}
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return MakeAbsPath(tmpfl);
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-29 09:02:08 +00:00
|
|
|
|
} // namespace anon
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-03-29 09:02:08 +00:00
|
|
|
|
int destroyDir(string const & tmpdir)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef __EMX__
|
2003-07-27 22:13:29 +00:00
|
|
|
|
Path p(user_lyxdir());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
2003-03-29 09:02:08 +00:00
|
|
|
|
if (DeleteAllFilesInDir(tmpdir))
|
2001-10-24 13:21:20 +00:00
|
|
|
|
return -1;
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (rmdir(tmpdir))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return -1;
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
|
return 0;
|
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?
|
|
|
|
|
// In fact I wrote this code to circumvent a problematic behaviour (bug?)
|
|
|
|
|
// of EMX mkstemp().
|
2005-01-06 15:40:49 +00:00
|
|
|
|
string const tmpfl = os::getTmpDir() + "/lyx_tmpbuf" + convert<string>(count++);
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (mkdir(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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
string const createLyXTmpDir(string const & deflt)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2004-02-25 12:00:53 +00:00
|
|
|
|
if (!deflt.empty() && deflt != "/tmp") {
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (mkdir(deflt, 0777)) {
|
2004-02-25 12:00:53 +00:00
|
|
|
|
if (IsDirWriteable(deflt))
|
|
|
|
|
// deflt could not be created because it
|
|
|
|
|
// did exist already, so let's create our own
|
|
|
|
|
// dir inside deflt.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifdef __EMX__
|
2004-02-25 12:00:53 +00:00
|
|
|
|
Path p(user_lyxdir());
|
|
|
|
|
#endif
|
|
|
|
|
return createTmpDir(deflt, "lyx_tmpdir");
|
|
|
|
|
else
|
|
|
|
|
// some other error occured.
|
|
|
|
|
#ifdef __EMX__
|
|
|
|
|
Path p(user_lyxdir());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
2004-02-25 12:00:53 +00:00
|
|
|
|
return createTmpDir("/tmp", "lyx_tmpdir");
|
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 {
|
|
|
|
|
#ifdef __EMX__
|
2003-07-27 22:13:29 +00:00
|
|
|
|
Path p(user_lyxdir());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
2004-02-25 12:00:53 +00:00
|
|
|
|
return createTmpDir("/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
|
|
|
|
{
|
2004-12-20 16:59:33 +00:00
|
|
|
|
string temp(rtrim(os::internal_path(path), "/"));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(!temp.empty());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (mkdir(temp, permission))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return false;
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Strip filename from path name
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const OnlyPath(string const & Filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// If empty filename, return empty
|
|
|
|
|
if (Filename.empty()) return Filename;
|
|
|
|
|
|
|
|
|
|
// Find last / or start of filename
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string::size_type j = Filename.rfind('/');
|
1999-11-01 04:22:28 +00:00
|
|
|
|
if (j == string::npos)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return "./";
|
1999-10-07 18:44:17 +00:00
|
|
|
|
return 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.
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const MakeAbsPath(string const & RelPath, string const & BasePath)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// checks for already absolute path
|
2001-10-08 14:09:06 +00:00
|
|
|
|
if (os::is_absolute_path(RelPath))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return RelPath;
|
|
|
|
|
|
|
|
|
|
// Copies given paths
|
2004-12-20 16:59:33 +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 "/"
|
|
|
|
|
TempRel = subst(TempRel, "//", "/");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string TempBase;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-10-08 14:09:06 +00:00
|
|
|
|
if (os::is_absolute_path(BasePath))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
TempBase = BasePath;
|
2001-05-17 15:11:01 +00:00
|
|
|
|
else
|
2003-06-30 23:56:22 +00:00
|
|
|
|
TempBase = AddPath(getcwd(), BasePath);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Handle /./ at the end of the path
|
2001-12-05 08:04:20 +00:00
|
|
|
|
while (suffixIs(TempBase, "/./"))
|
1999-10-02 16:21:10 +00:00
|
|
|
|
TempBase.erase(TempBase.length() - 2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// processes relative path
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string RTemp(TempRel);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string Temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
while (!RTemp.empty()) {
|
|
|
|
|
// Split by next /
|
1999-10-02 16:21:10 +00:00
|
|
|
|
RTemp = split(RTemp, Temp, '/');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-11-15 10:54:16 +00:00
|
|
|
|
if (Temp == ".") continue;
|
|
|
|
|
if (Temp == "..") {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Remove one level of TempBase
|
2000-10-18 14:07:29 +00:00
|
|
|
|
string::difference_type i = TempBase.length() - 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifndef __EMX__
|
1999-11-01 04:22:28 +00:00
|
|
|
|
if (i < 0) i = 0;
|
|
|
|
|
while (i > 0 && TempBase[i] != '/') --i;
|
|
|
|
|
if (i > 0)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#else
|
1999-11-01 04:22:28 +00:00
|
|
|
|
if (i < 2) i = 2;
|
|
|
|
|
while (i > 2 && TempBase[i] != '/') --i;
|
|
|
|
|
if (i > 2)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
|
TempBase.erase(i, string::npos);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
else
|
|
|
|
|
TempBase += '/';
|
2001-05-17 15:11:01 +00:00
|
|
|
|
} 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
|
1999-10-02 16:21:10 +00:00
|
|
|
|
if (!suffixIs(TempBase, '/'))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
TempBase += '/';
|
|
|
|
|
TempBase += Temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns absolute path
|
2004-12-20 16:59:33 +00:00
|
|
|
|
return 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.
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const AddName(string const & path, string const & fname)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// Get basename
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const basename(OnlyFilename(fname));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
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
|
2000-08-05 05:17:18 +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
|
1999-10-02 16:21:10 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-10-08 14:09:06 +00:00
|
|
|
|
|
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)
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const ExpandPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// checks for already absolute path
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string RTemp(ReplaceEnvironmentPath(path));
|
2001-10-08 14:09:06 +00:00
|
|
|
|
if (os::is_absolute_path(RTemp))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return RTemp;
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string Temp;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
string const copy(RTemp);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Split by next /
|
2000-08-05 05:17:18 +00:00
|
|
|
|
RTemp = split(RTemp, Temp, '/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
1999-11-15 10:54:16 +00:00
|
|
|
|
if (Temp == ".") {
|
2003-06-30 23:56:22 +00:00
|
|
|
|
return getcwd() + '/' + RTemp;
|
2001-10-24 13:21:20 +00:00
|
|
|
|
}
|
|
|
|
|
if (Temp == "~") {
|
2004-12-15 19:35:43 +00:00
|
|
|
|
return os::homepath() + '/' + RTemp;
|
2001-10-24 13:21:20 +00:00
|
|
|
|
}
|
|
|
|
|
if (Temp == "..") {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return MakeAbsPath(copy);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Normalize a path
|
|
|
|
|
// Constracts path/../path
|
|
|
|
|
// Can't handle "../../" or "/../" (Asger)
|
2003-06-20 14:03:49 +00:00
|
|
|
|
// Also converts paths like /foo//bar ==> /foo/bar
|
2000-08-05 05:17:18 +00:00
|
|
|
|
string const NormalizePath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string TempBase;
|
|
|
|
|
string RTemp;
|
|
|
|
|
string Temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-10-08 14:09:06 +00:00
|
|
|
|
if (os::is_absolute_path(path))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
RTemp = path;
|
|
|
|
|
else
|
|
|
|
|
// Make implicit current directory explicit
|
|
|
|
|
RTemp = "./" +path;
|
|
|
|
|
|
2003-06-20 14:03:49 +00:00
|
|
|
|
// Normalise paths like /foo//bar ==> /foo/bar
|
|
|
|
|
boost::RegEx regex("/{2,}");
|
2003-09-15 11:00:00 +00:00
|
|
|
|
RTemp = regex.Merge(RTemp, "/");
|
2003-06-20 14:03:49 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
while (!RTemp.empty()) {
|
|
|
|
|
// Split by next /
|
1999-10-02 16:21:10 +00:00
|
|
|
|
RTemp = split(RTemp, Temp, '/');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
1999-11-15 10:54:16 +00:00
|
|
|
|
if (Temp == ".") {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
TempBase = "./";
|
1999-11-15 10:54:16 +00:00
|
|
|
|
} else if (Temp == "..") {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Remove one level of TempBase
|
2000-10-18 14:07:29 +00:00
|
|
|
|
string::difference_type i = TempBase.length() - 2;
|
1999-12-07 00:44:53 +00:00
|
|
|
|
while (i > 0 && TempBase[i] != '/')
|
1999-10-02 16:21:10 +00:00
|
|
|
|
--i;
|
1999-12-07 00:44:53 +00:00
|
|
|
|
if (i >= 0 && TempBase[i] == '/')
|
|
|
|
|
TempBase.erase(i + 1, string::npos);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
else
|
|
|
|
|
TempBase = "../";
|
|
|
|
|
} else {
|
|
|
|
|
TempBase += Temp + '/';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns absolute path
|
2002-03-21 17:09:55 +00:00
|
|
|
|
return TempBase;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
|
|
|
|
|
string const GetFileContents(string const & fname)
|
|
|
|
|
{
|
2000-06-12 11:27:15 +00:00
|
|
|
|
FileInfo finfo(fname);
|
|
|
|
|
if (finfo.exist()) {
|
|
|
|
|
ifstream ifs(fname.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.
|
2000-08-05 05:17:18 +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;
|
|
|
|
|
}
|
2003-09-25 23:01:43 +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
|
2001-05-17 15:11:01 +00:00
|
|
|
|
string const MakeRelPath(string const & abspath, string 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.
|
|
|
|
|
{
|
2000-10-18 14:07:29 +00:00
|
|
|
|
string::size_type const abslen = abspath.length();
|
|
|
|
|
string::size_type const baselen = basepath.length();
|
2001-05-17 15:11:01 +00:00
|
|
|
|
|
|
|
|
|
string::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
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string buf;
|
2000-10-25 13:10:04 +00:00
|
|
|
|
string::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
|
2000-08-05 05:17:18 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
|
/*
|
1999-09-27 18:44:28 +00:00
|
|
|
|
Change extension of oldname to extension.
|
|
|
|
|
Strips path off if no_path == true.
|
|
|
|
|
If no extension on oldname, just appends.
|
|
|
|
|
*/
|
2001-10-24 13:21:20 +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
|
|
|
|
|
2000-08-30 03:40:51 +00:00
|
|
|
|
/// Return the extension of the file (not including the .)
|
2000-09-14 17:53:12 +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
|
|
|
|
|
2004-10-29 15:47:55 +00:00
|
|
|
|
string const getFormatFromContents(string const & filename)
|
2002-04-06 12:42:42 +00:00
|
|
|
|
{
|
|
|
|
|
// paranoia check
|
2002-03-01 15:28:02 +00:00
|
|
|
|
if (filename.empty() || !IsFileReadable(filename))
|
|
|
|
|
return string();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2002-01-31 14:20:09 +00:00
|
|
|
|
ifstream ifs(filename.c_str());
|
2002-03-01 15:28:02 +00:00
|
|
|
|
if (!ifs)
|
|
|
|
|
// Couldn't open file...
|
|
|
|
|
return string();
|
|
|
|
|
|
|
|
|
|
// gnuzip
|
2003-07-28 12:47:20 +00:00
|
|
|
|
string const gzipStamp = "\037\213";
|
2002-03-01 15:28:02 +00:00
|
|
|
|
|
|
|
|
|
// PKZIP
|
|
|
|
|
string const zipStamp = "PK";
|
|
|
|
|
|
|
|
|
|
// compress
|
2003-07-28 12:47:20 +00:00
|
|
|
|
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);
|
2002-03-04 10:37:25 +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
|
2002-02-16 18:34:30 +00:00
|
|
|
|
bool zippedFile(string const & name)
|
|
|
|
|
{
|
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)
|
|
|
|
|
{
|
|
|
|
|
string const ext = GetExtension(zipped_file);
|
|
|
|
|
if (ext == "gz" || ext == "z" || ext == "Z")
|
|
|
|
|
return ChangeExtension(zipped_file, string());
|
|
|
|
|
return "unzipped_" + zipped_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
string const unzipFile(string const & zipped_file, string const & unzipped_file)
|
2002-02-07 19:37:34 +00:00
|
|
|
|
{
|
2004-02-25 12:00:53 +00:00
|
|
|
|
string const tempfile = unzipped_file.empty() ?
|
|
|
|
|
unzippedFileName(zipped_file) : unzipped_file;
|
2002-02-16 18:34:30 +00:00
|
|
|
|
// Run gunzip
|
|
|
|
|
string const command = "gunzip -c " + zipped_file + " > " + tempfile;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
string 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;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2004-12-15 19:35:43 +00:00
|
|
|
|
string const home(os::homepath());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
// replace /home/blah with ~/
|
|
|
|
|
if (prefixIs(str, home))
|
|
|
|
|
str = subst(str, home, "~");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
|
if (str.length() <= threshold)
|
|
|
|
|
return 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.
|
|
|
|
|
str = OnlyFilename(path);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
return prefix + str;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-02 06:42:25 +00:00
|
|
|
|
|
2002-07-17 21:43:06 +00:00
|
|
|
|
bool LyXReadLink(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)
|
|
|
|
|
link = MakeAbsPath(linkbuffer, OnlyPath(file));
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2002-07-22 17:11:41 +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
|
|
|
|
|
2004-04-05 06:46:22 +00:00
|
|
|
|
sigset_t newMask, oldMask;
|
|
|
|
|
sigemptyset(&oldMask);
|
|
|
|
|
sigemptyset(&newMask);
|
|
|
|
|
sigaddset(&newMask, SIGCHLD);
|
|
|
|
|
|
|
|
|
|
// Block the SIGCHLD signal.
|
|
|
|
|
sigprocmask(SIG_BLOCK, &newMask, &oldMask);
|
2004-04-20 08:51:15 +00:00
|
|
|
|
|
2002-09-23 16:03:11 +00:00
|
|
|
|
FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
|
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) {
|
2002-02-08 11:23:58 +00:00
|
|
|
|
return make_pair(-1, string());
|
2004-04-05 06:46:22 +00:00
|
|
|
|
lyxerr << "RunCommand:: could not start child process" << endl;
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
2000-11-14 02:01:57 +00:00
|
|
|
|
int const pret = pclose(inf);
|
2004-04-05 06:46:22 +00:00
|
|
|
|
if (pret == -1)
|
|
|
|
|
perror("RunCommand:: could not terminate child process");
|
|
|
|
|
|
|
|
|
|
// Unblock the SIGCHLD signal and restore the old mask.
|
|
|
|
|
sigprocmask(SIG_SETMASK, &oldMask, 0);
|
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
return make_pair(pret, ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-10-24 13:21:20 +00:00
|
|
|
|
string 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.
|
|
|
|
|
if (FileInfo(fil).exist())
|
2000-03-01 18:15:39 +00:00
|
|
|
|
return MakeAbsPath(fil);
|
|
|
|
|
|
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
|
|
|
|
|
2002-07-22 17:11:41 +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'
|
2002-07-28 22:50:13 +00:00
|
|
|
|
<< "kpse result = `" << rtrim(c.second, "\n")
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< '\'' << endl;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
if (c.first != -1)
|
2002-07-28 22:50:13 +00:00
|
|
|
|
return os::internal_path(rtrim(c.second, "\n\r"));
|
2001-06-21 10:55:48 +00:00
|
|
|
|
else
|
|
|
|
|
return string();
|
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)
|
|
|
|
|
{
|
|
|
|
|
string a = OnlyPath(filename);
|
|
|
|
|
a += '#';
|
|
|
|
|
a += OnlyFilename(filename);
|
|
|
|
|
a += '#';
|
2000-11-14 02:01:57 +00:00
|
|
|
|
FileInfo const fileinfo(a);
|
2003-03-29 09:02:08 +00:00
|
|
|
|
if (fileinfo.exist())
|
2003-06-30 23:56:22 +00:00
|
|
|
|
unlink(a);
|
2000-08-08 15:36:25 +00:00
|
|
|
|
}
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
2002-07-28 18:13:51 +00:00
|
|
|
|
void readBB_lyxerrMessage(string const & file, bool & zipped,
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-04-08 14:55:52 +00:00
|
|
|
|
string const readBB_from_PSFile(string const & file)
|
|
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
string const file_ = zipped ?
|
2002-04-08 14:55:52 +00:00
|
|
|
|
string(unzipFile(file)) : string(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
|
|
|
|
|
|
|
|
|
std::ifstream is(file_.c_str());
|
|
|
|
|
while (is) {
|
|
|
|
|
string s;
|
2002-05-02 10:46:51 +00:00
|
|
|
|
getline(is,s);
|
2002-06-18 20:47:49 +00:00
|
|
|
|
if (contains(s,"%%BoundingBox:") && !contains(s,"atend")) {
|
2002-07-28 22:50:13 +00:00
|
|
|
|
string const bb = ltrim(s.substr(14));
|
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
|
|
|
|
|
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
|
int compare_timestamps(string const & file1, string const & file2)
|
2003-06-03 10:14:52 +00:00
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(AbsolutePath(file1) && AbsolutePath(file2));
|
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
|
|
|
|
FileInfo f1(file1);
|
|
|
|
|
FileInfo f2(file2);
|
|
|
|
|
|
|
|
|
|
int cmp = 0;
|
|
|
|
|
if (f1.exist() && f2.exist()) {
|
|
|
|
|
double const tmp = difftime(f1.getModificationTime(),
|
|
|
|
|
f2.getModificationTime());
|
|
|
|
|
if (tmp != 0)
|
|
|
|
|
cmp = tmp > 0 ? 1 : -1;
|
|
|
|
|
|
|
|
|
|
} else if (f1.exist()) {
|
|
|
|
|
cmp = 1;
|
|
|
|
|
} else if (f2.exist()) {
|
|
|
|
|
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
|