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"
25 /// remove directory and all contents, returns true on success
26 bool destroyDir(FileName const & tmpdir);
28 /// Creates the per buffer temporary directory
29 std::string const createBufferTmpDir();
31 /// Creates directory. Returns true on success
32 bool createDirectory(FileName const & name, int permissions);
34 /** Creates the global LyX temp dir.
35 \p deflt can be an existing directory name. In this case a new directory
36 inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
37 created and used as the temporary directory.
38 \return the tmp dir name or string() if something went wrong.
40 FileName const createLyXTmpDir(FileName const & deflt);
43 // FIXME unused. Should this be deleted or resurrected?
44 /** Find file by searching several directories.
45 Uses a string of paths separated by ";"s to find a file to open.
46 Can't cope with pathnames with a ';' in them. Returns full path to file.
47 If path entry begins with $$LyX/, use system_lyxdir.
48 If path entry begins with $$User/, use user_lyxdir.
49 Example: "$$User/doc;$$LyX/doc".
51 FileName const fileOpenSearch(std::string const & path,
52 std::string const & name,
53 std::string const & ext = std::string());
56 /// How to search files
58 // The file must exist (return an empty file name otherwise)
60 /// Only do file name expansion, return the complete name even if
61 /// the file does not exist
65 /** Returns the real name of file name in directory path, with optional
67 The file is searched in the given path (unless it is an absolute
68 file name), first directly, and then with extension .ext (if given).
70 FileName const fileSearch(std::string const & path,
71 std::string const & name,
72 std::string const & ext = std::string(),
73 search_mode mode = standard_mode);
75 /// Returns a vector of all files in directory dir having extension ext.
76 std::vector<FileName> const dirList(FileName const & dir,
77 std::string const & ext = std::string());
79 /** Is directory read only?
84 bool isDirWriteable(FileName const & path);
86 /** Is a file readable ?
87 Returns true if the file `path' is readable.
89 bool isFileReadable(FileName const & path);
92 bool isLyXFilename(std::string const & filename);
95 bool isSGMLFilename(std::string const & filename);
98 bool isValidLaTeXFilename(std::string const & filename);
100 /** Returns the path of a library data file.
101 Search the file name.ext in the subdirectory dir of
103 -# build_lyxdir (if not empty)
105 The third parameter `ext' is optional.
107 FileName const libFileSearch(std::string const & dir,
108 std::string const & name,
109 std::string const & ext = std::string());
111 /** Same as libFileSearch(), but tries first to find an
112 internationalized version of the file by prepending $LANG_ to the
116 i18nLibFileSearch(std::string const & dir,
117 std::string const & name,
118 std::string const & ext = std::string());
120 /// How to quote a filename
122 /** Quote for the (OS dependant) shell. This is needed for command
123 line arguments of subprocesses. */
125 /** Quote for python. Use this if you want to store a filename in a
126 python script. Example: \code
127 os << "infile = " << quoteName(filename) << '\\n';
128 \endcode This uses double quotes, so that you can also use this
129 to quote filenames as part of a string if the string is quoted
130 with single quotes. */
134 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
135 * and replaces "$$s/" with the path to the LyX support directory containing
136 * this script. If the script is not found, "$$s/" is removed. Executing the
137 * command will still fail, but the error message will make some sort of
140 std::string const libScriptSearch(std::string const & command,
141 quote_style style = quote_shell);
143 enum latex_path_extension {
148 enum latex_path_dots {
153 /** @param path a file path in internal_path format. Ie, directories
154 * are indicated by '/', not by '\'.
156 * Manipulates @c path into a form suitable for inclusion in a LaTeX
158 * If @c path contains LaTeX special characters, these are escaped.
159 * Eg, '~' -> '\\string~'
160 * If @c path contains spaces, then the returned path is enclosed in
161 * "-quotes. This last fix will lead to successful compiliation of the
162 * LaTeX file only if a sufficiently modern LaTeX compiler is used.
163 * If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
164 * This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
165 * (format=pdflatex 2005.4.11) in combination with
166 * pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
167 * It does not recognize the file extension if it is inside the quotes.
168 * If @c dots == ESCAPE_DOTS dots in the filename are replaced by
169 * "\\lyxdot ". This is needed for the \\includegraphics command if the
170 * automatic format selection is used.
172 std::string const latex_path(std::string const & path,
173 latex_path_extension extension = PROTECT_EXTENSION,
174 latex_path_dots dots = LEAVE_DOTS);
176 /// Substitutes active latex characters with underscores in filename
177 std::string const makeLatexName(std::string const & file);
179 /** Put the name in quotes suitable for the current shell or python,
180 depending on \p style. */
181 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
183 /// Add a filename to a path. Any path from filename is stripped first.
184 std::string const addName(std::string const & path, std::string const & fname);
186 /// Append sub-directory(ies) to path in an intelligent way
187 std::string const addPath(std::string const & path, std::string const & path2);
189 /** Change extension of oldname to extension.
190 If oldname does not have an extension, it is appended.
191 If the extension is empty, any extension is removed from the name.
194 changeExtension(std::string const & oldname, std::string const & extension);
196 /// Remove the extension from \p name
197 std::string const removeExtension(std::string const & name);
199 /** Add the extension \p ext to \p name.
200 Use this instead of changeExtension if you know that \p name is without
201 extension, because changeExtension would wrongly interpret \p name if it
205 addExtension(std::string const & name, std::string const & extension);
207 /// Return the extension of the file (not including the .)
208 std::string const getExtension(std::string const & name);
210 /** Guess the file format name (as in Format::name()) from contents.
211 Normally you don't want to use this directly, but rather
212 Formats::getFormatFromFile().
214 std::string const getFormatFromContents(FileName const & name);
216 /// check for zipped file
217 bool zippedFile(FileName const & name);
219 /** \return the name that LyX will give to the unzipped file \p zipped_file
220 if the second argument of unzipFile() is empty.
222 std::string const unzippedFileName(std::string const & zipped_file);
224 /** Unzip \p zipped_file.
225 The unzipped file is named \p unzipped_file if \p unzipped_file is not
226 empty, and unzippedFileName(\p zipped_file) otherwise.
227 Will overwrite an already existing unzipped file without warning.
229 FileName const unzipFile(FileName const & zipped_file,
230 std::string const & unzipped_file = std::string());
232 /// Returns true is path is absolute
233 bool absolutePath(std::string const & path);
235 /// Create absolute path. If impossible, don't do anything
236 std::string const expandPath(std::string const & path);
238 /** Convert relative path into absolute path based on a basepath.
239 If relpath is absolute, just use that.
240 If basepath doesn't exist use CWD.
242 FileName const makeAbsPath(std::string const & RelPath = std::string(),
243 std::string const & BasePath = std::string());
245 /** Creates a nice compact path for displaying. The parameter
246 threshold, if given, specifies the maximal length of the path.
249 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
251 /** Makes relative path out of absolute path.
252 If it is deeper than basepath,
253 it's easy. If basepath and abspath share something (they are all deeper
254 than some directory), it'll be rendered using ..'s. If they are completely
255 different, then the absolute path will be used as relative path
256 WARNING: the absolute path and base path must really be absolute paths!!!
259 makeRelPath(docstring const & abspath, docstring const & basepath);
261 /// Strip filename from path name
262 std::string const onlyPath(std::string const & fname);
264 /** Normalize a path. Constracts path/../path
265 * Also converts paths like /foo//bar ==> /foo/bar
267 std::string const normalizePath(std::string const & path);
269 /// Strips path from filename
270 std::string const onlyFilename(std::string const & fname);
272 /// Get the contents of a file as a huge std::string
273 std::string const getFileContents(FileName const & fname);
275 /** Check and Replace Environmentvariables ${NAME} in Path.
276 Replaces all occurences of these, if they are found in the
278 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
280 std::string const replaceEnvironmentPath(std::string const & path);
282 /** Set \c link to the path \c file points to as a symbolic link.
283 \return true if successful.
285 bool readLink(FileName const & file, FileName & link);
288 * Search a TeX file in all locations the latex compiler would search it,
289 * with the help of kpsewhich.
290 * The current working directory must be set correctly, so that relative
292 * \param fil The filename to search
293 * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
295 FileName const findtexfile(std::string const & fil,
296 std::string const & format);
298 /// remove the autosave-file and give a Message if it can't be done
299 void removeAutosaveFile(std::string const & filename);
301 /// read the BoundingBox entry from a ps/eps/pdf-file
302 std::string const readBB_from_PSFile(FileName const & file);
304 /** \param file1, file2 the two files to be compared. Must have absolute paths.
305 * \returns 1 if \c file1 has a more recent timestamp than \c file2,
306 * 0 if their timestamps are the same,
307 * -1 if \c file2 has a more recent timestamp than \c file1.
308 * If one of the files does not exist, the return value indicates the file
309 * which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
311 int compare_timestamps(FileName const & file1, FileName const & file2);
313 typedef std::pair<int, std::string> cmd_ret;
315 cmd_ret const runCommand(std::string const & cmd);
317 } // namespace support
320 /// The following functions are implemented in minizip/zipunzip.cpp, and are not in
321 /// the lyx::support namespace
323 /// zip several files to a zipfile. In-zip filenames are also specified
324 bool zipFiles(std::string const & zipfile, std::vector<std::pair<std::string, std::string> > const & files);
326 /// Unzip a zip file to a directory
327 bool unzipToDir(std::string const & zipfile, std::string const & path);