]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
ditch FileInfo -> use boost.filesystem
[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, std::string const & name,
47                        std::string const & ext = std::string());
48
49 /** Returns the real name of file name in directory path, with optional
50   extension ext.
51   The file is searched in the given path (unless it is an absolute
52   file name), first directly, and then with extension .ext (if given).
53   */
54 std::string const FileSearch(std::string const & path, std::string const & name,
55                   std::string const & ext = std::string());
56
57 /// Returns a vector of all files in directory dir having extension ext.
58 std::vector<std::string> const DirList(std::string const & dir,
59                                   std::string const & ext = std::string());
60
61 /** Is directory read only?
62   returns
63     true: dir writeable
64     false: not writeable
65 */
66 bool IsDirWriteable (std::string const & path);
67
68 /** Is a file readable ?
69   Returns true if the file `path' is readable.
70  */
71 bool IsFileReadable (std::string const & path);
72
73 ///
74 bool IsLyXFilename(std::string const & filename);
75
76 ///
77 bool IsSGMLFilename(std::string const & filename);
78
79 /** Returns the path of a library data file.
80   Search the file name.ext in the subdirectory dir of
81   \begin{enumerate}
82     \item user_lyxdir
83     \item build_lyxdir (if not empty)
84     \item system_lyxdir
85   \end{enumerate}
86     The third parameter `ext' is optional.
87 */
88 std::string const LibFileSearch(std::string const & dir, std::string const & name,
89                      std::string const & ext = std::string());
90
91 /** Same as LibFileSearch(), but tries first to find an
92   internationalized version of the file by prepending $LANG_ to the
93   name
94   */
95 std::string const
96 i18nLibFileSearch(std::string const & dir, std::string const & name,
97                   std::string const & ext = std::string());
98
99 /** Takes a command such as "sh $$s/scripts/convertDefault.sh file.in file.out"
100  *  and replaces "$$s/" with the path to the LyX support directory containing
101  *  this script. If the script is not found, "$$s/" is removed. Executing the
102  *  command will still fail, but the error message will make some sort of
103  *  sense ;-)
104  */
105 std::string const LibScriptSearch(std::string const & command);
106
107 ///
108 std::string const GetEnv(std::string const & envname);
109
110 /** Return the contents of the environment variable \c name,
111  *  split using the OS-dependent token separating elements.
112  *  Each element is then passed through os::internal_path to
113  *  guarantee that it is in the form of a unix-stype path.
114  *  If the environment variable is not set, then returns an empty vector.
115  */
116 std::vector<std::string> const getEnvPath(std::string const & name);
117
118 /** Set the contents of the environment variable \c name
119  *  using the paths stored in the \c env vector.
120  *  Each element is passed through os::external_path.
121  */
122 void setEnvPath(std::string const & name, std::vector<std::string> const & env);
123
124 /** Prepend a list of paths to that returned by the environment variable.
125  *  Identical paths occurring later in the list are removed.
126  *  @param name the name of the environment variable.
127  *  @prefix the list of paths in OS-native syntax.
128  *  Eg "/foo/bar:/usr/bin:/usr/local/bin" on *nix,
129  *     "C:\foo\bar;C:\windows" on Windows.
130  */
131 void prependEnvPath(std::string const & name, std::string const & prefix);
132
133 /// Set an environment variable using a string of the form "name=FOO".
134 bool putEnv(std::string const & envstr);
135
136 /// Substitutes active latex characters with underscores in filename
137 std::string const MakeLatexName(std::string const & file);
138
139 /// Put the name in quotes suitable for the current shell
140 std::string const QuoteName(std::string const & file);
141
142 /// Add a filename to a path. Any path from filename is stripped first.
143 std::string const AddName(std::string const & path, std::string const & fname);
144
145 /// Append sub-directory(ies) to path in an intelligent way
146 std::string const AddPath(std::string const & path, std::string const & path2);
147
148 /** Change extension of oldname to extension.
149  If oldname does not have an extension, it is appended.
150  If the extension is empty, any extension is removed from the name.
151  */
152 std::string const
153 ChangeExtension(std::string const & oldname, std::string const & extension);
154
155 /// Return the extension of the file (not including the .)
156 std::string const GetExtension(std::string const & name);
157
158 /** Guess the file format name (as in Format::name()) from contents.
159  Normally you don't want to use this directly, but rather
160  Formats::getFormatFromFile().
161  */
162 std::string const getFormatFromContents(std::string const & name);
163
164 /// check for zipped file
165 bool zippedFile(std::string const & name);
166
167 /** \return the name that LyX will give to the unzipped file \p zipped_file
168   if the second argument of unzipFile() is empty.
169  */
170 std::string const unzippedFileName(std::string const & zipped_file);
171
172 /** Unzip \p zipped_file.
173   The unzipped file is named \p unzipped_file if \p unzipped_file is not
174   empty, and unzippedFileName(\p zipped_file) otherwise.
175   Will overwrite an already existing unzipped file without warning.
176  */
177 std::string const unzipFile(std::string const & zipped_file,
178                             std::string const & unzipped_file = std::string());
179
180 /// Returns true is path is absolute
181 bool AbsolutePath(std::string const & path);
182
183 /// Create absolute path. If impossible, don't do anything
184 std::string const ExpandPath(std::string const & path);
185
186 /** Convert relative path into absolute path based on a basepath.
187   If relpath is absolute, just use that.
188   If basepath doesn't exist use CWD.
189   */
190 std::string const MakeAbsPath(std::string const & RelPath = std::string(),
191                          std::string const & BasePath = std::string());
192
193 /** Creates a nice compact path for displaying. The parameter
194   threshold, if given, specifies the maximal length of the path.
195   */
196 std::string const
197 MakeDisplayPath(std::string const & path, unsigned int threshold = 1000);
198
199 /** Makes relative path out of absolute path.
200   If it is deeper than basepath,
201   it's easy. If basepath and abspath share something (they are all deeper
202   than some directory), it'll be rendered using ..'s. If they are completely
203   different, then the absolute path will be used as relative path
204   WARNING: the absolute path and base path must really be absolute paths!!!
205   */
206 std::string const
207 MakeRelPath(std::string const & abspath, std::string const & basepath);
208
209 /// Strip filename from path name
210 std::string const OnlyPath(std::string const & fname);
211
212 /** Normalize a path. Constracts path/../path
213  *  Also converts paths like /foo//bar ==> /foo/bar
214  */
215 std::string const NormalizePath(std::string const & path);
216
217 /// Strips path from filename
218 std::string const OnlyFilename(std::string const & fname);
219
220 /// Get the contents of a file as a huge std::string
221 std::string const GetFileContents(std::string const & fname);
222
223 /** Check and Replace Environmentvariables ${NAME} in Path.
224     Replaces all occurences of these, if they are found in the
225     environment.
226     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
227 */
228 std::string const ReplaceEnvironmentPath(std::string const & path);
229
230 /* Set \c link to the path \c file points to as a symbolic link.
231    If \c resolve is true, then \c link is an absolute path
232    Returns true if successful */
233 bool LyXReadLink(std::string const & file, std::string & link, bool resolve = false);
234
235 /// Uses kpsewhich to find tex files
236 std::string const findtexfile(std::string const & fil, std::string const & format);
237
238 /// remove the autosave-file and give a Message if it can't be done
239 void removeAutosaveFile(std::string const & filename);
240
241 /// read the BoundingBox entry from a ps/eps/pdf-file
242 std::string const readBB_from_PSFile(std::string const & file);
243
244 /** \param file1, file2 the two files to be compared. Must have absolute paths.
245  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
246  *           0 if their timestamps are the same,
247  *          -1 if \c file2 has a more recent timestamp than \c file1.
248  *  If one of the files does not exist, the return value indicates the file
249  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
250  */
251 int compare_timestamps(std::string const & file1, std::string const & file2);
252
253 typedef std::pair<int, std::string> cmd_ret;
254
255 cmd_ret const RunCommand(std::string const & cmd);
256
257 } // namespace support
258 } // namespace lyx
259
260 #endif