2003-08-23 00:17:00 +00:00
|
|
|
|
// -*- C++ -*-
|
2002-09-25 10:03:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file filetools.h
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2003-06-18 09:56:10 +00:00
|
|
|
|
*
|
2003-07-28 22:37:28 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2003-06-18 09:56:10 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 10:03:41 +00:00
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#ifndef LYX_FILETOOL_H
|
|
|
|
|
#define LYX_FILETOOL_H
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
#include "support/docstring.h"
|
2006-11-26 21:30:39 +00:00
|
|
|
|
#include "support/filename.h"
|
2006-09-11 08:54:10 +00:00
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
#include <vector>
|
2002-07-22 17:11:41 +00:00
|
|
|
|
#include <utility>
|
2003-10-06 15:43:21 +00:00
|
|
|
|
#include <string>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace support {
|
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
/// remove directory and all contents, returns true on success
|
|
|
|
|
bool destroyDir(std::string const & tmpdir);
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
/// Creates the per buffer temporary directory
|
|
|
|
|
std::string const createBufferTmpDir();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-03-29 09:02:08 +00:00
|
|
|
|
/// Creates directory. Returns true on success
|
2003-10-06 15:43:21 +00:00
|
|
|
|
bool createDirectory(std::string const & name, int permissions);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
/** Creates the global LyX temp dir.
|
|
|
|
|
\p deflt can be an existing directory name. In this case a new directory
|
|
|
|
|
inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
|
|
|
|
|
created and used as the temporary directory.
|
|
|
|
|
\return the tmp dir name or string() if something went wrong.
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const createLyXTmpDir(FileName const & deflt);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/** Find file by searching several directories.
|
|
|
|
|
Uses a string of paths separated by ";"s to find a file to open.
|
|
|
|
|
Can't cope with pathnames with a ';' in them. Returns full path to file.
|
|
|
|
|
If path entry begins with $$LyX/, use system_lyxdir.
|
|
|
|
|
If path entry begins with $$User/, use user_lyxdir.
|
|
|
|
|
Example: "$$User/doc;$$LyX/doc".
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const fileOpenSearch(std::string const & path,
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const & name,
|
|
|
|
|
std::string const & ext = std::string());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/** Returns the real name of file name in directory path, with optional
|
|
|
|
|
extension ext.
|
|
|
|
|
The file is searched in the given path (unless it is an absolute
|
|
|
|
|
file name), first directly, and then with extension .ext (if given).
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const fileSearch(std::string const & path,
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const & name,
|
|
|
|
|
std::string const & ext = std::string());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
/// Returns a vector of all files in directory dir having extension ext.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::vector<std::string> const dirList(std::string const & dir,
|
|
|
|
|
std::string const & ext = std::string());
|
2000-11-08 09:39:46 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
/** Is directory read only?
|
2002-03-21 17:09:55 +00:00
|
|
|
|
returns
|
2001-08-01 10:08:53 +00:00
|
|
|
|
true: dir writeable
|
|
|
|
|
false: not writeable
|
2000-08-07 20:58:24 +00:00
|
|
|
|
*/
|
2006-04-08 22:31:11 +00:00
|
|
|
|
bool isDirWriteable(std::string const & path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/** Is a file readable ?
|
|
|
|
|
Returns true if the file `path' is readable.
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool isFileReadable(FileName const & path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
///
|
2006-04-08 09:09:57 +00:00
|
|
|
|
bool isLyXFilename(std::string const & filename);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2000-11-13 15:43:36 +00:00
|
|
|
|
///
|
2006-04-08 09:09:57 +00:00
|
|
|
|
bool isSGMLFilename(std::string const & filename);
|
2000-11-13 15:43:36 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
/** Returns the path of a library data file.
|
2005-07-08 15:43:46 +00:00
|
|
|
|
Search the file name.ext in the subdirectory dir of
|
|
|
|
|
-# user_lyxdir
|
|
|
|
|
-# build_lyxdir (if not empty)
|
|
|
|
|
-# system_lyxdir
|
1999-09-27 18:44:28 +00:00
|
|
|
|
The third parameter `ext' is optional.
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const libFileSearch(std::string const & dir,
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const & name,
|
|
|
|
|
std::string const & ext = std::string());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
/** Same as libFileSearch(), but tries first to find an
|
1999-09-27 18:44:28 +00:00
|
|
|
|
internationalized version of the file by prepending $LANG_ to the
|
2002-03-21 17:09:55 +00:00
|
|
|
|
name
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const
|
2006-04-08 22:31:11 +00:00
|
|
|
|
i18nLibFileSearch(std::string const & dir,
|
|
|
|
|
std::string const & name,
|
2003-10-06 15:43:21 +00:00
|
|
|
|
std::string const & ext = std::string());
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
2006-09-11 16:07:49 +00:00
|
|
|
|
/// How to quote a filename
|
|
|
|
|
enum quote_style {
|
|
|
|
|
/** Quote for the (OS dependant) shell. This is needed for command
|
|
|
|
|
line arguments of subprocesses. */
|
|
|
|
|
quote_shell,
|
|
|
|
|
/** Quote for python. Use this if you want to store a filename in a
|
|
|
|
|
python script. Example: \code
|
|
|
|
|
os << "infile = " << quoteName(filename) << '\\n';
|
|
|
|
|
\endcode This uses double quotes, so that you can also use this
|
|
|
|
|
to quote filenames as part of a string if the string is quoted
|
|
|
|
|
with single quotes. */
|
|
|
|
|
quote_python
|
|
|
|
|
};
|
|
|
|
|
|
2006-06-26 14:17:12 +00:00
|
|
|
|
/** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
|
2005-01-20 15:02:16 +00:00
|
|
|
|
* and replaces "$$s/" with the path to the LyX support directory containing
|
|
|
|
|
* this script. If the script is not found, "$$s/" is removed. Executing the
|
|
|
|
|
* command will still fail, but the error message will make some sort of
|
|
|
|
|
* sense ;-)
|
2003-06-01 20:49:48 +00:00
|
|
|
|
*/
|
2006-09-11 16:07:49 +00:00
|
|
|
|
std::string const libScriptSearch(std::string const & command,
|
|
|
|
|
quote_style style = quote_shell);
|
2002-03-27 00:05:28 +00:00
|
|
|
|
|
2005-07-10 09:31:27 +00:00
|
|
|
|
enum latex_path_extension {
|
|
|
|
|
PROTECT_EXTENSION,
|
|
|
|
|
EXCLUDE_EXTENSION
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum latex_path_dots {
|
|
|
|
|
LEAVE_DOTS,
|
|
|
|
|
ESCAPE_DOTS
|
|
|
|
|
};
|
|
|
|
|
|
2005-04-18 17:43:11 +00:00
|
|
|
|
/** @param path a file path in internal_path format. Ie, directories
|
|
|
|
|
* are indicated by '/', not by '\'.
|
|
|
|
|
*
|
|
|
|
|
* Manipulates @c path into a form suitable for inclusion in a LaTeX
|
2005-04-26 11:12:20 +00:00
|
|
|
|
* document.
|
2005-04-18 17:43:11 +00:00
|
|
|
|
* If @c path contains LaTeX special characters, these are escaped.
|
2005-07-08 15:43:46 +00:00
|
|
|
|
* Eg, '~' -> '\\string~'
|
2005-04-18 17:43:11 +00:00
|
|
|
|
* If @c path contains spaces, then the returned path is enclosed in
|
|
|
|
|
* "-quotes. This last fix will lead to successful compiliation of the
|
|
|
|
|
* LaTeX file only if a sufficiently modern LaTeX compiler is used.
|
2005-07-10 09:31:27 +00:00
|
|
|
|
* If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
|
2005-07-08 15:43:46 +00:00
|
|
|
|
* This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
|
|
|
|
|
* (format=pdflatex 2005.4.11) in combination with
|
|
|
|
|
* pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
|
|
|
|
|
* It does not recognize the file extension if it is inside the quotes.
|
2005-07-10 09:31:27 +00:00
|
|
|
|
* If @c dots == ESCAPE_DOTS dots in the filename are replaced by
|
|
|
|
|
* "\\lyxdot ". This is needed for the \\includegraphics command if the
|
|
|
|
|
* automatic format selection is used.
|
2005-04-18 17:43:11 +00:00
|
|
|
|
*/
|
2005-07-08 15:43:46 +00:00
|
|
|
|
std::string const latex_path(std::string const & path,
|
2005-07-10 09:31:27 +00:00
|
|
|
|
latex_path_extension extension = PROTECT_EXTENSION,
|
|
|
|
|
latex_path_dots dots = LEAVE_DOTS);
|
2005-04-18 17:43:11 +00:00
|
|
|
|
|
1999-12-03 13:51:01 +00:00
|
|
|
|
/// Substitutes active latex characters with underscores in filename
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const makeLatexName(std::string const & file);
|
1999-12-03 13:51:01 +00:00
|
|
|
|
|
2006-09-11 16:07:49 +00:00
|
|
|
|
/** Put the name in quotes suitable for the current shell or python,
|
|
|
|
|
depending on \p style. */
|
|
|
|
|
std::string const quoteName(std::string const & file, quote_style style = quote_shell);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/// Add a filename to a path. Any path from filename is stripped first.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const addName(std::string const & path, std::string const & fname);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/// Append sub-directory(ies) to path in an intelligent way
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const addPath(std::string const & path, std::string const & path2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/** Change extension of oldname to extension.
|
|
|
|
|
If oldname does not have an extension, it is appended.
|
|
|
|
|
If the extension is empty, any extension is removed from the name.
|
|
|
|
|
*/
|
2003-10-06 15:43:21 +00:00
|
|
|
|
std::string const
|
2006-04-08 22:31:11 +00:00
|
|
|
|
changeExtension(std::string const & oldname, std::string const & extension);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-01 10:49:58 +00:00
|
|
|
|
/// Remove the extension from \p name
|
|
|
|
|
std::string const removeExtension(std::string const & name);
|
|
|
|
|
|
2000-08-30 03:40:51 +00:00
|
|
|
|
/// Return the extension of the file (not including the .)
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const getExtension(std::string const & name);
|
2000-08-30 03:40:51 +00:00
|
|
|
|
|
2004-11-09 19:08:34 +00:00
|
|
|
|
/** Guess the file format name (as in Format::name()) from contents.
|
|
|
|
|
Normally you don't want to use this directly, but rather
|
|
|
|
|
Formats::getFormatFromFile().
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
std::string const getFormatFromContents(FileName const & name);
|
2002-01-31 14:20:09 +00:00
|
|
|
|
|
2002-02-05 12:19:32 +00:00
|
|
|
|
/// check for zipped file
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool zippedFile(FileName const & name);
|
2002-02-05 12:19:32 +00:00
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
/** \return the name that LyX will give to the unzipped file \p zipped_file
|
|
|
|
|
if the second argument of unzipFile() is empty.
|
|
|
|
|
*/
|
2003-10-06 15:43:21 +00:00
|
|
|
|
std::string const unzippedFileName(std::string const & zipped_file);
|
2003-09-03 17:23:38 +00:00
|
|
|
|
|
2004-02-25 12:00:53 +00:00
|
|
|
|
/** Unzip \p zipped_file.
|
|
|
|
|
The unzipped file is named \p unzipped_file if \p unzipped_file is not
|
|
|
|
|
empty, and unzippedFileName(\p zipped_file) otherwise.
|
|
|
|
|
Will overwrite an already existing unzipped file without warning.
|
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const unzipFile(FileName const & zipped_file,
|
|
|
|
|
std::string const & unzipped_file = std::string());
|
2002-02-07 19:37:34 +00:00
|
|
|
|
|
2001-10-08 14:09:06 +00:00
|
|
|
|
/// Returns true is path is absolute
|
2006-04-08 22:31:11 +00:00
|
|
|
|
bool absolutePath(std::string const & path);
|
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
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const expandPath(std::string const & path);
|
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 doesn't exist use CWD.
|
|
|
|
|
*/
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const makeAbsPath(std::string const & RelPath = std::string(),
|
|
|
|
|
std::string const & BasePath = std::string());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/** Creates a nice compact path for displaying. The parameter
|
|
|
|
|
threshold, if given, specifies the maximal length of the path.
|
|
|
|
|
*/
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const
|
2006-04-08 22:31:11 +00:00
|
|
|
|
makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
|
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
|
|
|
|
|
WARNING: the absolute path and base path must really be absolute paths!!!
|
|
|
|
|
*/
|
2003-10-06 15:43:21 +00:00
|
|
|
|
std::string const
|
2006-04-08 22:31:11 +00:00
|
|
|
|
makeRelPath(std::string const & abspath, std::string const & basepath);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/// Strip filename from path name
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const onlyPath(std::string const & fname);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-06-20 14:03:49 +00:00
|
|
|
|
/** Normalize a path. Constracts path/../path
|
|
|
|
|
* Also converts paths like /foo//bar ==> /foo/bar
|
|
|
|
|
*/
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const normalizePath(std::string const & path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/// Strips path from filename
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const onlyFilename(std::string const & fname);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
/// Get the contents of a file as a huge std::string
|
2006-11-26 21:30:39 +00:00
|
|
|
|
std::string const getFileContents(FileName const & fname);
|
2000-06-12 11:27:15 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
/** Check and Replace Environmentvariables ${NAME} in Path.
|
|
|
|
|
Replaces all occurences of these, if they are found in the
|
|
|
|
|
environment.
|
|
|
|
|
Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
|
|
|
|
|
*/
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const replaceEnvironmentPath(std::string const & path);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-07-17 21:43:06 +00:00
|
|
|
|
/* Set \c link to the path \c file points to as a symbolic link.
|
|
|
|
|
If \c resolve is true, then \c link is an absolute path
|
2000-05-30 15:41:16 +00:00
|
|
|
|
Returns true if successful */
|
2006-04-08 22:31:11 +00:00
|
|
|
|
bool readLink(std::string const & file,
|
|
|
|
|
std::string & link,
|
|
|
|
|
bool resolve = false);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
|
/// Uses kpsewhich to find tex files
|
2006-12-01 20:09:08 +00:00
|
|
|
|
FileName const findtexfile(std::string const & fil,
|
2006-04-08 22:31:11 +00:00
|
|
|
|
std::string const & format);
|
1999-11-24 22:14:46 +00:00
|
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
|
/// remove the autosave-file and give a Message if it can't be done
|
2003-10-06 15:43:21 +00:00
|
|
|
|
void removeAutosaveFile(std::string const & filename);
|
2000-08-08 15:36:25 +00:00
|
|
|
|
|
2002-04-08 14:55:52 +00:00
|
|
|
|
/// read the BoundingBox entry from a ps/eps/pdf-file
|
2006-11-26 21:30:39 +00:00
|
|
|
|
std::string const readBB_from_PSFile(FileName const & file);
|
2002-04-08 14:55:52 +00:00
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
|
/** \param file1, file2 the two files to be compared. Must have absolute paths.
|
|
|
|
|
* \returns 1 if \c file1 has a more recent timestamp than \c file2,
|
|
|
|
|
* 0 if their timestamps are the same,
|
|
|
|
|
* -1 if \c file2 has a more recent timestamp than \c file1.
|
|
|
|
|
* If one of the files does not exist, the return value indicates the file
|
|
|
|
|
* which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
|
2003-06-03 10:14:52 +00:00
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
|
int compare_timestamps(FileName const & file1, FileName const & file2);
|
2003-06-03 10:14:52 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
typedef std::pair<int, std::string> cmd_ret;
|
2002-07-22 17:11:41 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
cmd_ret const runCommand(std::string const & cmd);
|
2002-01-31 14:20:09 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
} // namespace support
|
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|