]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
compile fix for the latex_path patch from yesterday
[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       -# user_lyxdir
82       -# build_lyxdir (if not empty)
83       -# system_lyxdir
84     The third parameter `ext' is optional.
85 */
86 std::string const LibFileSearch(std::string const & dir, 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 std::string const
94 i18nLibFileSearch(std::string const & dir, std::string const & name,
95                   std::string const & ext = std::string());
96
97 /** Takes a command such as "sh $$s/scripts/convertDefault.sh file.in file.out"
98  *  and replaces "$$s/" with the path to the LyX support directory containing
99  *  this script. If the script is not found, "$$s/" is removed. Executing the
100  *  command will still fail, but the error message will make some sort of
101  *  sense ;-)
102  */
103 std::string const LibScriptSearch(std::string const & command);
104
105 /** @param path a file path in internal_path format. Ie, directories
106  *  are indicated by '/', not by '\'.
107  *
108  *  Manipulates @c path into a form suitable for inclusion in a LaTeX
109  *  document.
110  *  If @c path contains LaTeX special characters, these are escaped.
111  *  Eg, '~' -> '\\string~'
112  *  If @c path contains spaces, then the returned path is enclosed in
113  *  "-quotes. This last fix will lead to successful compiliation of the
114  *  LaTeX file only if a sufficiently modern LaTeX compiler is used.
115  *  If @c exclude_extension is true the extension is left outside the quotes.
116  *  This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
117  *  (format=pdflatex 2005.4.11) in combination with
118  *  pdftex.def 2002/06/19 v0.03k graphics/color for pdftex:
119  *  It does not recognize the file extension if it is inside the quotes.
120  */
121 std::string const latex_path(std::string const & path,
122                              bool exclude_extension = false);
123
124 /// Substitutes active latex characters with underscores in filename
125 std::string const MakeLatexName(std::string const & file);
126
127 /// Put the name in quotes suitable for the current shell
128 std::string const QuoteName(std::string const & file);
129
130 /// Add a filename to a path. Any path from filename is stripped first.
131 std::string const AddName(std::string const & path, std::string const & fname);
132
133 /// Append sub-directory(ies) to path in an intelligent way
134 std::string const AddPath(std::string const & path, std::string const & path2);
135
136 /** Change extension of oldname to extension.
137  If oldname does not have an extension, it is appended.
138  If the extension is empty, any extension is removed from the name.
139  */
140 std::string const
141 ChangeExtension(std::string const & oldname, std::string const & extension);
142
143 /// Return the extension of the file (not including the .)
144 std::string const GetExtension(std::string const & name);
145
146 /** Guess the file format name (as in Format::name()) from contents.
147  Normally you don't want to use this directly, but rather
148  Formats::getFormatFromFile().
149  */
150 std::string const getFormatFromContents(std::string const & name);
151
152 /// check for zipped file
153 bool zippedFile(std::string const & name);
154
155 /** \return the name that LyX will give to the unzipped file \p zipped_file
156   if the second argument of unzipFile() is empty.
157  */
158 std::string const unzippedFileName(std::string const & zipped_file);
159
160 /** Unzip \p zipped_file.
161   The unzipped file is named \p unzipped_file if \p unzipped_file is not
162   empty, and unzippedFileName(\p zipped_file) otherwise.
163   Will overwrite an already existing unzipped file without warning.
164  */
165 std::string const unzipFile(std::string const & zipped_file,
166                             std::string const & unzipped_file = std::string());
167
168 /// Returns true is path is absolute
169 bool AbsolutePath(std::string const & path);
170
171 /// Create absolute path. If impossible, don't do anything
172 std::string const ExpandPath(std::string const & path);
173
174 /** Convert relative path into absolute path based on a basepath.
175   If relpath is absolute, just use that.
176   If basepath doesn't exist use CWD.
177   */
178 std::string const MakeAbsPath(std::string const & RelPath = std::string(),
179                          std::string const & BasePath = std::string());
180
181 /** Creates a nice compact path for displaying. The parameter
182   threshold, if given, specifies the maximal length of the path.
183   */
184 std::string const
185 MakeDisplayPath(std::string const & path, unsigned int threshold = 1000);
186
187 /** Makes relative path out of absolute path.
188   If it is deeper than basepath,
189   it's easy. If basepath and abspath share something (they are all deeper
190   than some directory), it'll be rendered using ..'s. If they are completely
191   different, then the absolute path will be used as relative path
192   WARNING: the absolute path and base path must really be absolute paths!!!
193   */
194 std::string const
195 MakeRelPath(std::string const & abspath, std::string const & basepath);
196
197 /// Strip filename from path name
198 std::string const OnlyPath(std::string const & fname);
199
200 /** Normalize a path. Constracts path/../path
201  *  Also converts paths like /foo//bar ==> /foo/bar
202  */
203 std::string const NormalizePath(std::string const & path);
204
205 /// Strips path from filename
206 std::string const OnlyFilename(std::string const & fname);
207
208 /// Get the contents of a file as a huge std::string
209 std::string const GetFileContents(std::string const & fname);
210
211 /** Check and Replace Environmentvariables ${NAME} in Path.
212     Replaces all occurences of these, if they are found in the
213     environment.
214     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
215 */
216 std::string const ReplaceEnvironmentPath(std::string const & path);
217
218 /* Set \c link to the path \c file points to as a symbolic link.
219    If \c resolve is true, then \c link is an absolute path
220    Returns true if successful */
221 bool LyXReadLink(std::string const & file, std::string & link, bool resolve = false);
222
223 /// Uses kpsewhich to find tex files
224 std::string const findtexfile(std::string const & fil, std::string const & format);
225
226 /// remove the autosave-file and give a Message if it can't be done
227 void removeAutosaveFile(std::string const & filename);
228
229 /// read the BoundingBox entry from a ps/eps/pdf-file
230 std::string const readBB_from_PSFile(std::string const & file);
231
232 /** \param file1, file2 the two files to be compared. Must have absolute paths.
233  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
234  *           0 if their timestamps are the same,
235  *          -1 if \c file2 has a more recent timestamp than \c file1.
236  *  If one of the files does not exist, the return value indicates the file
237  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
238  */
239 int compare_timestamps(std::string const & file1, std::string const & file2);
240
241 typedef std::pair<int, std::string> cmd_ret;
242
243 cmd_ret const RunCommand(std::string const & cmd);
244
245 } // namespace support
246 } // namespace lyx
247
248 #endif