4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
12 #ifndef LYX_FILETOOL_H
13 #define LYX_FILETOOL_H
22 /// remove directory and all contents, returns 0 on success
23 int destroyDir(std::string const & tmpdir);
25 /// Creates the per buffer temporary directory
26 std::string const createBufferTmpDir();
28 /// Creates directory. Returns true on success
29 bool createDirectory(std::string const & name, int permissions);
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.
37 std::string const createLyXTmpDir(std::string const & deflt);
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".
46 std::string const FileOpenSearch(std::string const & path, std::string const & name,
47 std::string const & ext = std::string());
49 /** Returns the real name of file name in directory path, with optional
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).
54 std::string const FileSearch(std::string const & path, std::string const & name,
55 std::string const & ext = std::string());
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());
61 /** Is directory read only?
66 bool IsDirWriteable (std::string const & path);
68 /** Is a file readable ?
69 Returns true if the file `path' is readable.
71 bool IsFileReadable (std::string const & path);
73 /** Is file read only?
77 -1: error (doesn't exist, no access, anything else)
79 int IsFileWriteable (std::string const & path);
82 bool IsLyXFilename(std::string const & filename);
85 bool IsSGMLFilename(std::string const & filename);
87 /** Returns the path of a library data file.
88 Search the file name.ext in the subdirectory dir of
91 \item build_lyxdir (if not empty)
94 The third parameter `ext' is optional.
96 std::string const LibFileSearch(std::string const & dir, std::string const & name,
97 std::string const & ext = std::string());
99 /** Same as LibFileSearch(), but tries first to find an
100 internationalized version of the file by prepending $LANG_ to the
104 i18nLibFileSearch(std::string const & dir, std::string const & name,
105 std::string const & ext = std::string());
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 ;-)
113 std::string const LibScriptSearch(std::string const & command);
116 std::string const GetEnv(std::string const & envname);
118 /** Return the contents of the environment variable \c name,
119 * split using the OS-dependent token separating elements.
120 * Each element is then passed through os::internal_path to
121 * guarantee that it is in the form of a unix-stype path.
122 * If the environment variable is not set, then returns an empty vector.
124 std::vector<std::string> const getEnvPath(std::string const & name);
126 /** Set the contents of the environment variable \c name
127 * using the paths stored in the \c env vector.
128 * Each element is passed through os::external_path.
130 void setEnvPath(std::string const & name, std::vector<std::string> const & env);
132 /** Prepend a list of paths to that returned by the environment variable.
133 * Identical paths occurring later in the list are removed.
134 * @param name the name of the environment variable.
135 * @prefix the list of paths in OS-native syntax.
136 * Eg "/foo/bar:/usr/bin:/usr/local/bin" on *nix,
137 * "C:\foo\bar;C:\windows" on Windows.
139 void prependEnvPath(std::string const & name, std::string const & prefix);
141 /// Set an environment variable using a string of the form "name=FOO".
142 bool putEnv(std::string const & envstr);
144 /// Substitutes active latex characters with underscores in filename
145 std::string const MakeLatexName(std::string const & file);
147 /// Put the name in quotes suitable for the current shell
148 std::string const QuoteName(std::string const & file);
150 /// Add a filename to a path. Any path from filename is stripped first.
151 std::string const AddName(std::string const & path, std::string const & fname);
153 /// Append sub-directory(ies) to path in an intelligent way
154 std::string const AddPath(std::string const & path, std::string const & path2);
156 /** Change extension of oldname to extension.
157 If oldname does not have an extension, it is appended.
158 If the extension is empty, any extension is removed from the name.
161 ChangeExtension(std::string const & oldname, std::string const & extension);
163 /// Return the extension of the file (not including the .)
164 std::string const GetExtension(std::string const & name);
166 /** Guess the file format name (as in Format::name()) from contents.
167 Normally you don't want to use this directly, but rather
168 Formats::getFormatFromFile().
170 std::string const getFormatFromContents(std::string const & name);
172 /// check for zipped file
173 bool zippedFile(std::string const & name);
175 /** \return the name that LyX will give to the unzipped file \p zipped_file
176 if the second argument of unzipFile() is empty.
178 std::string const unzippedFileName(std::string const & zipped_file);
180 /** Unzip \p zipped_file.
181 The unzipped file is named \p unzipped_file if \p unzipped_file is not
182 empty, and unzippedFileName(\p zipped_file) otherwise.
183 Will overwrite an already existing unzipped file without warning.
185 std::string const unzipFile(std::string const & zipped_file,
186 std::string const & unzipped_file = std::string());
188 /// Returns true is path is absolute
189 bool AbsolutePath(std::string const & path);
191 /// Create absolute path. If impossible, don't do anything
192 std::string const ExpandPath(std::string const & path);
194 /** Convert relative path into absolute path based on a basepath.
195 If relpath is absolute, just use that.
196 If basepath doesn't exist use CWD.
198 std::string const MakeAbsPath(std::string const & RelPath = std::string(),
199 std::string const & BasePath = std::string());
201 /** Creates a nice compact path for displaying. The parameter
202 threshold, if given, specifies the maximal length of the path.
205 MakeDisplayPath(std::string const & path, unsigned int threshold = 1000);
207 /** Makes relative path out of absolute path.
208 If it is deeper than basepath,
209 it's easy. If basepath and abspath share something (they are all deeper
210 than some directory), it'll be rendered using ..'s. If they are completely
211 different, then the absolute path will be used as relative path
212 WARNING: the absolute path and base path must really be absolute paths!!!
215 MakeRelPath(std::string const & abspath, std::string const & basepath);
217 /// Strip filename from path name
218 std::string const OnlyPath(std::string const & fname);
220 /** Normalize a path. Constracts path/../path
221 * Also converts paths like /foo//bar ==> /foo/bar
223 std::string const NormalizePath(std::string const & path);
225 /// Strips path from filename
226 std::string const OnlyFilename(std::string const & fname);
228 /// Get the contents of a file as a huge std::string
229 std::string const GetFileContents(std::string const & fname);
231 /** Check and Replace Environmentvariables ${NAME} in Path.
232 Replaces all occurences of these, if they are found in the
234 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
236 std::string const ReplaceEnvironmentPath(std::string const & path);
238 /* Set \c link to the path \c file points to as a symbolic link.
239 If \c resolve is true, then \c link is an absolute path
240 Returns true if successful */
241 bool LyXReadLink(std::string const & file, std::string & link, bool resolve = false);
243 /// Uses kpsewhich to find tex files
244 std::string const findtexfile(std::string const & fil, std::string const & format);
246 /// remove the autosave-file and give a Message if it can't be done
247 void removeAutosaveFile(std::string const & filename);
249 /// read the BoundingBox entry from a ps/eps/pdf-file
250 std::string const readBB_from_PSFile(std::string const & file);
252 /** \param file1, file2 the two files to be compared. Must have absolute paths.
253 * \returns 1 if \c file1 has a more recent timestamp than \c file2,
254 * 0 if their timestamps are the same,
255 * -1 if \c file2 has a more recent timestamp than \c file1.
256 * If one of the files does not exist, the return value indicates the file
257 * which does exist. Eg, if \c file1 exists but \c file2 does not, return 1.
259 int compare_timestamps(std::string const & file1, std::string const & file2);
261 typedef std::pair<int, std::string> cmd_ret;
263 cmd_ret const RunCommand(std::string const & cmd);
265 } // namespace support