]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
Fix up a few little things after r29065. If we're calculating the autosave
[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 };
55
56 /** Returns the real name of file name in directory path, with optional
57   extension ext.
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).
60   */
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);
65
66 ///
67 bool isLyXFilename(std::string const & filename);
68
69 ///
70 bool isSGMLFilename(std::string const & filename);
71
72 ///
73 bool isValidLaTeXFilename(std::string const & filename);
74
75 /** Returns the path of a library data file.
76     Search the file name.ext in the subdirectory dir of
77       -# user_lyxdir
78       -# build_lyxdir (if not empty)
79       -# system_lyxdir
80     The third parameter `ext' is optional.
81 */
82 FileName const libFileSearch(std::string const & dir,
83                                 std::string const & name,
84                                 std::string const & ext = std::string());
85
86 /** Same as libFileSearch(), but tries first to find an
87   internationalized version of the file by prepending $LANG_ to the
88   name
89   */
90 FileName const
91 i18nLibFileSearch(std::string const & dir,
92                   std::string const & name,
93                   std::string const & ext = std::string());
94
95 /// How to quote a filename
96 enum quote_style {
97         /** Quote for the (OS dependant) shell. This is needed for command
98             line arguments of subprocesses. */
99         quote_shell,
100         /** Quote for python. Use this if you want to store a filename in a
101             python script. Example: \code
102             os << "infile = " << quoteName(filename) << '\\n';
103             \endcode This uses double quotes, so that you can also use this
104             to quote filenames as part of a string if the string is quoted
105             with single quotes. */
106         quote_python
107 };
108
109 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
110  *  and replaces "$$s/" with the path to the LyX support directory containing
111  *  this script. If the script is not found, "$$s/" is removed. Executing the
112  *  command will still fail, but the error message will make some sort of
113  *  sense ;-)
114  */
115 std::string const libScriptSearch(std::string const & command,
116                 quote_style style = quote_shell);
117
118 enum latex_path_extension {
119         PROTECT_EXTENSION,
120         EXCLUDE_EXTENSION
121 };
122
123 enum latex_path_dots {
124         LEAVE_DOTS,
125         ESCAPE_DOTS
126 };
127
128 /** @param path a file path in internal_path format. Ie, directories
129  *  are indicated by '/', not by '\'.
130  *
131  *  Manipulates @c path into a form suitable for inclusion in a LaTeX
132  *  document.
133  *  If @c path contains LaTeX special characters, these are escaped.
134  *  Eg, '~' -> '\\string~'
135  *  If @c path contains spaces, then the returned path is enclosed in
136  *  "-quotes. This last fix will lead to successful compiliation of the
137  *  LaTeX file only if a sufficiently modern LaTeX compiler is used.
138  *  If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
139  *  This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
140  *  (format=pdflatex 2005.4.11) in combination with
141  *  pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
142  *  It does not recognize the file extension if it is inside the quotes.
143  *  If @c dots == ESCAPE_DOTS dots in the filename are replaced by
144  *  "\\lyxdot ". This is needed for the \\includegraphics command if the
145  *  automatic format selection is used.
146  */
147 std::string const latex_path(std::string const & path,
148                 latex_path_extension extension = PROTECT_EXTENSION,
149                 latex_path_dots dots = LEAVE_DOTS);
150
151 /// Substitutes active latex characters with underscores in filename
152 FileName const makeLatexName(FileName const & file);
153
154 /** Put the name in quotes suitable for the current shell or python,
155     depending on \p style. */
156 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
157
158 /// Add a filename to a path. Any path from filename is stripped first.
159 std::string const addName(std::string const & path, std::string const & fname);
160
161 /// Append sub-directory(ies) to path in an intelligent way
162 std::string const addPath(std::string const & path, std::string const & path2);
163
164 /** Change extension of oldname to extension.
165  If oldname does not have an extension, it is appended.
166  If the extension is empty, any extension is removed from the name.
167  */
168 std::string const
169 changeExtension(std::string const & oldname, std::string const & extension);
170
171 /// Remove the extension from \p name
172 std::string const removeExtension(std::string const & name);
173
174 /** Add the extension \p ext to \p name.
175  Use this instead of changeExtension if you know that \p name is without
176  extension, because changeExtension would wrongly interpret \p name if it
177  contains a dot.
178  */
179 std::string const
180 addExtension(std::string const & name, std::string const & extension);
181
182 /// Return the extension of the file (not including the .)
183 std::string const getExtension(std::string const & name);
184
185 /** \return the name that LyX will give to the unzipped file \p zipped_file
186   if the second argument of unzipFile() is empty.
187  */
188 std::string const unzippedFileName(std::string const & zipped_file);
189
190 /** Unzip \p zipped_file.
191   The unzipped file is named \p unzipped_file if \p unzipped_file is not
192   empty, and unzippedFileName(\p zipped_file) otherwise.
193   Will overwrite an already existing unzipped file without warning.
194  */
195 FileName const unzipFile(FileName const & zipped_file,
196                          std::string const & unzipped_file = std::string());
197
198 /// Create absolute path. If impossible, don't do anything
199 std::string const expandPath(std::string const & path);
200
201 /** Convert relative path into absolute path based on a basepath.
202   If relpath is absolute, just use that.
203   If basepath doesn't exist use CWD.
204   */
205 FileName const makeAbsPath(std::string const & RelPath = std::string(),
206                               std::string const & BasePath = std::string());
207
208 /** Creates a nice compact path for displaying. The parameter
209   threshold, if given, specifies the maximal length of the path.
210   */
211 docstring const
212 makeDisplayPath(std::string const & path, unsigned int threshold = 1000);
213
214 /** Makes relative path out of absolute path.
215   If it is deeper than basepath,
216   it's easy. If basepath and abspath share something (they are all deeper
217   than some directory), it'll be rendered using ..'s. If they are completely
218   different, then the absolute path will be used as relative path
219   WARNING: the absolute path and base path must really be absolute paths!!!
220   */
221 docstring const
222 makeRelPath(docstring const & abspath, docstring const & basepath);
223
224 /// Strip filename from path name
225 std::string const onlyPath(std::string const & fname);
226
227 /// Strips path from filename
228 std::string const onlyFilename(std::string const & fname);
229
230 /** Check and Replace Environmentvariables ${NAME} in Path.
231     Replaces all occurences of these, if they are found in the
232     environment.
233     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
234 */
235 std::string const replaceEnvironmentPath(std::string const & path);
236
237 /** Set \c link to the path \c file points to as a symbolic link.
238     \return true if successful.
239  */
240 bool readLink(FileName const & file, FileName & link);
241
242 /**
243  * Search a TeX file in all locations the latex compiler would search it,
244  * with the help of kpsewhich.
245  * The current working directory must be set correctly, so that relative
246  * names work.
247  * \param fil The filename to search
248  * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
249  */
250 FileName const findtexfile(std::string const & fil,
251                               std::string const & format);
252
253 /// read the BoundingBox entry from a ps/eps/pdf-file
254 std::string const readBB_from_PSFile(FileName const & file);
255
256 /** \param file1, file2 the two files to be compared. Must have absolute paths.
257  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
258  *           0 if their timestamps are the same,
259  *          -1 if \c file2 has a more recent timestamp than \c file1.
260  *  If one of the files does not exist, the return value indicates the file
261  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
262  */
263 int compare_timestamps(FileName const & file1, FileName const & file2);
264
265 typedef std::pair<int, std::string> cmd_ret;
266
267 cmd_ret const runCommand(std::string const & cmd);
268
269
270 } // namespace support
271 } // namespace lyx
272
273
274
275 #endif