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