]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
cosmetics
[lyx.git] / src / support / filetools.h
1 // -*- C++ -*-
2 /**
3  * \file filetools.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_FILETOOL_H
13 #define LYX_FILETOOL_H
14
15 #include "support/docstring.h"
16 #include "support/FileName.h"
17
18 #include <vector>
19 #include <utility>
20 #include <string>
21
22 namespace lyx {
23 namespace support {
24
25 /// Creates the per buffer temporary directory
26 std::string const createBufferTmpDir();
27
28 /** Creates the global LyX temp dir.
29   \p deflt can be an existing directory name. In this case a new directory
30   inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
31   created and used as the temporary directory.
32   \return the tmp dir name or string() if something went wrong.
33  */
34 FileName const createLyXTmpDir(FileName const & deflt);
35
36 #if 0
37 // FIXME unused. Should this be deleted or resurrected?
38 /** Find file by searching several directories.
39   Uses a string of paths separated by ";"s to find a file to open.
40     Can't cope with pathnames with a ';' in them. Returns full path to file.
41     If path entry begins with $$LyX/, use system_lyxdir.
42     If path entry begins with $$User/, use user_lyxdir.
43     Example: "$$User/doc;$$LyX/doc".
44 */
45 FileName const fileOpenSearch(std::string const & path,
46                                  std::string const & name,
47                                  std::string const & ext = std::string());
48 #endif
49
50 /// How to search files
51 enum search_mode {
52         // The file must exist (return an empty file name otherwise)
53         standard_mode,
54         /// Only do file name expansion, return the complete name even if
55         /// the file does not exist
56         allow_unreadable
57 };
58
59 /** Returns the real name of file name in directory path, with optional
60   extension ext.
61   The file is searched in the given path (unless it is an absolute
62   file name), first directly, and then with extension .ext (if given).
63   */
64 FileName const fileSearch(std::string const & path,
65                              std::string const & name,
66                              std::string const & ext = std::string(),
67                              search_mode mode = standard_mode);
68
69 /// Returns a vector of all files in directory dir having extension ext.
70 std::vector<FileName> const dirList(FileName const & dir,
71                                        std::string const & ext = std::string());
72
73 ///
74 bool isLyXFilename(std::string const & filename);
75
76 ///
77 bool isSGMLFilename(std::string const & filename);
78
79 ///
80 bool isValidLaTeXFilename(std::string const & filename);
81
82 /** Returns the path of a library data file.
83     Search the file name.ext in the subdirectory dir of
84       -# user_lyxdir
85       -# build_lyxdir (if not empty)
86       -# system_lyxdir
87     The third parameter `ext' is optional.
88 */
89 FileName const libFileSearch(std::string const & dir,
90                                 std::string const & name,
91                                 std::string const & ext = std::string());
92
93 /** Same as libFileSearch(), but tries first to find an
94   internationalized version of the file by prepending $LANG_ to the
95   name
96   */
97 FileName const
98 i18nLibFileSearch(std::string const & dir,
99                   std::string const & name,
100                   std::string const & ext = std::string());
101
102 /// How to quote a filename
103 enum quote_style {
104         /** Quote for the (OS dependant) shell. This is needed for command
105             line arguments of subprocesses. */
106         quote_shell,
107         /** Quote for python. Use this if you want to store a filename in a
108             python script. Example: \code
109             os << "infile = " << quoteName(filename) << '\\n';
110             \endcode This uses double quotes, so that you can also use this
111             to quote filenames as part of a string if the string is quoted
112             with single quotes. */
113         quote_python
114 };
115
116 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
117  *  and replaces "$$s/" with the path to the LyX support directory containing
118  *  this script. If the script is not found, "$$s/" is removed. Executing the
119  *  command will still fail, but the error message will make some sort of
120  *  sense ;-)
121  */
122 std::string const libScriptSearch(std::string const & command,
123                 quote_style style = quote_shell);
124
125 enum latex_path_extension {
126         PROTECT_EXTENSION,
127         EXCLUDE_EXTENSION
128 };
129
130 enum latex_path_dots {
131         LEAVE_DOTS,
132         ESCAPE_DOTS
133 };
134
135 /** @param path a file path in internal_path format. Ie, directories
136  *  are indicated by '/', not by '\'.
137  *
138  *  Manipulates @c path into a form suitable for inclusion in a LaTeX
139  *  document.
140  *  If @c path contains LaTeX special characters, these are escaped.
141  *  Eg, '~' -> '\\string~'
142  *  If @c path contains spaces, then the returned path is enclosed in
143  *  "-quotes. This last fix will lead to successful compiliation of the
144  *  LaTeX file only if a sufficiently modern LaTeX compiler is used.
145  *  If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
146  *  This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
147  *  (format=pdflatex 2005.4.11) in combination with
148  *  pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
149  *  It does not recognize the file extension if it is inside the quotes.
150  *  If @c dots == ESCAPE_DOTS dots in the filename are replaced by
151  *  "\\lyxdot ". This is needed for the \\includegraphics command if the
152  *  automatic format selection is used.
153  */
154 std::string const latex_path(std::string const & path,
155                 latex_path_extension extension = PROTECT_EXTENSION,
156                 latex_path_dots dots = LEAVE_DOTS);
157
158 /// Substitutes active latex characters with underscores in filename
159 std::string const makeLatexName(std::string const & file);
160
161 /** Put the name in quotes suitable for the current shell or python,
162     depending on \p style. */
163 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
164
165 /// Add a filename to a path. Any path from filename is stripped first.
166 std::string const addName(std::string const & path, std::string const & fname);
167
168 /// Append sub-directory(ies) to path in an intelligent way
169 std::string const addPath(std::string const & path, std::string const & path2);
170
171 /** Change extension of oldname to extension.
172  If oldname does not have an extension, it is appended.
173  If the extension is empty, any extension is removed from the name.
174  */
175 std::string const
176 changeExtension(std::string const & oldname, std::string const & extension);
177
178 /// Remove the extension from \p name
179 std::string const removeExtension(std::string const & name);
180
181 /** Add the extension \p ext to \p name.
182  Use this instead of changeExtension if you know that \p name is without
183  extension, because changeExtension would wrongly interpret \p name if it
184  contains a dot.
185  */
186 std::string const
187 addExtension(std::string const & name, std::string const & extension);
188
189 /// Return the extension of the file (not including the .)
190 std::string const getExtension(std::string const & name);
191
192 /** \return the name that LyX will give to the unzipped file \p zipped_file
193   if the second argument of unzipFile() is empty.
194  */
195 std::string const unzippedFileName(std::string const & zipped_file);
196
197 /** Unzip \p zipped_file.
198   The unzipped file is named \p unzipped_file if \p unzipped_file is not
199   empty, and unzippedFileName(\p zipped_file) otherwise.
200   Will overwrite an already existing unzipped file without warning.
201  */
202 FileName const unzipFile(FileName const & zipped_file,
203                          std::string const & unzipped_file = std::string());
204
205 /// Returns true is path is absolute
206 bool absolutePath(std::string const & path);
207
208 /// Create absolute path. If impossible, don't do anything
209 std::string const expandPath(std::string const & path);
210
211 /** Convert relative path into absolute path based on a basepath.
212   If relpath is absolute, just use that.
213   If basepath doesn't exist use CWD.
214   */
215 FileName const makeAbsPath(std::string const & RelPath = std::string(),
216                               std::string const & BasePath = std::string());
217
218 /** Creates a nice compact path for displaying. The parameter
219   threshold, if given, specifies the maximal length of the path.
220   */
221 docstring const
222 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
223
224 /** Makes relative path out of absolute path.
225   If it is deeper than basepath,
226   it's easy. If basepath and abspath share something (they are all deeper
227   than some directory), it'll be rendered using ..'s. If they are completely
228   different, then the absolute path will be used as relative path
229   WARNING: the absolute path and base path must really be absolute paths!!!
230   */
231 docstring const
232 makeRelPath(docstring const & abspath, docstring const & basepath);
233
234 /// Strip filename from path name
235 std::string const onlyPath(std::string const & fname);
236
237 /** Normalize a path. Constracts path/../path
238  *  Also converts paths like /foo//bar ==> /foo/bar
239  */
240 std::string const normalizePath(std::string const & path);
241
242 /// Strips path from filename
243 std::string const onlyFilename(std::string const & fname);
244
245 /** Check and Replace Environmentvariables ${NAME} in Path.
246     Replaces all occurences of these, if they are found in the
247     environment.
248     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
249 */
250 std::string const replaceEnvironmentPath(std::string const & path);
251
252 /** Set \c link to the path \c file points to as a symbolic link.
253     \return true if successful.
254  */
255 bool readLink(FileName const & file, FileName & link);
256
257 /**
258  * Search a TeX file in all locations the latex compiler would search it,
259  * with the help of kpsewhich.
260  * The current working directory must be set correctly, so that relative
261  * names work.
262  * \param fil The filename to search
263  * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
264  */
265 FileName const findtexfile(std::string const & fil,
266                               std::string const & format);
267
268 /// remove the autosave-file and give a Message if it can't be done
269 void removeAutosaveFile(std::string const & filename);
270
271 /// read the BoundingBox entry from a ps/eps/pdf-file
272 std::string const readBB_from_PSFile(FileName const & file);
273
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.
280  */
281 int compare_timestamps(FileName const & file1, FileName const & file2);
282
283 typedef std::pair<int, std::string> cmd_ret;
284
285 cmd_ret const runCommand(std::string const & cmd);
286
287 } // namespace support
288 } // namespace lyx
289
290 /// The following functions are implemented in minizip/zipunzip.cpp, and are not in
291 /// the lyx::support namespace
292
293 /// zip several files to a zipfile. In-zip filenames are also specified
294 bool zipFiles(std::string const & zipfile, std::vector<std::pair<std::string, std::string> > const & files);
295
296 /// Unzip a zip file to a directory
297 bool unzipToDir(std::string const & zipfile, std::string const & path);
298
299
300 #endif