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