4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
12 #ifndef LYX_FILETOOL_H
13 #define LYX_FILETOOL_H
15 #include "support/docstring.h"
16 #include "support/FileName.h"
24 /// Creates the per buffer temporary directory
25 std::string const createBufferTmpDir();
27 /** Creates the global LyX temp dir.
28 \p deflt can be an existing directory name. In this case a new directory
29 inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
30 created and used as the temporary directory.
31 \return the tmp dir name or string() if something went wrong.
33 FileName const createLyXTmpDir(FileName const & deflt);
36 // FIXME unused. Should this be deleted or resurrected?
37 /** Find file by searching several directories.
38 Uses a string of paths separated by ";"s to find a file to open.
39 Can't cope with pathnames with a ';' in them. Returns full path to file.
40 If path entry begins with $$LyX/, use system_lyxdir.
41 If path entry begins with $$User/, use user_lyxdir.
42 Example: "$$User/doc;$$LyX/doc".
44 FileName const fileOpenSearch(std::string const & path,
45 std::string const & name,
46 std::string const & ext = std::string());
49 /// How to search files
51 // The file must exist (return an empty file name otherwise)
53 /// Only do file name expansion, return the complete name even if
54 /// the file does not exist
58 /** Returns the real name of file name in directory path, with optional
60 The file is searched in the given path (unless it is an absolute
61 file name), first directly, and then with extension .ext (if given).
63 FileName const fileSearch(std::string const & path,
64 std::string const & name,
65 std::string const & ext = std::string(),
66 search_mode mode = standard_mode);
69 bool isLyXFilename(std::string const & filename);
72 bool isSGMLFilename(std::string const & filename);
75 bool isValidLaTeXFilename(std::string const & filename);
77 /** Returns the path of a library data file.
78 Search the file name.ext in the subdirectory dir of
80 -# build_lyxdir (if not empty)
82 The third parameter `ext' is optional.
84 FileName const libFileSearch(std::string const & dir,
85 std::string const & name,
86 std::string const & ext = std::string());
88 /** Same as libFileSearch(), but tries first to find an
89 internationalized version of the file by prepending $LANG_ to the
93 i18nLibFileSearch(std::string const & dir,
94 std::string const & name,
95 std::string const & ext = std::string());
97 /// How to quote a filename
99 /** Quote for the (OS dependant) shell. This is needed for command
100 line arguments of subprocesses. */
102 /** Quote for python. Use this if you want to store a filename in a
103 python script. Example: \code
104 os << "infile = " << quoteName(filename) << '\\n';
105 \endcode This uses double quotes, so that you can also use this
106 to quote filenames as part of a string if the string is quoted
107 with single quotes. */
111 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
112 * and replaces "$$s/" with the path to the LyX support directory containing
113 * this script. If the script is not found, "$$s/" is removed. Executing the
114 * command will still fail, but the error message will make some sort of
117 std::string const libScriptSearch(std::string const & command,
118 quote_style style = quote_shell);
120 enum latex_path_extension {
125 enum latex_path_dots {
130 /** @param path a file path in internal_path format. Ie, directories
131 * are indicated by '/', not by '\'.
133 * Manipulates @c path into a form suitable for inclusion in a LaTeX
135 * If @c path contains LaTeX special characters, these are escaped.
136 * Eg, '~' -> '\\string~'
137 * If @c path contains spaces, then the returned path is enclosed in
138 * "-quotes. This last fix will lead to successful compiliation of the
139 * LaTeX file only if a sufficiently modern LaTeX compiler is used.
140 * If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
141 * This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
142 * (format=pdflatex 2005.4.11) in combination with
143 * pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
144 * It does not recognize the file extension if it is inside the quotes.
145 * If @c dots == ESCAPE_DOTS dots in the filename are replaced by
146 * "\\lyxdot ". This is needed for the \\includegraphics command if the
147 * automatic format selection is used.
149 std::string const latex_path(std::string const & path,
150 latex_path_extension extension = PROTECT_EXTENSION,
151 latex_path_dots dots = LEAVE_DOTS);
153 /// Substitutes active latex characters with underscores in filename
154 FileName const makeLatexName(FileName const & file);
156 /** Put the name in quotes suitable for the current shell or python,
157 depending on \p style. */
158 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
160 /// Add a filename to a path. Any path from filename is stripped first.
161 std::string const addName(std::string const & path, std::string const & fname);
163 /// Append sub-directory(ies) to path in an intelligent way
164 std::string const addPath(std::string const & path, std::string const & path2);
166 /** Change extension of oldname to extension.
167 If oldname does not have an extension, it is appended.
168 If the extension is empty, any extension is removed from the name.
171 changeExtension(std::string const & oldname, std::string const & extension);
173 /// Remove the extension from \p name
174 std::string const removeExtension(std::string const & name);
176 /** Add the extension \p ext to \p name.
177 Use this instead of changeExtension if you know that \p name is without
178 extension, because changeExtension would wrongly interpret \p name if it
182 addExtension(std::string const & name, std::string const & extension);
184 /// Return the extension of the file (not including the .)
185 std::string const getExtension(std::string const & name);
187 /** \return the name that LyX will give to the unzipped file \p zipped_file
188 if the second argument of unzipFile() is empty.
190 std::string const unzippedFileName(std::string const & zipped_file);
192 /** Unzip \p zipped_file.
193 The unzipped file is named \p unzipped_file if \p unzipped_file is not
194 empty, and unzippedFileName(\p zipped_file) otherwise.
195 Will overwrite an already existing unzipped file without warning.
197 FileName const unzipFile(FileName const & zipped_file,
198 std::string const & unzipped_file = std::string());
200 /// Returns true is path is absolute
201 bool absolutePath(std::string const & path);
203 /// Create absolute path. If impossible, don't do anything
204 std::string const expandPath(std::string const & path);
206 /** Convert relative path into absolute path based on a basepath.
207 If relpath is absolute, just use that.
208 If basepath doesn't exist use CWD.
210 FileName const makeAbsPath(std::string const & RelPath = std::string(),
211 std::string const & BasePath = std::string());
213 /** Creates a nice compact path for displaying. The parameter
214 threshold, if given, specifies the maximal length of the path.
217 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
219 /** Makes relative path out of absolute path.
220 If it is deeper than basepath,
221 it's easy. If basepath and abspath share something (they are all deeper
222 than some directory), it'll be rendered using ..'s. If they are completely
223 different, then the absolute path will be used as relative path
224 WARNING: the absolute path and base path must really be absolute paths!!!
227 makeRelPath(docstring const & abspath, docstring const & basepath);
229 /// Strip filename from path name
230 std::string const onlyPath(std::string const & fname);
232 /// Strips path from filename
233 std::string const onlyFilename(std::string const & fname);
235 /** Check and Replace Environmentvariables ${NAME} in Path.
236 Replaces all occurences of these, if they are found in the
238 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
240 std::string const replaceEnvironmentPath(std::string const & path);
242 /** Set \c link to the path \c file points to as a symbolic link.
243 \return true if successful.
245 bool readLink(FileName const & file, FileName & link);
248 * Search a TeX file in all locations the latex compiler would search it,
249 * with the help of kpsewhich.
250 * The current working directory must be set correctly, so that relative
252 * \param fil The filename to search
253 * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
255 FileName const findtexfile(std::string const & fil,
256 std::string const & format);
258 /// remove the autosave-file and give a Message if it can't be done
259 void removeAutosaveFile(std::string const & filename);
261 /// read the BoundingBox entry from a ps/eps/pdf-file
262 std::string const readBB_from_PSFile(FileName const & file);
264 /** \param file1, file2 the two files to be compared. Must have absolute paths.
265 * \returns 1 if \c file1 has a more recent timestamp than \c file2,
266 * 0 if their timestamps are the same,
267 * -1 if \c file2 has a more recent timestamp than \c file1.
268 * If one of the files does not exist, the return value indicates the file
269 * which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
271 int compare_timestamps(FileName const & file1, FileName const & file2);
273 typedef std::pair<int, std::string> cmd_ret;
275 cmd_ret const runCommand(std::string const & cmd);
278 } // namespace support