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