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"
24 /// remove directory and all contents, returns true on success
25 bool destroyDir(std::string const & tmpdir);
27 /// Creates the per buffer temporary directory
28 std::string const createBufferTmpDir();
30 /// Creates directory. Returns true on success
31 bool createDirectory(std::string const & name, int permissions);
33 /** Creates the global LyX temp dir.
34 \p deflt can be an existing directory name. In this case a new directory
35 inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
36 created and used as the temporary directory.
37 \return the tmp dir name or string() if something went wrong.
39 std::string const createLyXTmpDir(std::string const & deflt);
41 /** Find file by searching several directories.
42 Uses a string of paths separated by ";"s to find a file to open.
43 Can't cope with pathnames with a ';' in them. Returns full path to file.
44 If path entry begins with $$LyX/, use system_lyxdir.
45 If path entry begins with $$User/, use user_lyxdir.
46 Example: "$$User/doc;$$LyX/doc".
48 std::string const fileOpenSearch(std::string const & path,
49 std::string const & name,
50 std::string const & ext = std::string());
52 /** Returns the real name of file name in directory path, with optional
54 The file is searched in the given path (unless it is an absolute
55 file name), first directly, and then with extension .ext (if given).
57 std::string const fileSearch(std::string const & path,
58 std::string const & name,
59 std::string const & ext = std::string());
61 /// Returns a vector of all files in directory dir having extension ext.
62 std::vector<std::string> const dirList(std::string const & dir,
63 std::string const & ext = std::string());
65 /** Is directory read only?
70 bool isDirWriteable(std::string const & path);
72 /** Is a file readable ?
73 Returns true if the file `path' is readable.
75 bool isFileReadable (std::string const & path);
78 bool isLyXFilename(std::string const & filename);
81 bool isSGMLFilename(std::string const & filename);
83 /** Returns the path of a library data file.
84 Search the file name.ext in the subdirectory dir of
86 -# build_lyxdir (if not empty)
88 The third parameter `ext' is optional.
90 std::string const libFileSearch(std::string const & dir,
91 std::string const & name,
92 std::string const & ext = std::string());
94 /** Same as libFileSearch(), but tries first to find an
95 internationalized version of the file by prepending $LANG_ to the
99 i18nLibFileSearch(std::string const & dir,
100 std::string const & name,
101 std::string const & ext = std::string());
103 /// How to quote a filename
105 /** Quote for the (OS dependant) shell. This is needed for command
106 line arguments of subprocesses. */
108 /** Quote for python. Use this if you want to store a filename in a
109 python script. Example: \code
110 os << "infile = " << quoteName(filename) << '\\n';
111 \endcode This uses double quotes, so that you can also use this
112 to quote filenames as part of a string if the string is quoted
113 with single quotes. */
117 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
118 * and replaces "$$s/" with the path to the LyX support directory containing
119 * this script. If the script is not found, "$$s/" is removed. Executing the
120 * command will still fail, but the error message will make some sort of
123 std::string const libScriptSearch(std::string const & command,
124 quote_style style = quote_shell);
126 enum latex_path_extension {
131 enum latex_path_dots {
136 /** @param path a file path in internal_path format. Ie, directories
137 * are indicated by '/', not by '\'.
139 * Manipulates @c path into a form suitable for inclusion in a LaTeX
141 * If @c path contains LaTeX special characters, these are escaped.
142 * Eg, '~' -> '\\string~'
143 * If @c path contains spaces, then the returned path is enclosed in
144 * "-quotes. This last fix will lead to successful compiliation of the
145 * LaTeX file only if a sufficiently modern LaTeX compiler is used.
146 * If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
147 * This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
148 * (format=pdflatex 2005.4.11) in combination with
149 * pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
150 * It does not recognize the file extension if it is inside the quotes.
151 * If @c dots == ESCAPE_DOTS dots in the filename are replaced by
152 * "\\lyxdot ". This is needed for the \\includegraphics command if the
153 * automatic format selection is used.
155 std::string const latex_path(std::string const & path,
156 latex_path_extension extension = PROTECT_EXTENSION,
157 latex_path_dots dots = LEAVE_DOTS);
159 /// Substitutes active latex characters with underscores in filename
160 std::string const makeLatexName(std::string const & file);
162 /** Put the name in quotes suitable for the current shell or python,
163 depending on \p style. */
164 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
166 /// Add a filename to a path. Any path from filename is stripped first.
167 std::string const addName(std::string const & path, std::string const & fname);
169 /// Append sub-directory(ies) to path in an intelligent way
170 std::string const addPath(std::string const & path, std::string const & path2);
172 /** Change extension of oldname to extension.
173 If oldname does not have an extension, it is appended.
174 If the extension is empty, any extension is removed from the name.
177 changeExtension(std::string const & oldname, std::string const & extension);
179 /// Remove the extension from \p name
180 std::string const removeExtension(std::string const & name);
182 /// Return the extension of the file (not including the .)
183 std::string const getExtension(std::string const & name);
185 /** Guess the file format name (as in Format::name()) from contents.
186 Normally you don't want to use this directly, but rather
187 Formats::getFormatFromFile().
189 std::string const getFormatFromContents(std::string const & name);
191 /// check for zipped file
192 bool zippedFile(std::string const & name);
194 /** \return the name that LyX will give to the unzipped file \p zipped_file
195 if the second argument of unzipFile() is empty.
197 std::string const unzippedFileName(std::string const & zipped_file);
199 /** Unzip \p zipped_file.
200 The unzipped file is named \p unzipped_file if \p unzipped_file is not
201 empty, and unzippedFileName(\p zipped_file) otherwise.
202 Will overwrite an already existing unzipped file without warning.
204 std::string const unzipFile(std::string const & zipped_file,
205 std::string const & unzipped_file = std::string());
207 /// Returns true is path is absolute
208 bool absolutePath(std::string const & path);
210 /// Create absolute path. If impossible, don't do anything
211 std::string const expandPath(std::string const & path);
213 /** Convert relative path into absolute path based on a basepath.
214 If relpath is absolute, just use that.
215 If basepath doesn't exist use CWD.
217 std::string const makeAbsPath(std::string const & RelPath = std::string(),
218 std::string const & BasePath = std::string());
220 /** Creates a nice compact path for displaying. The parameter
221 threshold, if given, specifies the maximal length of the path.
224 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
226 /** Makes relative path out of absolute path.
227 If it is deeper than basepath,
228 it's easy. If basepath and abspath share something (they are all deeper
229 than some directory), it'll be rendered using ..'s. If they are completely
230 different, then the absolute path will be used as relative path
231 WARNING: the absolute path and base path must really be absolute paths!!!
234 makeRelPath(std::string const & abspath, std::string const & basepath);
236 /// Strip filename from path name
237 std::string const onlyPath(std::string const & fname);
239 /** Normalize a path. Constracts path/../path
240 * Also converts paths like /foo//bar ==> /foo/bar
242 std::string const normalizePath(std::string const & path);
244 /// Strips path from filename
245 std::string const onlyFilename(std::string const & fname);
247 /// Get the contents of a file as a huge std::string
248 std::string const getFileContents(std::string const & fname);
250 /** Check and Replace Environmentvariables ${NAME} in Path.
251 Replaces all occurences of these, if they are found in the
253 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
255 std::string const replaceEnvironmentPath(std::string const & path);
257 /* Set \c link to the path \c file points to as a symbolic link.
258 If \c resolve is true, then \c link is an absolute path
259 Returns true if successful */
260 bool readLink(std::string const & file,
262 bool resolve = false);
264 /// Uses kpsewhich to find tex files
265 std::string const findtexfile(std::string const & fil,
266 std::string const & format);
268 /// remove the autosave-file and give a Message if it can't be done
269 void removeAutosaveFile(std::string const & filename);
271 /// read the BoundingBox entry from a ps/eps/pdf-file
272 std::string const readBB_from_PSFile(std::string const & file);
274 /** \param file1, file2 the two files to be compared. Must have absolute paths.
275 * \returns 1 if \c file1 has a more recent timestamp than \c file2,
276 * 0 if their timestamps are the same,
277 * -1 if \c file2 has a more recent timestamp than \c file1.
278 * If one of the files does not exist, the return value indicates the file
279 * which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
281 int compare_timestamps(std::string const & file1, std::string const & file2);
283 typedef std::pair<int, std::string> cmd_ret;
285 cmd_ret const runCommand(std::string const & cmd);
287 } // namespace support