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