]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
move funtion with std::vector to filetools
[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 /// Creates the per buffer temporary directory
26 std::string const createBufferTmpDir();
27
28 /** Creates the global LyX temp dir.
29   \p deflt can be an existing directory name. In this case a new directory
30   inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
31   created and used as the temporary directory.
32   \return the tmp dir name or string() if something went wrong.
33  */
34 FileName const createLyXTmpDir(FileName const & deflt);
35
36 #if 0
37 // FIXME unused. Should this be deleted or resurrected?
38 /** Find file by searching several directories.
39   Uses a string of paths separated by ";"s to find a file to open.
40     Can't cope with pathnames with a ';' in them. Returns full path to file.
41     If path entry begins with $$LyX/, use system_lyxdir.
42     If path entry begins with $$User/, use user_lyxdir.
43     Example: "$$User/doc;$$LyX/doc".
44 */
45 FileName const fileOpenSearch(std::string const & path,
46                                  std::string const & name,
47                                  std::string const & ext = std::string());
48 #endif
49
50 /// How to search files
51 enum search_mode {
52         // The file must exist (return an empty file name otherwise)
53         standard_mode,
54         /// Only do file name expansion, return the complete name even if
55         /// the file does not exist
56         allow_unreadable
57 };
58
59 /** Returns the real name of file name in directory path, with optional
60   extension ext.
61   The file is searched in the given path (unless it is an absolute
62   file name), first directly, and then with extension .ext (if given).
63   */
64 FileName const fileSearch(std::string const & path,
65                              std::string const & name,
66                              std::string const & ext = std::string(),
67                              search_mode mode = standard_mode);
68
69 ///
70 bool isLyXFilename(std::string const & filename);
71
72 ///
73 bool isSGMLFilename(std::string const & filename);
74
75 ///
76 bool isValidLaTeXFilename(std::string const & filename);
77
78 /** Returns the path of a library data file.
79     Search the file name.ext in the subdirectory dir of
80       -# user_lyxdir
81       -# build_lyxdir (if not empty)
82       -# system_lyxdir
83     The third parameter `ext' is optional.
84 */
85 FileName const libFileSearch(std::string const & dir,
86                                 std::string const & name,
87                                 std::string const & ext = std::string());
88
89 /** Same as libFileSearch(), but tries first to find an
90   internationalized version of the file by prepending $LANG_ to the
91   name
92   */
93 FileName const
94 i18nLibFileSearch(std::string const & dir,
95                   std::string const & name,
96                   std::string const & ext = std::string());
97
98 /// How to quote a filename
99 enum quote_style {
100         /** Quote for the (OS dependant) shell. This is needed for command
101             line arguments of subprocesses. */
102         quote_shell,
103         /** Quote for python. Use this if you want to store a filename in a
104             python script. Example: \code
105             os << "infile = " << quoteName(filename) << '\\n';
106             \endcode This uses double quotes, so that you can also use this
107             to quote filenames as part of a string if the string is quoted
108             with single quotes. */
109         quote_python
110 };
111
112 /** Takes a command such as "python $$s/scripts/convertDefault.py file.in file.out"
113  *  and replaces "$$s/" with the path to the LyX support directory containing
114  *  this script. If the script is not found, "$$s/" is removed. Executing the
115  *  command will still fail, but the error message will make some sort of
116  *  sense ;-)
117  */
118 std::string const libScriptSearch(std::string const & command,
119                 quote_style style = quote_shell);
120
121 enum latex_path_extension {
122         PROTECT_EXTENSION,
123         EXCLUDE_EXTENSION
124 };
125
126 enum latex_path_dots {
127         LEAVE_DOTS,
128         ESCAPE_DOTS
129 };
130
131 /** @param path a file path in internal_path format. Ie, directories
132  *  are indicated by '/', not by '\'.
133  *
134  *  Manipulates @c path into a form suitable for inclusion in a LaTeX
135  *  document.
136  *  If @c path contains LaTeX special characters, these are escaped.
137  *  Eg, '~' -> '\\string~'
138  *  If @c path contains spaces, then the returned path is enclosed in
139  *  "-quotes. This last fix will lead to successful compiliation of the
140  *  LaTeX file only if a sufficiently modern LaTeX compiler is used.
141  *  If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
142  *  This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
143  *  (format=pdflatex 2005.4.11) in combination with
144  *  pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
145  *  It does not recognize the file extension if it is inside the quotes.
146  *  If @c dots == ESCAPE_DOTS dots in the filename are replaced by
147  *  "\\lyxdot ". This is needed for the \\includegraphics command if the
148  *  automatic format selection is used.
149  */
150 std::string const latex_path(std::string const & path,
151                 latex_path_extension extension = PROTECT_EXTENSION,
152                 latex_path_dots dots = LEAVE_DOTS);
153
154 /// Substitutes active latex characters with underscores in filename
155 FileName const makeLatexName(FileName const & file);
156
157 /** Put the name in quotes suitable for the current shell or python,
158     depending on \p style. */
159 std::string const quoteName(std::string const & file, quote_style style = quote_shell);
160
161 /// Add a filename to a path. Any path from filename is stripped first.
162 std::string const addName(std::string const & path, std::string const & fname);
163
164 /// Append sub-directory(ies) to path in an intelligent way
165 std::string const addPath(std::string const & path, std::string const & path2);
166
167 /** Change extension of oldname to extension.
168  If oldname does not have an extension, it is appended.
169  If the extension is empty, any extension is removed from the name.
170  */
171 std::string const
172 changeExtension(std::string const & oldname, std::string const & extension);
173
174 /// Remove the extension from \p name
175 std::string const removeExtension(std::string const & name);
176
177 /** Add the extension \p ext to \p name.
178  Use this instead of changeExtension if you know that \p name is without
179  extension, because changeExtension would wrongly interpret \p name if it
180  contains a dot.
181  */
182 std::string const
183 addExtension(std::string const & name, std::string const & extension);
184
185 /// Return the extension of the file (not including the .)
186 std::string const getExtension(std::string const & name);
187
188 /** \return the name that LyX will give to the unzipped file \p zipped_file
189   if the second argument of unzipFile() is empty.
190  */
191 std::string const unzippedFileName(std::string const & zipped_file);
192
193 /** Unzip \p zipped_file.
194   The unzipped file is named \p unzipped_file if \p unzipped_file is not
195   empty, and unzippedFileName(\p zipped_file) otherwise.
196   Will overwrite an already existing unzipped file without warning.
197  */
198 FileName const unzipFile(FileName const & zipped_file,
199                          std::string const & unzipped_file = std::string());
200
201 /// Returns true is path is absolute
202 bool absolutePath(std::string const & path);
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 /// remove the autosave-file and give a Message if it can't be done
260 void removeAutosaveFile(std::string const & filename);
261
262 /// read the BoundingBox entry from a ps/eps/pdf-file
263 std::string const readBB_from_PSFile(FileName const & file);
264
265 /** \param file1, file2 the two files to be compared. Must have absolute paths.
266  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
267  *           0 if their timestamps are the same,
268  *          -1 if \c file2 has a more recent timestamp than \c file1.
269  *  If one of the files does not exist, the return value indicates the file
270  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
271  */
272 int compare_timestamps(FileName const & file1, FileName const & file2);
273
274 typedef std::pair<int, std::string> cmd_ret;
275
276 cmd_ret const runCommand(std::string const & cmd);
277
278 /// \return list other files in the directory having optional extension 'ext'.
279 std::vector<FileName> dirList(FileName const & filename, std::string const & ext);
280
281
282 } // namespace support
283 } // namespace lyx
284
285 /// The following functions are implemented in minizip/zipunzip.cpp, and are not in
286 /// the lyx::support namespace
287
288 /// zip several files to a zipfile. In-zip filenames are also specified
289 bool zipFiles(std::string const & zipfile, std::vector<std::pair<std::string, std::string> > const & files);
290
291 /// Unzip a zip file to a directory
292 bool unzipToDir(std::string const & zipfile, std::string const & path);
293
294
295 #endif