]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
Remove the iconScaleFactor() method from src/support.
[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
17 #include <utility>
18 #include <string>
19
20 namespace lyx {
21 namespace support {
22
23 class FileName;
24
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.
30  */
31 FileName const createLyXTmpDir(FileName const & deflt);
32
33 #if 0
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".
41 */
42 FileName const fileOpenSearch(std::string const & path,
43                                  std::string const & name,
44                                  std::string const & ext = std::string());
45 #endif
46
47 /// How to search files
48 enum search_mode {
49         /// The file must exist (return an empty file name otherwise)
50         must_exist,
51         /// Only do file name expansion, return the complete name even if
52         /// the file does not exist
53         may_not_exist,
54         /// The (image) file may be present with hi-dpi resolution -
55         /// the lookup checks for a file named "image" + "@2x" + ".ext" first.
56         /// If found it will return e.g. "image@2x.png" instead of "image.png".
57         /// Otherwise it will work as must_exist.
58         check_hidpi
59 };
60
61 /** Returns the real name of file name in directory path, with optional
62   extension ext.
63   The file is searched in the given path (unless it is an absolute
64   file name), first directly, and then with extension .ext (if given).
65   */
66 FileName const fileSearch(std::string const & path,
67                              std::string const & name,
68                              std::string const & ext = std::string(),
69                              search_mode mode = must_exist);
70
71 ///
72 bool isLyXFileName(std::string const & filename);
73
74 ///
75 bool isSGMLFileName(std::string const & filename);
76
77 /// check for characters in filenames not allowed by LaTeX
78 bool isValidLaTeXFileName(std::string const & filename);
79
80 /** check for characters in filenames that might lead to
81   problems when manually compiling the LaTeX export of LyX
82   and opening the result with some older DVI-viewers
83 */
84 bool isValidDVIFileName(std::string const & filename);
85
86 /// check whether the file has binary contents
87 bool isBinaryFile(FileName const & filename);
88
89 /** Returns the path of a library data file.
90     Search the file name.ext in the subdirectory dir of
91       -# user_lyxdir
92       -# build_lyxdir (if not empty)
93       -# system_lyxdir
94     The third parameter `ext' is optional.
95 */
96 FileName const libFileSearch(std::string const & dir,
97                                 std::string const & name,
98                                 std::string const & ext = std::string(),
99                                 search_mode mode = must_exist);
100
101 /** Same as libFileSearch(), but tries first to find an
102   internationalized version of the file by prepending $LANG_ to the
103   name
104   */
105 FileName const
106 i18nLibFileSearch(std::string const & dir,
107                   std::string const & name,
108                   std::string const & ext = std::string());
109
110 /** Same as libFileSearch(), but tries first to find a preferred
111   version of the file by appending the icon set name to the dir
112   */
113 FileName const
114 imageLibFileSearch(std::string & dir, std::string const & name,
115                    std::string const & ext = std::string(),
116                    search_mode mode = must_exist);
117
118 /// How to quote a filename
119 enum quote_style {
120         /** Quote for the (OS dependant) shell. This is needed for command
121             line arguments of subprocesses. */
122         quote_shell,
123         /** Quote a file name for the (OS dependant) shell. This is needed
124             for file names as command line arguments of subprocesses. */
125         quote_shell_filename,
126         /** Quote for python. Use this if you want to store a filename in a
127             python script. Example: \code
128             os << "infile = " << quoteName(filename) << '\\n';
129             \endcode This uses double quotes, so that you can also use this
130             to quote filenames as part of a string if the string is quoted
131             with single quotes. */
132         quote_python
133 };
134
135 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
136  *  and replaces "$$s/" with the path to the LyX support directory containing
137  *  this script. If the script is not found, "$$s/" is removed. Executing the
138  *  command will still fail, but the error message will make some sort of
139  *  sense ;-)
140  */
141 std::string const commandPrep(std::string const & command);
142
143 enum latex_path_extension {
144         PROTECT_EXTENSION,
145         EXCLUDE_EXTENSION
146 };
147
148 enum latex_path_dots {
149         LEAVE_DOTS,
150         ESCAPE_DOTS
151 };
152
153 /** @param path a file path in internal_path format. Ie, directories
154  *  are indicated by '/', not by '\'.
155  *
156  *  Manipulates @c path into a form suitable for inclusion in a LaTeX
157  *  document.
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.
171  */
172 std::string const latex_path(std::string const & path,
173                 latex_path_extension extension = PROTECT_EXTENSION,
174                 latex_path_dots dots = LEAVE_DOTS);
175
176 /// Substitutes active latex characters with underscores in filename
177 FileName const makeLatexName(FileName const & file);
178
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);
182
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);
185
186 /// Append sub-directory(ies) to path in an intelligent way
187 std::string const addPath(std::string const & path, std::string const & path2);
188
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.
192  */
193 std::string const
194 changeExtension(std::string const & oldname, std::string const & extension);
195
196 /// Remove the extension from \p name
197 std::string const removeExtension(std::string const & name);
198
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
202  contains a dot.
203  */
204 std::string const
205 addExtension(std::string const & name, std::string const & extension);
206
207 /// Return the extension of the file (not including the .)
208 std::string const getExtension(std::string const & name);
209
210 /** \return the name that LyX will give to the unzipped file \p zipped_file
211   if the second argument of unzipFile() is empty.
212  */
213 std::string const unzippedFileName(std::string const & zipped_file);
214
215 /** Unzip \p zipped_file.
216   The unzipped file is named \p unzipped_file if \p unzipped_file is not
217   empty, and unzippedFileName(\p zipped_file) otherwise.
218   Will overwrite an already existing unzipped file without warning.
219  */
220 FileName const unzipFile(FileName const & zipped_file,
221                          std::string const & unzipped_file = std::string());
222
223 /// Create absolute path. If impossible, don't do anything
224 std::string const expandPath(std::string const & path);
225
226 /** Convert relative path into absolute path based on a basepath.
227   If relpath is absolute, just use that.
228   If basepath doesn't exist use CWD.
229   */
230 FileName const makeAbsPath(std::string const & RelPath = std::string(),
231                               std::string const & BasePath = std::string());
232
233 /** Creates a nice compact path for displaying. The parameter
234   threshold, if given, specifies the maximal length of the path.
235   */
236 docstring const
237 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
238
239 /** Makes relative path out of absolute path.
240   If it is deeper than basepath,
241   it's easy. If basepath and abspath share something (they are all deeper
242   than some directory), it'll be rendered using ..'s. If they are completely
243   different, then the absolute path will be used as relative path
244   WARNING: the absolute path and base path must really be absolute paths!!!
245   */
246 docstring const
247 makeRelPath(docstring const & abspath, docstring const & basepath);
248
249 /// Strip filename from path name
250 std::string const onlyPath(std::string const & fname);
251
252 /// Strips path from filename
253 std::string const onlyFileName(std::string const & fname);
254
255 /** Check and Replace Environmentvariables ${NAME} in Path.
256     Replaces all occurences of these, if they are found in the
257     environment.
258     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
259 */
260 std::string const replaceEnvironmentPath(std::string const & path);
261
262 /**
263    Return a string to be used as a prefix to a command for setting the
264    environment of the TeX engine with respect to the path \p path.
265  */
266 std::string latexEnvCmdPrefix(std::string const & path);
267
268 /** Replace all references to a current directory (a lonely '.' or
269     the prefix "./") in \c pathlist with \c path. Also prefixes
270     all non-absolute paths with \c path.
271 */
272 std::string const replaceCurdirPath(std::string const & path, std::string const & pathlist);
273
274 /** Set \c link to the path \c file points to as a symbolic link.
275     \return true if successful.
276  */
277 bool readLink(FileName const & file, FileName & link);
278
279 /**
280  * Search a TeX file in all locations the latex compiler would search it,
281  * with the help of kpsewhich.
282  * The current working directory must be set correctly, so that relative
283  * names work.
284  * \param fil The filename to search
285  * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
286  */
287 FileName const findtexfile(std::string const & fil,
288                               std::string const & format);
289
290 /** \param file1, file2 the two files to be compared. Must have absolute paths.
291  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
292  *           0 if their timestamps are the same,
293  *          -1 if \c file2 has a more recent timestamp than \c file1.
294  *  If one of the files does not exist, the return value indicates the file
295  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
296  */
297 int compare_timestamps(FileName const & file1, FileName const & file2);
298
299 /// \param lfuns: true if we're converting lfuns, false if prefs
300 bool prefs2prefs(FileName const & filename, FileName const & tempfile,
301                  bool lfuns);
302
303 /// Does file \p file need to be updated by configure.py?
304 bool configFileNeedsUpdate(std::string const & file);
305
306 typedef std::pair<int, std::string> cmd_ret;
307
308 cmd_ret const runCommand(std::string const & cmd);
309
310 int fileLock(const char * lock_file);
311 void fileUnlock(int fd, const char * lock_file);
312
313 } // namespace support
314 } // namespace lyx
315
316
317
318 #endif