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