]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
Georg Baum\'s no-tempdir patch
[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 0 on success
23 int 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 /** Is file read only?
74   returns
75     1: read-write
76     0: read_only
77    -1: error (doesn't exist, no access, anything else)
78   */
79 int IsFileWriteable (std::string const & path);
80
81 ///
82 bool IsLyXFilename(std::string const & filename);
83
84 ///
85 bool IsSGMLFilename(std::string const & filename);
86
87 /** Returns the path of a library data file.
88   Search the file name.ext in the subdirectory dir of
89   \begin{enumerate}
90     \item user_lyxdir
91     \item build_lyxdir (if not empty)
92     \item system_lyxdir
93   \end{enumerate}
94     The third parameter `ext' is optional.
95 */
96 std::string const LibFileSearch(std::string const & dir, std::string const & name,
97                      std::string const & ext = std::string());
98
99 /** Same as LibFileSearch(), but tries first to find an
100   internationalized version of the file by prepending $LANG_ to the
101   name
102   */
103 std::string const
104 i18nLibFileSearch(std::string const & dir, std::string const & name,
105                   std::string const & ext = std::string());
106
107 /** Takes a command such as "sh $$s/convertDefault.sh file.in file.out"
108  *  and replaces "$$s/" with the path to the "most important" of LyX's
109  *  script directories containing this script. If the script is not found,
110  *  "$$s/" is removed. Executing the command will still fail, but the
111  *  error message will make some sort of sense ;-)
112  */
113 std::string const LibScriptSearch(std::string const & command);
114
115 ///
116 std::string const GetEnv(std::string const & envname);
117
118 /// A helper function.
119 std::string const GetEnvPath(std::string const & name);
120
121 /// Substitutes active latex characters with underscores in filename
122 std::string const MakeLatexName(std::string const & file);
123
124 /// Put the name in quotes suitable for the current shell
125 std::string const QuoteName(std::string const & file);
126
127 /// Add a filename to a path. Any path from filename is stripped first.
128 std::string const AddName(std::string const & path, std::string const & fname);
129
130 /// Append sub-directory(ies) to path in an intelligent way
131 std::string const AddPath(std::string const & path, std::string const & path2);
132
133 /** Change extension of oldname to extension.
134  If oldname does not have an extension, it is appended.
135  If the extension is empty, any extension is removed from the name.
136  */
137 std::string const
138 ChangeExtension(std::string const & oldname, std::string const & extension);
139
140 /// Return the extension of the file (not including the .)
141 std::string const GetExtension(std::string const & name);
142
143 /// Return the type of the file as an extension from contents
144 std::string const getExtFromContents(std::string const & name);
145
146 /// check for zipped file
147 bool zippedFile(std::string const & name);
148
149 /** \return the name that LyX will give to the unzipped file \p zipped_file
150   if the second argument of unzipFile() is empty.
151  */
152 std::string const unzippedFileName(std::string const & zipped_file);
153
154 /** Unzip \p zipped_file.
155   The unzipped file is named \p unzipped_file if \p unzipped_file is not
156   empty, and unzippedFileName(\p zipped_file) otherwise.
157   Will overwrite an already existing unzipped file without warning.
158  */
159 std::string const unzipFile(std::string const & zipped_file,
160                             std::string const & unzipped_file = std::string());
161
162 /// Returns true is path is absolute
163 bool AbsolutePath(std::string const & path);
164
165 /// Create absolute path. If impossible, don't do anything
166 std::string const ExpandPath(std::string const & path);
167
168 /** Convert relative path into absolute path based on a basepath.
169   If relpath is absolute, just use that.
170   If basepath doesn't exist use CWD.
171   */
172 std::string const MakeAbsPath(std::string const & RelPath = std::string(),
173                          std::string const & BasePath = std::string());
174
175 /** Creates a nice compact path for displaying. The parameter
176   threshold, if given, specifies the maximal length of the path.
177   */
178 std::string const
179 MakeDisplayPath(std::string const & path, unsigned int threshold = 1000);
180
181 /** Makes relative path out of absolute path.
182   If it is deeper than basepath,
183   it's easy. If basepath and abspath share something (they are all deeper
184   than some directory), it'll be rendered using ..'s. If they are completely
185   different, then the absolute path will be used as relative path
186   WARNING: the absolute path and base path must really be absolute paths!!!
187   */
188 std::string const
189 MakeRelPath(std::string const & abspath, std::string const & basepath);
190
191 /// Strip filename from path name
192 std::string const OnlyPath(std::string const & fname);
193
194 /** Normalize a path. Constracts path/../path
195  *  Also converts paths like /foo//bar ==> /foo/bar
196  */
197 std::string const NormalizePath(std::string const & path);
198
199 /// Strips path from filename
200 std::string const OnlyFilename(std::string const & fname);
201
202 /// Get the contents of a file as a huge std::string
203 std::string const GetFileContents(std::string const & fname);
204
205 /** Check and Replace Environmentvariables ${NAME} in Path.
206     Replaces all occurences of these, if they are found in the
207     environment.
208     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
209 */
210 std::string const ReplaceEnvironmentPath(std::string const & path);
211
212 /* Set \c link to the path \c file points to as a symbolic link.
213    If \c resolve is true, then \c link is an absolute path
214    Returns true if successful */
215 bool LyXReadLink(std::string const & file, std::string & link, bool resolve = false);
216
217 /// Uses kpsewhich to find tex files
218 std::string const findtexfile(std::string const & fil, std::string const & format);
219
220 /// remove the autosave-file and give a Message if it can't be done
221 void removeAutosaveFile(std::string const & filename);
222
223 /// read the BoundingBox entry from a ps/eps/pdf-file
224 std::string const readBB_from_PSFile(std::string const & file);
225
226 /** \param file1, file2 the two files to be compared. Must have absolute paths.
227  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
228  *           0 if their timestamps are the same,
229  *          -1 if \c file2 has a more recent timestamp than \c file1.
230  *  If one of the files does not exist, the return value indicates the file
231  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
232  */
233 int compare_timestamps(std::string const & file1, std::string const & file2);
234
235 typedef std::pair<int, std::string> cmd_ret;
236
237 cmd_ret const RunCommand(std::string const & cmd);
238
239 } // namespace support
240 } // namespace lyx
241
242 #endif