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"
25 /** Creates the global LyX temp dir.
26 \p deflt can be an existing directory name. In this case a new directory
27 inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
28 created and used as the temporary directory.
29 \return the tmp dir name or string() if something went wrong.
31 FileName const createLyXTmpDir(FileName const & deflt);
34 // FIXME unused. Should this be deleted or resurrected?
35 /** Find file by searching several directories.
36 Uses a string of paths separated by ";"s to find a file to open.
37 Can't cope with pathnames with a ';' in them. Returns full path to file.
38 If path entry begins with $$LyX/, use system_lyxdir.
39 If path entry begins with $$User/, use user_lyxdir.
40 Example: "$$User/doc;$$LyX/doc".
42 FileName const fileOpenSearch(std::string const & path,
43 std::string const & name,
44 std::string const & ext = std::string());
47 /// How to search files
49 /// The file must exist (return an empty file name otherwise)
51 /// Only do file name expansion, return the complete name even if
52 /// the file does not exist
56 /** Returns the real name of file name in directory path, with optional
58 The file is searched in the given path (unless it is an absolute
59 file name), first directly, and then with extension .ext (if given).
61 FileName const fileSearch(std::string const & path,
62 std::string const & name,
63 std::string const & ext = std::string(),
64 search_mode mode = must_exist);
67 bool isLyXFileName(std::string const & filename);
70 bool isSGMLFileName(std::string const & filename);
72 /// check for characters in filenames not allowed by LaTeX
73 bool isValidLaTeXFileName(std::string const & filename);
75 /** check for characters in filenames that might lead to
76 problems when manually compiling the LaTeX export of LyX
77 and opening the result with some older DVI-viewers
79 bool isValidDVIFileName(std::string const & filename);
81 /** Returns the path of a library data file.
82 Search the file name.ext in the subdirectory dir of
84 -# build_lyxdir (if not empty)
86 The third parameter `ext' is optional.
88 FileName const libFileSearch(std::string const & dir,
89 std::string const & name,
90 std::string const & ext = std::string());
92 /** Same as libFileSearch(), but tries first to find an
93 internationalized version of the file by prepending $LANG_ to the
97 i18nLibFileSearch(std::string const & dir,
98 std::string const & name,
99 std::string const & ext = std::string());
101 /** Same as libFileSearch(), but tries first to find a preferred
102 version of the file by appending the icon set name to the dir
105 imageLibFileSearch(std::string & dir, std::string const & name,
106 std::string const & ext = std::string());
108 /// How to quote a filename
110 /** Quote for the (OS dependant) shell. This is needed for command
111 line arguments of subprocesses. */
113 /** Quote for python. Use this if you want to store a filename in a
114 python script. Example: \code
115 os << "infile = " << quoteName(filename) << '\\n';
116 \endcode This uses double quotes, so that you can also use this
117 to quote filenames as part of a string if the string is quoted
118 with single quotes. */
122 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
123 * and replaces "$$s/" with the path to the LyX support directory containing
124 * this script. If the script is not found, "$$s/" is removed. Executing the
125 * command will still fail, but the error message will make some sort of
128 std::string const libScriptSearch(std::string const & command,
129 quote_style style = quote_shell);
131 enum latex_path_extension {
136 enum latex_path_dots {
141 /** @param path a file path in internal_path format. Ie, directories
142 * are indicated by '/', not by '\'.
144 * Manipulates @c path into a form suitable for inclusion in a LaTeX
146 * If @c path contains LaTeX special characters, these are escaped.
147 * Eg, '~' -> '\\string~'
148 * If @c path contains spaces, then the returned path is enclosed in
149 * "-quotes. This last fix will lead to successful compiliation of the
150 * LaTeX file only if a sufficiently modern LaTeX compiler is used.
151 * If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
152 * This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
153 * (format=pdflatex 2005.4.11) in combination with
154 * pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
155 * It does not recognize the file extension if it is inside the quotes.
156 * If @c dots == ESCAPE_DOTS dots in the filename are replaced by
157 * "\\lyxdot ". This is needed for the \\includegraphics command if the
158 * automatic format selection is used.
160 std::string const latex_path(std::string const & path,
161 latex_path_extension extension = PROTECT_EXTENSION,
162 latex_path_dots dots = LEAVE_DOTS);
164 /// Substitutes active latex characters with underscores in filename
165 FileName const makeLatexName(FileName const & file);
167 /** Put the name in quotes suitable for the current shell or python,
168 depending on \p style. */
169 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
171 /// Add a filename to a path. Any path from filename is stripped first.
172 std::string const addName(std::string const & path, std::string const & fname);
174 /// Append sub-directory(ies) to path in an intelligent way
175 std::string const addPath(std::string const & path, std::string const & path2);
177 /** Change extension of oldname to extension.
178 If oldname does not have an extension, it is appended.
179 If the extension is empty, any extension is removed from the name.
182 changeExtension(std::string const & oldname, std::string const & extension);
184 /// Remove the extension from \p name
185 std::string const removeExtension(std::string const & name);
187 /** Add the extension \p ext to \p name.
188 Use this instead of changeExtension if you know that \p name is without
189 extension, because changeExtension would wrongly interpret \p name if it
193 addExtension(std::string const & name, std::string const & extension);
195 /// Return the extension of the file (not including the .)
196 std::string const getExtension(std::string const & name);
198 /** \return the name that LyX will give to the unzipped file \p zipped_file
199 if the second argument of unzipFile() is empty.
201 std::string const unzippedFileName(std::string const & zipped_file);
203 /** Unzip \p zipped_file.
204 The unzipped file is named \p unzipped_file if \p unzipped_file is not
205 empty, and unzippedFileName(\p zipped_file) otherwise.
206 Will overwrite an already existing unzipped file without warning.
208 FileName const unzipFile(FileName const & zipped_file,
209 std::string const & unzipped_file = std::string());
211 /// Create absolute path. If impossible, don't do anything
212 std::string const expandPath(std::string const & path);
214 /** Convert relative path into absolute path based on a basepath.
215 If relpath is absolute, just use that.
216 If basepath doesn't exist use CWD.
218 FileName const makeAbsPath(std::string const & RelPath = std::string(),
219 std::string const & BasePath = std::string());
221 /** Creates a nice compact path for displaying. The parameter
222 threshold, if given, specifies the maximal length of the path.
225 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
227 /** Makes relative path out of absolute path.
228 If it is deeper than basepath,
229 it's easy. If basepath and abspath share something (they are all deeper
230 than some directory), it'll be rendered using ..'s. If they are completely
231 different, then the absolute path will be used as relative path
232 WARNING: the absolute path and base path must really be absolute paths!!!
235 makeRelPath(docstring const & abspath, docstring const & basepath);
237 /// Strip filename from path name
238 std::string const onlyPath(std::string const & fname);
240 /// Strips path from filename
241 std::string const onlyFileName(std::string const & fname);
243 /** Check and Replace Environmentvariables ${NAME} in Path.
244 Replaces all occurences of these, if they are found in the
246 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
248 std::string const replaceEnvironmentPath(std::string const & path);
251 Return a string to be used as a prefix to a command for setting the
252 environment of the TeX engine with respect to the path \p path.
254 std::string latexEnvCmdPrefix(std::string const & path);
256 /** Replace all references to a current directory (a lonely '.' or
257 the prefix "./") in \c pathlist with \c path. Also prefixes
258 all non-absolute paths with \c path.
260 std::string const replaceCurdirPath(std::string const & path, std::string const & pathlist);
262 /** Set \c link to the path \c file points to as a symbolic link.
263 \return true if successful.
265 bool readLink(FileName const & file, FileName & link);
268 * Search a TeX file in all locations the latex compiler would search it,
269 * with the help of kpsewhich.
270 * The current working directory must be set correctly, so that relative
272 * \param fil The filename to search
273 * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
275 FileName const findtexfile(std::string const & fil,
276 std::string const & format);
278 /** \param file1, file2 the two files to be compared. Must have absolute paths.
279 * \returns 1 if \c file1 has a more recent timestamp than \c file2,
280 * 0 if their timestamps are the same,
281 * -1 if \c file2 has a more recent timestamp than \c file1.
282 * If one of the files does not exist, the return value indicates the file
283 * which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
285 int compare_timestamps(FileName const & file1, FileName const & file2);
287 /// \param lfuns: true if we're converting lfuns, false if prefs
288 bool prefs2prefs(FileName const & filename, FileName const & tempfile,
291 typedef std::pair<int, std::string> cmd_ret;
293 cmd_ret const runCommand(std::string const & cmd);
295 int fileLock(const char * lock_file);
296 void fileUnlock(int fd, const char * lock_file);
298 } // namespace support