]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
Replace LString.h with support/std_string.h,
[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 "support/std_string.h"
18
19
20 namespace lyx {
21 namespace support {
22
23 /// remove directory and all contents, returns 0 on success
24 int destroyDir(string const & tmpdir);
25
26 ///
27 string const CreateBufferTmpDir(string const & pathfor = string());
28
29 /// Creates directory. Returns true on success
30 bool createDirectory(string const & name, int permissions);
31
32 ///
33 string const CreateLyXTmpDir(string const & deflt);
34
35 /** Find file by searching several directories.
36   Uses a string of paths separated by ";"s to find a file to open.
37     Can't cope with pathnames with a ';' in them. Returns full path to file.
38     If path entry begins with $$LyX/, use system_lyxdir.
39     If path entry begins with $$User/, use user_lyxdir.
40     Example: "$$User/doc;$$LyX/doc".
41 */
42 string const FileOpenSearch(string const & path, string const & name,
43                        string const & ext = string());
44
45 /** Returns the real name of file name in directory path, with optional
46   extension ext.
47   The file is searched in the given path (unless it is an absolute
48   file name), first directly, and then with extension .ext (if given).
49   */
50 string const FileSearch(string const & path, string const & name,
51                   string const & ext = string());
52
53 /// Returns a vector of all files in directory dir having extension ext.
54 std::vector<string> const DirList(string const & dir,
55                                   string const & ext = string());
56
57 /** Is directory read only?
58   returns
59     true: dir writeable
60     false: not writeable
61 */
62 bool IsDirWriteable (string const & path);
63
64 /** Is a file readable ?
65   Returns true if the file `path' is readable.
66  */
67 bool IsFileReadable (string const & path);
68
69 /** Is file read only?
70   returns
71     1: read-write
72     0: read_only
73    -1: error (doesn't exist, no access, anything else)
74   */
75 int IsFileWriteable (string const & path);
76
77 ///
78 bool IsLyXFilename(string const & filename);
79
80 ///
81 bool IsSGMLFilename(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   \begin{enumerate}
86     \item user_lyxdir
87     \item build_lyxdir (if not empty)
88     \item system_lyxdir
89   \end{enumerate}
90     The third parameter `ext' is optional.
91 */
92 string const LibFileSearch(string const & dir, string const & name,
93                      string const & ext = string());
94
95 /** Same as LibFileSearch(), but tries first to find an
96   internationalized version of the file by prepending $LANG_ to the
97   name
98   */
99 string const
100 i18nLibFileSearch(string const & dir, string const & name,
101                   string const & ext = string());
102
103 /** Takes a command such as "sh $$s/convertDefault.sh file.in file.out"
104  *  and replaces "$$s/" with the path to the "most important" of LyX's
105  *  script directories containing this script. If the script is not found,
106  *  "$$s/" is removed. Executing the command will still fail, but the
107  *  error message will make some sort of sense ;-)
108  */
109 string const LibScriptSearch(string const & command);
110
111 ///
112 string const GetEnv(string const & envname);
113
114 /// A helper function.
115 string const GetEnvPath(string const & name);
116
117 /// Substitutes active latex characters with underscores in filename
118 string const MakeLatexName(string const & file);
119
120 /// Put the name in quotes suitable for the current shell
121 string const QuoteName(string const & file);
122
123 /// Add a filename to a path. Any path from filename is stripped first.
124 string const AddName(string const & path, string const & fname);
125
126 /// Append sub-directory(ies) to path in an intelligent way
127 string const AddPath(string const & path, string const & path2);
128
129 /** Change extension of oldname to extension.
130  If oldname does not have an extension, it is appended.
131  If the extension is empty, any extension is removed from the name.
132  */
133 string const
134 ChangeExtension(string const & oldname, string const & extension);
135
136 /// Return the extension of the file (not including the .)
137 string const GetExtension(string const & name);
138
139 /// Return the type of the file as an extension from contents
140 string const getExtFromContents(string const & name);
141
142 /// check for zipped file
143 bool zippedFile(string const & name);
144
145 /// \return the name that LyX will give to the unzipped file.
146 string const unzippedFileName(string const & zipped_file);
147
148 /// unzip a file
149 string const unzipFile(string const & zipped_file);
150
151 /// Returns true is path is absolute
152 bool AbsolutePath(string const & path);
153
154 /// Create absolute path. If impossible, don't do anything
155 string const ExpandPath(string const & path);
156
157 /** Convert relative path into absolute path based on a basepath.
158   If relpath is absolute, just use that.
159   If basepath doesn't exist use CWD.
160   */
161 string const MakeAbsPath(string const & RelPath = string(),
162                          string const & BasePath = string());
163
164 /** Creates a nice compact path for displaying. The parameter
165   threshold, if given, specifies the maximal length of the path.
166   */
167 string const
168 MakeDisplayPath(string const & path, unsigned int threshold = 1000);
169
170 /** Makes relative path out of absolute path.
171   If it is deeper than basepath,
172   it's easy. If basepath and abspath share something (they are all deeper
173   than some directory), it'll be rendered using ..'s. If they are completely
174   different, then the absolute path will be used as relative path
175   WARNING: the absolute path and base path must really be absolute paths!!!
176   */
177 string const
178 MakeRelPath(string const & abspath, string const & basepath);
179
180 /// Strip filename from path name
181 string const OnlyPath(string const & fname);
182
183 /** Normalize a path. Constracts path/../path
184  *  Also converts paths like /foo//bar ==> /foo/bar
185  */
186 string const NormalizePath(string const & path);
187
188 /// Strips path from filename
189 string const OnlyFilename(string const & fname);
190
191 /// Get the contents of a file as a huge string
192 string const GetFileContents(string const & fname);
193
194 /** Check and Replace Environmentvariables ${NAME} in Path.
195     Replaces all occurences of these, if they are found in the
196     environment.
197     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
198 */
199 string const ReplaceEnvironmentPath(string const & path);
200
201 /* Set \c link to the path \c file points to as a symbolic link.
202    If \c resolve is true, then \c link is an absolute path
203    Returns true if successful */
204 bool LyXReadLink(string const & file, string & link, bool resolve = false);
205
206 /// Uses kpsewhich to find tex files
207 string const findtexfile(string const & fil, string const & format);
208
209 /// remove the autosave-file and give a Message if it can't be done
210 void removeAutosaveFile(string const & filename);
211
212 /// read the BoundingBox entry from a ps/eps/pdf-file
213 string const readBB_from_PSFile(string const & file);
214
215 /** \param file1, file2 the two files to be compared. Must have absolute paths.
216  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
217  *           0 if their timestamps are the same,
218  *          -1 if \c file2 has a more recent timestamp than \c file1.
219  *  If one of the files does not exist, the return value indicates the file
220  *  which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
221  */
222 int compare_timestamps(string const & file1, string const & file2);
223
224 typedef std::pair<int, string> cmd_ret;
225
226 cmd_ret const RunCommand(string const & cmd);
227
228 } // namespace support
229 } // namespace lyx
230
231 #endif