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