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
|
|
|
|
2001-10-08 14:09:06 +00:00
|
|
|
See also filetools.h.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
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>
|
2000-06-12 11:27:15 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
#include "Lsstream.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "filetools.h"
|
|
|
|
#endif
|
|
|
|
|
2001-07-29 15:34:18 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <cerrno>
|
|
|
|
#include "debug.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#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"
|
2001-05-17 15:11:01 +00:00
|
|
|
#include "os.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
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::make_pair;
|
|
|
|
using std::pair;
|
|
|
|
using std::endl;
|
2000-06-12 11:27:15 +00:00
|
|
|
using std::ifstream;
|
2000-11-08 09:39:46 +00:00
|
|
|
using std::vector;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
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
|
|
|
{
|
2001-06-27 14:10:35 +00:00
|
|
|
return suffixIs(lowercase(filename), ".lyx");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-13 15:43:36 +00:00
|
|
|
bool IsSGMLFilename(string const & filename)
|
|
|
|
{
|
2001-06-27 14:10:35 +00:00
|
|
|
return suffixIs(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);
|
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.
|
2000-09-26 13:54:57 +00:00
|
|
|
string const keep("abcdefghijklmnopqrstuvwxyz"
|
1999-11-03 12:24:23 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
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) ?
|
|
|
|
"\'" + name + "\'":
|
|
|
|
"\"" + name + "\"";
|
1999-12-03 13:51:01 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2001-08-01 10:08:53 +00:00
|
|
|
//lyxerr << "fi : " << fi << endl;
|
|
|
|
//lyxerr << "fi.exists" << fi.exist() << endl;
|
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
|
|
|
|
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;
|
|
|
|
|
2001-10-24 13:21:20 +00:00
|
|
|
string const tmpfl(lyx::tempName(path, "lyxwritetest"));
|
2001-08-01 10:08:53 +00:00
|
|
|
|
|
|
|
if (tmpfl.empty())
|
|
|
|
return false;
|
|
|
|
|
2001-01-08 09:37:13 +00:00
|
|
|
lyx::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"
|
2000-08-05 05:17:18 +00:00
|
|
|
string const FileOpenSearch (string const & path, string const & name,
|
|
|
|
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()) {
|
2001-05-17 15:11:01 +00:00
|
|
|
path_element = os::slashify_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+= '/';
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
string extension(ext);
|
2000-12-10 05:20:36 +00:00
|
|
|
if (!extension.empty() && extension[0] != '.')
|
2000-11-14 02:01:57 +00:00
|
|
|
extension.insert(0, ".");
|
2000-11-08 09:39:46 +00:00
|
|
|
vector<string> dirlist;
|
|
|
|
DIR * dirp = ::opendir(dir.c_str());
|
|
|
|
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);
|
|
|
|
if (extension[0] != '.') extension.insert(0, ".");
|
|
|
|
vector<string> dirlist;
|
2000-11-08 09:39:46 +00:00
|
|
|
directory_iterator dit("dir");
|
|
|
|
while (dit != directory_iterator()) {
|
2001-07-12 11:11:10 +00:00
|
|
|
string fil = dit->filename;
|
2000-11-14 02:01:57 +00:00
|
|
|
if (prefixIs(fil, extension)) {
|
2000-11-08 09:39:46 +00:00
|
|
|
dirlist.push_back(fil);
|
|
|
|
}
|
|
|
|
++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
|
|
|
|
// extension ext.
|
2000-08-05 05:17:18 +00:00
|
|
|
string const 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'
|
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;
|
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
|
2000-08-05 05:17:18 +00:00
|
|
|
string const LibFileSearch(string const & dir, string const & name,
|
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-24 13:21:20 +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())
|
2001-10-24 13:21:20 +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
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
string const
|
|
|
|
i18nLibFileSearch(string const & dir, string const & name,
|
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-12 16:23:26 +00:00
|
|
|
// this comment is from intl/dcigettext.c. We try to mimick this
|
|
|
|
// behaviour here.
|
|
|
|
/* 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. */
|
2001-10-12 16:23:26 +00:00
|
|
|
|
|
|
|
string const lc_all = GetEnv("LC_ALL");
|
|
|
|
string lang = GetEnv("LANGUAGE");
|
|
|
|
if (lang.empty() || lc_all == "C") {
|
|
|
|
lang = lc_all;
|
|
|
|
if (lang.empty()) {
|
|
|
|
lang = GetEnv("LANG");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lang = token(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 {
|
2000-09-26 13:54:57 +00:00
|
|
|
string const tmp = LibFileSearch(dir, lang + '_' + name,
|
|
|
|
ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!tmp.empty())
|
|
|
|
return tmp;
|
|
|
|
else
|
|
|
|
return LibFileSearch(dir, name, ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2001-10-24 13:21:20 +00:00
|
|
|
string const pathlist = os::slashify_path(GetEnv(name));
|
1999-10-02 16:21:10 +00:00
|
|
|
#endif
|
2001-10-24 13:21:20 +00:00
|
|
|
return strip(pathlist, ';');
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PutEnv(string const & envstr)
|
|
|
|
{
|
2000-04-26 13:57:28 +00:00
|
|
|
// CHECK Look at and fix this.
|
2001-10-24 13:21:20 +00:00
|
|
|
// f.ex. what about error checking?
|
2000-06-08 13:55:33 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#if HAVE_PUTENV
|
2001-10-24 13:21:20 +00:00
|
|
|
// 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-09-26 13:54:57 +00:00
|
|
|
int const 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
|
2001-10-24 13:21:20 +00:00
|
|
|
string varname;
|
|
|
|
string const str = envstr.split(varname,'=');
|
|
|
|
int const retval = ::setenv(varname.c_str(), str.c_str(), true);
|
2000-06-08 13:55:33 +00:00
|
|
|
#else
|
|
|
|
// No environment setting function. Can this happen?
|
2000-09-26 13:54:57 +00:00
|
|
|
int const retval = 1; //return an error condition.
|
1999-10-02 16:21:10 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2001-10-24 13:21:20 +00:00
|
|
|
return retval == 0;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PutEnvPath(string const & envstr)
|
|
|
|
{
|
2001-10-24 13:21:20 +00:00
|
|
|
return PutEnv(envstr);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
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:
|
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
|
|
|
// WriteFSAlert(_("Error! Cannot open directory:"), path);
|
|
|
|
// 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));
|
2000-09-26 13:54:57 +00:00
|
|
|
// if (lyx::unlink(unlinkpath))
|
2000-01-20 01:41:55 +00:00
|
|
|
// WriteFSAlert(_("Error! Could not remove file:"),
|
|
|
|
// unlinkpath);
|
|
|
|
// }
|
|
|
|
// return 0;
|
2000-09-26 13:54:57 +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;
|
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;
|
1999-11-15 10:54:16 +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
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
lyxerr[Debug::FILES] << "Deleting file: " << unlinkpath
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-09-11 15:42:17 +00:00
|
|
|
bool deleted = true;
|
|
|
|
if (FileInfo(unlinkpath).isDir())
|
|
|
|
deleted = (DeleteAllFilesInDir(unlinkpath) == 0);
|
2000-09-26 13:54:57 +00:00
|
|
|
deleted &= (lyx::unlink(unlinkpath) == 0);
|
2000-09-11 15:42:17 +00:00
|
|
|
if (!deleted) {
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert (_("Error! Could not remove file:"),
|
|
|
|
unlinkpath);
|
2000-09-11 15:42:17 +00:00
|
|
|
return_value = -1;
|
|
|
|
}
|
1999-09-27 18:44:28 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +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]
|
|
|
|
<< "CreateTmpDir: tempdir=`" << tempdir << "'\n"
|
|
|
|
<< "CreateTmpDir: mask=`" << mask << "'" << endl;
|
2000-11-08 09:39:46 +00:00
|
|
|
|
|
|
|
string const tmpfl(lyx::tempName(tempdir, mask));
|
|
|
|
// 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)
|
|
|
|
lyx::unlink(tmpfl.c_str());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-02-28 11:56:36 +00:00
|
|
|
if (tmpfl.empty() || lyx::mkdir(tmpfl, 0700)) {
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +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
|
2001-10-24 13:21:20 +00:00
|
|
|
if (Allfiles && DeleteAllFilesInDir(tmpdir)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2000-09-26 13:54:57 +00:00
|
|
|
if (lyx::rmdir(tmpdir)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert(_("Error! Couldn't delete temporary directory:"),
|
|
|
|
tmpdir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2001-03-20 01:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anon
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
string const CreateBufferTmpDir(string const & pathfor)
|
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
|
|
|
static string const tmpdir(pathfor.empty() ? os::getTmpDir() : pathfor);
|
|
|
|
// 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().
|
2001-06-04 23:57:32 +00:00
|
|
|
string const tmpfl = tmpdir + "/lyx_tmpbuf" + tostr(count++);
|
2001-05-17 15:11:01 +00:00
|
|
|
if (lyx::mkdir(tmpfl, 0777)) {
|
|
|
|
WriteFSAlert(_("Error! Couldn't create temporary directory:"),
|
|
|
|
tmpdir);
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
return tmpfl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
string const 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-09-26 13:54:57 +00:00
|
|
|
if (lyx::mkdir(deflt, 0777)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __EMX__
|
2001-10-24 13:21:20 +00:00
|
|
|
Path p(user_lyxdir);
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2001-10-24 13:21:20 +00:00
|
|
|
return CreateTmpDir(deflt, "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__
|
1999-11-01 04:22:28 +00:00
|
|
|
Path p(user_lyxdir);
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2001-10-24 13:21:20 +00:00
|
|
|
return CreateTmpDir("/tmp", "lyx_tmpdir");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
int DestroyLyXTmpDir(string const & tmpdir)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-24 13:21:20 +00:00
|
|
|
return DestroyTmpDir (tmpdir, false); // Why false?
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2001-05-17 15:11:01 +00:00
|
|
|
string temp(strip(os::slashify_path(path), '/'));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (temp.empty()) {
|
|
|
|
WriteAlert(_("Internal error!"),
|
|
|
|
_("Call to createDirectory with invalid name"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
if (lyx::mkdir(temp, permission)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
WriteFSAlert (_("Error! Couldn't create directory:"), temp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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
|
2001-05-17 15:11:01 +00:00
|
|
|
string TempRel(os::slashify_path(RelPath));
|
|
|
|
// 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
|
|
|
|
TempBase = AddPath(lyx::getcwd(), BasePath);
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// 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
|
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, '/');
|
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
|
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
|
2001-05-17 15:11:01 +00:00
|
|
|
return os::slashify_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()) {
|
2001-05-17 15:11:01 +00:00
|
|
|
buf = os::slashify_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 == ".") {
|
2000-11-08 09:39:46 +00:00
|
|
|
return lyx::getcwd() /*GetCWD()*/ + '/' + RTemp;
|
2001-10-24 13:21:20 +00:00
|
|
|
}
|
|
|
|
if (Temp == "~") {
|
1999-10-02 16:21:10 +00:00
|
|
|
return GetEnvPath("HOME") + '/' + 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)
|
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;
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
return TempBase;
|
|
|
|
}
|
|
|
|
|
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();
|
2000-11-06 15:47:22 +00:00
|
|
|
return ofs.str().c_str();
|
2000-06-12 11:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lyxerr << "LyX was not able to read file '" << fname << "'" << endl;
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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]*} '}'
|
|
|
|
//
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
string const 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;
|
2000-05-04 10:57:00 +00:00
|
|
|
result0.erase();
|
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;
|
|
|
|
}
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
string env(GetEnv(res1_contents + 1));
|
1999-10-02 16:21:10 +00:00
|
|
|
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
|
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;
|
2001-05-17 15:11:01 +00:00
|
|
|
string const path2 = os::slashify_path(path_2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (!path.empty() && path != "." && path != "./") {
|
2001-05-17 15:11:01 +00:00
|
|
|
buf = os::slashify_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()) {
|
|
|
|
string::size_type const p2start = path2.find_first_not_of('/');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
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] != '.')
|
2000-10-03 18:38:10 +00:00
|
|
|
ext= "." + extension;
|
1999-09-27 18:44:28 +00:00
|
|
|
else
|
|
|
|
ext = extension;
|
2000-05-11 16:12:46 +00:00
|
|
|
|
2001-05-17 15:11:01 +00:00
|
|
|
return os::slashify_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
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Creates a nice compact path for displaying
|
2000-08-05 05:17:18 +00:00
|
|
|
string const
|
|
|
|
MakeDisplayPath (string const & path, unsigned int threshold)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-10-18 14:07:29 +00:00
|
|
|
string::size_type const l1 = path.length();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// First, we try a relative path compared to home
|
2000-08-05 05:17:18 +00:00
|
|
|
string const home(GetEnvPath("HOME"));
|
1999-10-02 16:21:10 +00:00
|
|
|
string relhome = MakeRelPath(path, home);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-10-18 14:07:29 +00:00
|
|
|
string::size_type 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
|
2001-10-08 14:09:06 +00:00
|
|
|
if (prefixIs(relhome, "../") || os::is_absolute_path(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);
|
2000-11-14 02:01:57 +00:00
|
|
|
string const 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();
|
2000-11-14 02:01:57 +00:00
|
|
|
string const tail =
|
1999-10-02 16:21:10 +00:00
|
|
|
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
|
2000-09-26 13:54:57 +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;
|
2000-11-14 02:01:57 +00:00
|
|
|
LinkBuffer[nRead] = '\0'; // terminator
|
1999-09-27 18:44:28 +00:00
|
|
|
Link = LinkBuffer;
|
|
|
|
return true;
|
|
|
|
}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
typedef pair<int, string> cmdret;
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
cmdret const do_popen(string const & cmd)
|
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
|
2000-09-26 13:54:57 +00:00
|
|
|
FILE * inf = ::popen(cmd.c_str(), "r");
|
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);
|
1999-11-24 22:14:46 +00:00
|
|
|
return make_pair(pret, ret);
|
|
|
|
}
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2000-03-01 18:15:39 +00:00
|
|
|
// If the file can be found directly, we just return a
|
|
|
|
// absolute path version of it.
|
|
|
|
if (FileInfo(fil).exist())
|
|
|
|
return MakeAbsPath(fil);
|
|
|
|
|
1999-11-24 22:14:46 +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
|
|
|
|
2001-10-24 13:21:20 +00:00
|
|
|
cmdret const c = do_popen(kpsecmd);
|
|
|
|
|
|
|
|
lyxerr[Debug::LATEX] << "kpse status = " << c.first << "\n"
|
|
|
|
<< "kpse result = `" << strip(c.second, '\n')
|
|
|
|
<< "'" << endl;
|
2001-06-21 10:55:48 +00:00
|
|
|
if (c.first != -1)
|
2001-10-08 14:09:06 +00:00
|
|
|
return os::internal_path(strip(strip(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);
|
2000-08-08 15:36:25 +00:00
|
|
|
if (fileinfo.exist()) {
|
2000-09-26 13:54:57 +00:00
|
|
|
if (lyx::unlink(a) != 0) {
|
2000-08-08 15:36:25 +00:00
|
|
|
WriteFSAlert(_("Could not delete auto-save file!"), a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|