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