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