1999-09-27 18:44:28 +00:00
|
|
|
/*
|
|
|
|
filetools.C (former paths.C) - part of LyX project
|
|
|
|
General path-mangling functions
|
1999-10-02 16:21:10 +00:00
|
|
|
Copyright 1996 Ivan Schreter
|
|
|
|
Parts Copyright 1996 Dirk Niggemann
|
|
|
|
Parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
|
|
|
|
Parts Copyright 1996 Asger Alstrup
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
See also filetools.H.
|
|
|
|
|
|
|
|
lyx-filetool.C : tools functions for file/path handling
|
|
|
|
this file is part of LyX, the High Level Word Processor
|
1999-10-02 16:21:10 +00:00
|
|
|
Copyright 1995-1996, Matthias Ettrich and the LyX Team
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cctype>
|
2000-01-06 11:35:29 +00:00
|
|
|
|
1999-11-25 13:15:52 +00:00
|
|
|
#include <utility>
|
|
|
|
using std::make_pair;
|
|
|
|
using std::pair;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "filetools.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "filetools.h"
|
1999-12-03 13:51:01 +00:00
|
|
|
#include "LSubstring.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyx_gui_misc.h"
|
|
|
|
#include "FileInfo.h"
|
1999-10-13 17:32:46 +00:00
|
|
|
#include "support/path.h" // I know it's OS/2 specific (SMiyata)
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "gettext.h"
|
1999-11-01 04:22:28 +00:00
|
|
|
#include "lyxlib.h"
|
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
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
extern string system_lyxdir;
|
|
|
|
extern string build_lyxdir;
|
|
|
|
extern string user_lyxdir;
|
|
|
|
extern string system_tempdir;
|
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
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
return contains(filename, ".lyx");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool IsSGMLFilename(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
return contains(filename, ".sgml");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Substitutes spaces with underscores in filename (and path)
|
1999-12-03 13:51:01 +00:00
|
|
|
string MakeLatexName(string const & file)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string name = OnlyFilename(file);
|
|
|
|
string path = OnlyPath(file);
|
1999-09-27 18:44:28 +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.
|
1999-11-03 12:24:23 +00:00
|
|
|
string keep("abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"@!\"'()*+,-./0123456789:;<=>?[]`|");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1999-12-03 13:51:01 +00:00
|
|
|
// Substitutes spaces with underscores in filename (and path)
|
|
|
|
string QuoteName(string const & name)
|
|
|
|
{
|
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
#warning Add proper emx support here!
|
|
|
|
#endif
|
1999-12-28 14:06:29 +00:00
|
|
|
#ifndef __EMX__
|
|
|
|
return '\'' + name + '\'';
|
|
|
|
#else
|
|
|
|
return name;
|
|
|
|
#endif
|
1999-12-03 13:51:01 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/// Returns an unique name to be used as a temporary file.
|
1999-10-02 16:21:10 +00:00
|
|
|
string TmpFileName(string const & dir, string const & mask)
|
1999-09-27 18:44:28 +00:00
|
|
|
{// With all these temporary variables, it should be safe enough :-) (JMarc)
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmpdir;
|
1999-09-27 18:44:28 +00:00
|
|
|
if (dir.empty())
|
|
|
|
tmpdir = system_tempdir;
|
|
|
|
else
|
|
|
|
tmpdir = dir;
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmpfl = AddName(tmpdir, mask);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// find a uniq postfix for the filename...
|
|
|
|
// using the pid, and...
|
1999-10-02 16:21:10 +00:00
|
|
|
tmpfl += tostr(getpid());
|
1999-09-27 18:44:28 +00:00
|
|
|
// a short string...
|
1999-10-02 16:21:10 +00:00
|
|
|
string ret;
|
1999-09-27 18:44:28 +00:00
|
|
|
FileInfo fnfo;
|
1999-12-07 00:44:53 +00:00
|
|
|
for (int a = 'a'; a <= 'z'; ++a)
|
|
|
|
for (int b = 'a'; b <= 'z'; ++b)
|
|
|
|
for (int c = 'a'; c <= 'z'; ++c) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// if this is not enough I have no idea what
|
|
|
|
// to do.
|
|
|
|
ret = tmpfl + char(a) + char(b) + char(c);
|
|
|
|
// check if the file exist
|
|
|
|
if (!fnfo.newFile(ret).exist())
|
|
|
|
return ret;
|
|
|
|
}
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "Not able to find a uniq tmpfile name." << endl;
|
1999-10-02 16:21:10 +00:00
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Is a file readable ?
|
1999-10-02 16:21:10 +00:00
|
|
|
bool IsFileReadable (string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
FileInfo file(path);
|
|
|
|
if (file.isOK() && file.isRegular() && file.readable())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Is a file read_only?
|
|
|
|
// return 1 read-write
|
|
|
|
// 0 read_only
|
|
|
|
// -1 error (doesn't exist, no access, anything else)
|
1999-10-02 16:21:10 +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);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//returns 1: dir writeable
|
|
|
|
// 0: not writeable
|
|
|
|
// -1: error- couldn't find out
|
1999-10-02 16:21:10 +00:00
|
|
|
int IsDirWriteable (string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmpfl = TmpFileName(path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (tmpfl.empty()) {
|
|
|
|
WriteFSAlert(_("LyX Internal Error!"),
|
|
|
|
_("Could not test if directory is writeable"));
|
|
|
|
return -1;
|
|
|
|
} else {
|
2000-01-06 02:44:26 +00:00
|
|
|
FileInfo fi(path);
|
|
|
|
if (fi.writable()) return 1;
|
|
|
|
return 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"
|
1999-10-02 16:21:10 +00:00
|
|
|
string FileOpenSearch (string const & path, string const & name,
|
2000-01-20 01:41:55 +00:00
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string real_file, 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, ';');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
while (notfound && !path_element.empty()) {
|
|
|
|
path_element = CleanupPath(path_element);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (!suffixIs(path_element, '/'))
|
1999-11-15 10:54:16 +00:00
|
|
|
path_element+= '/';
|
1999-10-02 16:21:10 +00:00
|
|
|
path_element = subst(path_element, "$$LyX", system_lyxdir);
|
|
|
|
path_element = subst(path_element, "$$User", user_lyxdir);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
real_file = FileSearch(path_element, name, ext);
|
2000-01-20 01:41: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, ';');
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the real name of file name in directory path, with optional
|
|
|
|
// extension ext.
|
1999-10-02 16:21:10 +00:00
|
|
|
string FileSearch(string const & path, string const & name,
|
|
|
|
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'
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmpname = ReplaceEnvironmentPath(name);
|
|
|
|
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;
|
1999-10-02 16:21:10 +00:00
|
|
|
else if (ext.empty())
|
|
|
|
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;
|
|
|
|
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
|
1999-10-02 16:21:10 +00:00
|
|
|
string LibFileSearch(string const & dir, string const & name,
|
1999-12-07 00:44:53 +00:00
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
string fullname = FileSearch(AddPath(user_lyxdir, dir),
|
|
|
|
name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!fullname.empty())
|
|
|
|
return fullname;
|
2000-01-20 01:41:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!build_lyxdir.empty())
|
1999-10-02 16:21:10 +00:00
|
|
|
fullname = FileSearch(AddPath(build_lyxdir, dir),
|
|
|
|
name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!fullname.empty())
|
|
|
|
return fullname;
|
2000-01-20 01:41:55 +00:00
|
|
|
|
1999-11-15 10:54:16 +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
|
|
|
|
|
|
|
string i18nLibFileSearch(string const & dir, string const & name,
|
1999-12-07 00:44:53 +00:00
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string lang = token(string(GetEnv("LANG")), '_', 0);
|
2000-01-20 01:41:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
if (lang.empty() || lang == "C")
|
1999-09-27 18:44:28 +00:00
|
|
|
return LibFileSearch(dir, name, ext);
|
|
|
|
else {
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmp = LibFileSearch(dir, lang + '_' + name,
|
2000-01-20 01:41:55 +00:00
|
|
|
ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!tmp.empty())
|
|
|
|
return tmp;
|
|
|
|
else
|
|
|
|
return LibFileSearch(dir, name, ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string GetEnv(string const & envname)
|
|
|
|
{
|
|
|
|
// f.ex. what about error checking?
|
|
|
|
char const * const ch = getenv(envname.c_str());
|
|
|
|
string envstr = !ch ? "" : ch;
|
|
|
|
return envstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string GetEnvPath(string const & name)
|
|
|
|
{
|
|
|
|
#ifndef __EMX__
|
|
|
|
string pathlist = subst(GetEnv(name), ':', ';');
|
|
|
|
#else
|
|
|
|
string pathlist = subst(GetEnv(name), '\\', '/');
|
|
|
|
#endif
|
|
|
|
return strip(pathlist, ';');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PutEnv(string const & envstr)
|
|
|
|
{
|
1999-10-28 15:19:59 +00:00
|
|
|
#ifdef WITH_WARNINGS
|
1999-10-02 16:21:10 +00:00
|
|
|
#warning Look at and fix this.
|
1999-10-28 15:19:59 +00:00
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
// f.ex. what about error checking?
|
|
|
|
#if HAVE_PUTENV
|
|
|
|
// this leaks, but what can we do about it?
|
|
|
|
// Is doing a getenv() and a free() of the older value
|
|
|
|
// a good idea? (JMarc)
|
2000-01-06 02:44:26 +00:00
|
|
|
// Actually we don't have to leak...calling putenv like this
|
2000-01-08 21:02:58 +00:00
|
|
|
// should be enough: ... and this is obviously not enough if putenv
|
|
|
|
// does not make a copy of the string. It is also not very wise to
|
|
|
|
// put a string on the free store. If we have to leak we should do it
|
|
|
|
// like this:
|
|
|
|
char * leaker = new char[envstr.length() + 1];
|
|
|
|
envstr.copy(leaker, envstr.length());
|
|
|
|
leaker[envstr.length()] = '\0';
|
2000-01-20 01:41:55 +00:00
|
|
|
int retval = lyx::putenv(leaker);
|
2000-01-08 21:02:58 +00:00
|
|
|
|
|
|
|
// If putenv does not make a copy of the char const * this
|
|
|
|
// is very dangerous. OTOH if it does take a copy this is the
|
|
|
|
// best solution.
|
2000-01-20 01:41:55 +00:00
|
|
|
// The only implementation of putenv that I have seen does not
|
|
|
|
// allocate memory. _And_ after testing the putenv in glibc it
|
|
|
|
// seems that we need to make a copy of the string contents.
|
|
|
|
// I will enable the above.
|
|
|
|
//int retval = lyx::putenv(envstr.c_str());
|
1999-10-02 16:21:10 +00:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_SETENV
|
|
|
|
string varname;
|
|
|
|
string str = envstr.split(varname,'=');
|
1999-12-07 00:44:53 +00:00
|
|
|
int retval = setenv(varname.c_str(), str.c_str(), true);
|
1999-10-02 16:21:10 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return retval == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PutEnvPath(string const & envstr)
|
|
|
|
{
|
1999-11-01 04:22:28 +00:00
|
|
|
return PutEnv(envstr);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
static
|
1999-10-02 16:21:10 +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:
|
|
|
|
// directory_iterator dit(path.c_str());
|
|
|
|
// if (<some way to detect failure>) {
|
|
|
|
// WriteFSAlert(_("Error! Cannot open directory:"), path);
|
|
|
|
// return -1;
|
|
|
|
// }
|
|
|
|
// for (; dit != <someend>; ++dit) {
|
|
|
|
// if ((*dit) == 2." || (*dit) == "..")
|
|
|
|
// continue;
|
|
|
|
// string unlinkpath = AddName(path, temp);
|
|
|
|
// if (remove(unlinkpath.c_str()))
|
|
|
|
// WriteFSAlert(_("Error! Could not remove file:"),
|
|
|
|
// unlinkpath);
|
|
|
|
// }
|
|
|
|
// return 0;
|
1999-11-02 06:42:25 +00:00
|
|
|
DIR * dir = opendir(path.c_str());
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!dir) {
|
|
|
|
WriteFSAlert (_("Error! Cannot open directory:"), path);
|
|
|
|
return -1;
|
|
|
|
}
|
1999-12-07 00:44:53 +00:00
|
|
|
struct dirent * de;
|
1999-09-27 18:44:28 +00:00
|
|
|
while ((de = readdir(dir))) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string temp = de->d_name;
|
1999-11-15 10:54:16 +00:00
|
|
|
if (temp == "." || temp == "..")
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
1999-10-02 16:21:10 +00:00
|
|
|
string unlinkpath = AddName (path, temp);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr.debug() << "Deleting file: " << unlinkpath << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
if (remove(unlinkpath.c_str()))
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert (_("Error! Could not remove file:"),
|
|
|
|
unlinkpath);
|
|
|
|
}
|
1999-12-07 00:44:53 +00:00
|
|
|
closedir(dir);
|
1999-09-27 18:44:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
1999-10-02 16:21:10 +00:00
|
|
|
string CreateTmpDir (string const & tempdir, string const & mask)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmpfl = TmpFileName(tempdir, mask);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-01-20 01:41:55 +00:00
|
|
|
if ((tmpfl.empty()) || lyx::mkdir (tmpfl.c_str(), 0777)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert(_("Error! Couldn't create temporary directory:"),
|
|
|
|
tempdir);
|
1999-10-02 16:21:10 +00:00
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
return MakeAbsPath(tmpfl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
1999-10-02 16:21:10 +00:00
|
|
|
int DestroyTmpDir (string const & tmpdir, bool Allfiles)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
#ifdef __EMX__
|
1999-11-01 04:22:28 +00:00
|
|
|
Path p(user_lyxdir);
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
1999-11-01 04:22:28 +00:00
|
|
|
if (Allfiles && DeleteAllFilesInDir(tmpdir)) return -1;
|
|
|
|
if (rmdir(tmpdir.c_str())) {
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert(_("Error! Couldn't delete temporary directory:"),
|
|
|
|
tmpdir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string CreateBufferTmpDir (string const & pathfor)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
return CreateTmpDir(pathfor, "lyx_bufrtmp");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
int DestroyBufferTmpDir (string const & tmpdir)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
return DestroyTmpDir(tmpdir, true);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string CreateLyXTmpDir (string const & deflt)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
if ((!deflt.empty()) && (deflt != "/tmp")) {
|
2000-01-20 01:41:55 +00:00
|
|
|
if (lyx::mkdir(deflt.c_str(), 0777)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __EMX__
|
1999-11-01 04:22:28 +00:00
|
|
|
Path p(user_lyxdir);
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
1999-12-07 00:44:53 +00:00
|
|
|
string t = CreateTmpDir (deflt.c_str(), "lyx_tmp");
|
1999-09-27 18:44:28 +00:00
|
|
|
return t;
|
|
|
|
} else
|
|
|
|
return deflt;
|
|
|
|
} else {
|
|
|
|
#ifdef __EMX__
|
1999-11-01 04:22:28 +00:00
|
|
|
Path p(user_lyxdir);
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
1999-12-07 00:44:53 +00:00
|
|
|
string t = CreateTmpDir ("/tmp", "lyx_tmp");
|
1999-09-27 18:44:28 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
int DestroyLyXTmpDir (string const & tmpdir)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
return DestroyTmpDir (tmpdir, false); // Why false?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates directory. Returns true if succesfull
|
1999-10-02 16:21:10 +00:00
|
|
|
bool createDirectory(string const & path, int permission)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string temp = strip(CleanupPath(path), '/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (temp.empty()) {
|
|
|
|
WriteAlert(_("Internal error!"),
|
|
|
|
_("Call to createDirectory with invalid name"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-01-20 01:41:55 +00:00
|
|
|
if (lyx::mkdir(temp.c_str(), permission)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert (_("Error! Couldn't create directory:"), temp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns current working directory
|
1999-10-02 16:21:10 +00:00
|
|
|
string GetCWD ()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
int n = 256; // Assume path is less than 256 chars
|
|
|
|
char * err;
|
1999-11-01 04:22:28 +00:00
|
|
|
char * tbuf = new char[n];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Safe. Hopefully all getcwds behave this way!
|
1999-11-01 04:22:28 +00:00
|
|
|
while (((err = lyx::getcwd (tbuf, n)) == 0) && (errno == ERANGE)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Buffer too small, double the buffersize and try again
|
|
|
|
delete[] tbuf;
|
1999-11-01 04:22:28 +00:00
|
|
|
n = 2 * n;
|
|
|
|
tbuf = new char[n];
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
string result;
|
1999-09-27 18:44:28 +00:00
|
|
|
if (err) result = tbuf;
|
|
|
|
delete[] tbuf;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Strip filename from path name
|
1999-10-02 16:21:10 +00:00
|
|
|
string 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.
|
1999-10-02 16:21:10 +00:00
|
|
|
string MakeAbsPath(string const & RelPath, string const & BasePath)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// checks for already absolute path
|
|
|
|
if (AbsolutePath(RelPath))
|
|
|
|
#ifdef __EMX__
|
1999-11-15 10:54:16 +00:00
|
|
|
if(RelPath[0]!= '/' && RelPath[0]!= '\\')
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
return RelPath;
|
|
|
|
|
|
|
|
// Copies given paths
|
1999-10-02 16:21:10 +00:00
|
|
|
string TempRel = CleanupPath(RelPath);
|
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
|
|
|
|
|
|
|
if (!BasePath.empty()) {
|
|
|
|
#ifndef __EMX__
|
|
|
|
TempBase = BasePath;
|
|
|
|
#else
|
1999-10-02 16:21:10 +00:00
|
|
|
char * with_drive = new char[_MAX_PATH];
|
1999-09-27 18:44:28 +00:00
|
|
|
_abspath(with_drive, BasePath.c_str(), _MAX_PATH);
|
|
|
|
TempBase = with_drive;
|
|
|
|
delete[] with_drive;
|
|
|
|
#endif
|
|
|
|
} else
|
1999-10-02 16:21:10 +00:00
|
|
|
TempBase = GetCWD();
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
if (AbsolutePath(TempRel))
|
1999-11-01 04:22:28 +00:00
|
|
|
return TempBase.substr(0, 2) + TempRel;
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Handle /./ at the end of the path
|
1999-10-02 16:21:10 +00:00
|
|
|
while(suffixIs(TempBase, "/./"))
|
|
|
|
TempBase.erase(TempBase.length() - 2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// processes relative path
|
1999-10-02 16:21:10 +00:00
|
|
|
string RTemp = TempRel;
|
|
|
|
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, '/');
|
1999-09-27 18:44:28 +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
|
1999-12-07 00:44:53 +00:00
|
|
|
int 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 += '/';
|
|
|
|
} 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
|
1999-12-07 00:44:53 +00:00
|
|
|
return 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.
|
1999-10-02 16:21:10 +00:00
|
|
|
string AddName(string const & path, string const & fname)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// Get basename
|
1999-10-02 16:21:10 +00:00
|
|
|
string 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()) {
|
|
|
|
buf = CleanupPath(path);
|
|
|
|
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
|
1999-10-02 16:21:10 +00:00
|
|
|
string 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Is a filename/path absolute?
|
1999-10-02 16:21:10 +00:00
|
|
|
bool AbsolutePath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
#ifndef __EMX__
|
1999-10-02 16:21:10 +00:00
|
|
|
return (!path.empty() && path[0] == '/');
|
1999-09-27 18:44:28 +00:00
|
|
|
#else
|
1999-11-15 10:54:16 +00:00
|
|
|
return (!path.empty() && (path[0] == '/' || (isalpha(static_cast<unsigned char>(path[0])) && path.length()>1 && path[1] == ':')));
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create absolute path. If impossible, don't do anything
|
|
|
|
// Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
|
1999-10-02 16:21:10 +00:00
|
|
|
string ExpandPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// checks for already absolute path
|
1999-10-02 16:21:10 +00:00
|
|
|
string RTemp = ReplaceEnvironmentPath(path);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (AbsolutePath(RTemp))
|
|
|
|
return RTemp;
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string Temp;
|
|
|
|
string copy(RTemp);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Split by next /
|
1999-11-15 10:54:16 +00:00
|
|
|
RTemp= split(RTemp, Temp, '/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-11-15 10:54:16 +00:00
|
|
|
if (Temp == ".") {
|
1999-09-27 18:44:28 +00:00
|
|
|
return GetCWD() + '/' + RTemp;
|
1999-11-15 10:54:16 +00:00
|
|
|
} else if (Temp == "~") {
|
1999-10-02 16:21:10 +00:00
|
|
|
return GetEnvPath("HOME") + '/' + RTemp;
|
1999-11-15 10:54:16 +00:00
|
|
|
} else if (Temp == "..") {
|
1999-09-27 18:44:28 +00:00
|
|
|
return MakeAbsPath(copy);
|
|
|
|
} else
|
|
|
|
// Don't know how to handle this
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Normalize a path
|
|
|
|
// Constracts path/../path
|
|
|
|
// Can't handle "../../" or "/../" (Asger)
|
1999-10-02 16:21:10 +00:00
|
|
|
string 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
|
|
|
|
|
|
|
if (AbsolutePath(path))
|
|
|
|
RTemp = path;
|
|
|
|
else
|
|
|
|
// Make implicit current directory explicit
|
|
|
|
RTemp = "./" +path;
|
|
|
|
|
|
|
|
while (!RTemp.empty()) {
|
|
|
|
// Split by next /
|
1999-10-02 16:21:10 +00:00
|
|
|
RTemp = split(RTemp, Temp, '/');
|
1999-09-27 18:44:28 +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
|
1999-12-07 00:44:53 +00:00
|
|
|
int i = TempBase.length() - 2;
|
|
|
|
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
|
|
|
|
return TempBase;
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string CleanupPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
#ifdef __EMX__ /* SMiyata: This should fix searchpath bug. */
|
1999-10-26 23:33:30 +00:00
|
|
|
string temppath = subst(path, '\\', '/');
|
|
|
|
temppath = subst(temppath, "//", "/");
|
1999-10-02 16:21:10 +00:00
|
|
|
return lowercase(temppath);
|
1999-09-27 18:44:28 +00:00
|
|
|
#else // On unix, nothing to do
|
|
|
|
return path;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Search ${...} as Variable-Name inside the string and replace it with
|
|
|
|
// the denoted environmentvariable
|
|
|
|
// Allow Variables according to
|
|
|
|
// variable := '$' '{' [A-Za-z_]{[A-Za-z_0-9]*} '}'
|
|
|
|
//
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string ReplaceEnvironmentPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// CompareChar: Environmentvariables starts with this character
|
|
|
|
// PathChar: Next path component start with this character
|
|
|
|
// while CompareChar found do:
|
|
|
|
// Split String with PathChar
|
|
|
|
// Search Environmentvariable
|
|
|
|
// if found: Replace Strings
|
|
|
|
//
|
1999-11-15 10:54:16 +00:00
|
|
|
char const CompareChar = '$';
|
|
|
|
char const FirstChar = '{';
|
|
|
|
char const EndChar = '}';
|
|
|
|
char const UnderscoreChar = '_';
|
1999-10-02 16:21:10 +00:00
|
|
|
string EndString; EndString += EndChar;
|
|
|
|
string FirstString; FirstString += FirstChar;
|
|
|
|
string CompareString; CompareString += CompareChar;
|
1999-11-15 10:54:16 +00:00
|
|
|
string const RegExp("*}*"); // Exist EndChar inside a String?
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// first: Search for a '$' - Sign.
|
1999-10-02 16:21:10 +00:00
|
|
|
//string copy(path);
|
|
|
|
string result1; //(copy); // for split-calls
|
|
|
|
string result0 = split(path, result1, CompareChar);
|
1999-09-27 18:44:28 +00:00
|
|
|
while (!result0.empty()) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string copy1(result0); // contains String after $
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Check, if there is an EndChar inside original String.
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
if (!regexMatch(copy1, RegExp)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// No EndChar inside. So we are finished
|
|
|
|
result1 += CompareString + result0;
|
1999-10-19 20:59:27 +00:00
|
|
|
result0.clear();
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string res1;
|
|
|
|
string res0 = split(copy1, res1, EndChar);
|
1999-09-27 18:44:28 +00:00
|
|
|
// Now res1 holds the environmentvariable
|
|
|
|
// First, check, if Contents is ok.
|
|
|
|
if (res1.empty()) { // No environmentvariable. Continue Loop.
|
|
|
|
result1 += CompareString + FirstString;
|
|
|
|
result0 = res0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// check contents of res1
|
1999-11-15 10:54:16 +00:00
|
|
|
char const * res1_contents = res1.c_str();
|
1999-09-27 18:44:28 +00:00
|
|
|
if (*res1_contents != FirstChar) {
|
|
|
|
// Again No Environmentvariable
|
|
|
|
result1 += CompareString;
|
1999-12-07 00:44:53 +00:00
|
|
|
result0 = res0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for variable names
|
|
|
|
// Situation ${} is detected as "No Environmentvariable"
|
1999-12-07 00:44:53 +00:00
|
|
|
char const * cp1 = res1_contents + 1;
|
1999-11-04 01:40:20 +00:00
|
|
|
bool result = isalpha(*cp1) || (*cp1 == UnderscoreChar);
|
1999-09-27 18:44:28 +00:00
|
|
|
++cp1;
|
|
|
|
while (*cp1 && result) {
|
1999-11-04 01:40:20 +00:00
|
|
|
result = isalnum(*cp1) ||
|
1999-09-27 18:44:28 +00:00
|
|
|
(*cp1 == UnderscoreChar);
|
|
|
|
++cp1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
// no correct variable name
|
|
|
|
result1 += CompareString + res1 + EndString;
|
1999-10-02 16:21:10 +00:00
|
|
|
result0 = split(res0, res1, CompareChar);
|
1999-09-27 18:44:28 +00:00
|
|
|
result1 += res1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string env = GetEnv(res1_contents+1);
|
|
|
|
if (!env.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Congratulations. Environmentvariable found
|
|
|
|
result1 += env;
|
|
|
|
} else {
|
|
|
|
result1 += CompareString + res1 + EndString;
|
|
|
|
}
|
|
|
|
// Next $-Sign?
|
1999-10-02 16:21:10 +00:00
|
|
|
result0 = split(res0, res1, CompareChar);
|
1999-09-27 18:44:28 +00:00
|
|
|
result1 += res1;
|
|
|
|
}
|
|
|
|
return result1;
|
|
|
|
} // ReplaceEnvironmentPath
|
|
|
|
|
|
|
|
|
|
|
|
// Make relative path out of two absolute paths
|
1999-10-02 16:21:10 +00:00
|
|
|
string MakeRelPath(string const & abspath0, string const & basepath0)
|
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.
|
|
|
|
{
|
|
|
|
// This is a hack. It should probaly be done in another way. Lgb.
|
1999-10-02 16:21:10 +00:00
|
|
|
string abspath = CleanupPath(abspath0);
|
|
|
|
string basepath = CleanupPath(basepath0);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (abspath.empty())
|
|
|
|
return "<unknown_path>";
|
|
|
|
|
|
|
|
const int abslen = abspath.length();
|
|
|
|
const int baselen = basepath.length();
|
|
|
|
|
|
|
|
// Find first different character
|
|
|
|
int i = 0;
|
|
|
|
while (i < abslen && i < baselen && abspath[i] == basepath[i]) ++i;
|
|
|
|
|
|
|
|
// Go back to last /
|
|
|
|
if (i < abslen && i < baselen
|
1999-11-15 10:54:16 +00:00
|
|
|
|| (i<abslen && abspath[i] != '/' && i == baselen)
|
|
|
|
|| (i<baselen && basepath[i] != '/' && i == abslen))
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
if (i) --i; // here was the last match
|
|
|
|
while (i && abspath[i] != '/') --i;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
1999-09-27 18:44:28 +00:00
|
|
|
int j = i;
|
|
|
|
while (j < baselen) {
|
|
|
|
if (basepath[j] == '/') {
|
2000-01-13 16:28:54 +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
|
|
|
|
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
|
1999-10-02 16:21:10 +00:00
|
|
|
string 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;
|
|
|
|
string path2 = CleanupPath(path_2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (!path.empty() && path != "." && path != "./") {
|
|
|
|
buf = CleanupPath(path);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (path[path.length() - 1] != '/')
|
1999-09-27 18:44:28 +00:00
|
|
|
buf += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path2.empty()){
|
1999-10-02 16:21:10 +00:00
|
|
|
int p2start = path2.find_first_not_of('/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
int p2end = path2.find_last_not_of('/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmp = path2.substr(p2start, p2end - p2start + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
buf += tmp + '/';
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Change extension of oldname to extension.
|
|
|
|
Strips path off if no_path == true.
|
|
|
|
If no extension on oldname, just appends.
|
|
|
|
*/
|
1999-10-02 16:21:10 +00:00
|
|
|
string ChangeExtension(string const & oldname, string const & extension,
|
1999-09-27 18:44:28 +00:00
|
|
|
bool no_path)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
string::size_type 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;
|
|
|
|
|
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] != '.')
|
1999-11-15 10:54:16 +00:00
|
|
|
ext= '.' + extension;
|
1999-09-27 18:44:28 +00:00
|
|
|
else
|
|
|
|
ext = extension;
|
1999-10-02 16:21:10 +00:00
|
|
|
string ret_str;
|
|
|
|
if (no_path && last_slash != string::npos) {
|
|
|
|
++last_slash; // step it
|
|
|
|
ret_str = oldname.substr(last_slash,
|
|
|
|
last_dot - last_slash) + ext;
|
|
|
|
} else
|
|
|
|
ret_str = oldname.substr(0, last_dot) + ext;
|
|
|
|
return CleanupPath(ret_str);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a nice compact path for displaying
|
1999-10-02 16:21:10 +00:00
|
|
|
string MakeDisplayPath (string const & path, unsigned int threshold)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
const int l1 = path.length();
|
|
|
|
|
|
|
|
// First, we try a relative path compared to home
|
1999-10-02 16:21:10 +00:00
|
|
|
string home = GetEnvPath("HOME");
|
|
|
|
string relhome = MakeRelPath(path, home);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
unsigned int l2 = relhome.length();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string prefix;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// If we backup from home or don't have a relative path,
|
|
|
|
// this try is no good
|
1999-10-02 16:21:10 +00:00
|
|
|
if (prefixIs(relhome, "../") || AbsolutePath(relhome)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// relative path was no good, just use the original path
|
|
|
|
relhome = path;
|
|
|
|
l2 = l1;
|
|
|
|
} else {
|
|
|
|
prefix = "~/";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the path too long?
|
|
|
|
if (l2 > threshold) {
|
|
|
|
// Yes, shortend it
|
|
|
|
prefix += ".../";
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
while (relhome.length() > threshold)
|
|
|
|
relhome = split(relhome, temp, '/');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Did we shortend everything away?
|
|
|
|
if (relhome.empty()) {
|
|
|
|
// Yes, filename in itself is too long.
|
|
|
|
// Pick the start and the end of the filename.
|
|
|
|
relhome = OnlyFilename(path);
|
1999-10-02 16:21:10 +00:00
|
|
|
string head = relhome.substr(0, threshold/2 - 3);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
l2 = relhome.length();
|
|
|
|
string tail =
|
|
|
|
relhome.substr(l2 - threshold/2 - 2, l2 - 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
relhome = head + "..." + tail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prefix + relhome;
|
|
|
|
}
|
|
|
|
|
1999-11-02 06:42:25 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool LyXReadLink(string const & File, string & Link)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
char LinkBuffer[512];
|
1999-12-07 00:44:53 +00:00
|
|
|
// Should be PATH_MAX but that needs autconf support
|
|
|
|
int nRead = readlink(File.c_str(), LinkBuffer, sizeof(LinkBuffer)-1);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (nRead <= 0)
|
|
|
|
return false;
|
|
|
|
LinkBuffer[nRead] = 0;
|
|
|
|
Link = LinkBuffer;
|
|
|
|
return true;
|
|
|
|
}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef pair<int, string> cmdret;
|
|
|
|
static cmdret do_popen(string const & cmd)
|
|
|
|
{
|
|
|
|
// 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
|
1999-11-24 22:14:46 +00:00
|
|
|
FILE * inf = popen(cmd.c_str(), "r");
|
|
|
|
string ret;
|
|
|
|
int c = fgetc(inf);
|
|
|
|
while (c != EOF) {
|
|
|
|
ret += static_cast<char>(c);
|
|
|
|
c = fgetc(inf);
|
|
|
|
}
|
|
|
|
int pret = pclose(inf);
|
|
|
|
return make_pair(pret, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string findtexfile(string const & fil, string const & format)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
// If fil is a file with absolute path we just return it
|
|
|
|
if (AbsolutePath(fil)) return fil;
|
|
|
|
|
|
|
|
// Check in the current dir.
|
|
|
|
if (FileInfo(OnlyFilename(fil)).exist())
|
|
|
|
return OnlyFilename(fil);
|
|
|
|
|
|
|
|
// No we try to find it using kpsewhich.
|
2000-01-12 11:16:01 +00:00
|
|
|
string kpsecmd = "kpsewhich --format=" + format + " " + OnlyFilename(fil);
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
cmdret c = do_popen(kpsecmd);
|
|
|
|
|
1999-12-22 14:35:05 +00:00
|
|
|
lyxerr[Debug::LATEX] << "kpse status = " << c.first << "\n"
|
|
|
|
<< "kpse result = `" << strip(c.second, '\n')
|
|
|
|
<< "'" << endl;
|
1999-11-24 22:14:46 +00:00
|
|
|
return c.first != -1 ? strip(c.second, '\n') : string();
|
|
|
|
}
|