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