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
24 /// remove directory and all contents, returns 0 on success
25 int destroyDir(string const & tmpdir);
28 string const CreateBufferTmpDir(string const & pathfor = string());
30 /// Creates directory. Returns true on success
31 bool createDirectory(string const & name, int permissions);
34 string const CreateLyXTmpDir(string const & deflt);
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".
43 string const FileOpenSearch(string const & path, string const & name,
44 string const & ext = string());
46 /** Returns the real name of file name in directory path, with optional
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).
51 string const FileSearch(string const & path, string const & name,
52 string const & ext = string());
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());
58 /** Is directory read only?
63 bool IsDirWriteable (string const & path);
65 /** Is a file readable ?
66 Returns true if the file `path' is readable.
68 bool IsFileReadable (string const & path);
70 /** Is file read only?
74 -1: error (doesn't exist, no access, anything else)
76 int IsFileWriteable (string const & path);
79 bool IsLyXFilename(string const & filename);
82 bool IsSGMLFilename(string const & filename);
84 /** Returns the path of a library data file.
85 Search the file name.ext in the subdirectory dir of
88 \item build_lyxdir (if not empty)
91 The third parameter `ext' is optional.
93 string const LibFileSearch(string const & dir, string const & name,
94 string const & ext = string());
96 /** Same as LibFileSearch(), but tries first to find an
97 internationalized version of the file by prepending $LANG_ to the
101 i18nLibFileSearch(string const & dir, string const & name,
102 string const & ext = string());
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 ;-)
110 string const LibScriptSearch(string const & command);
113 string const GetEnv(string const & envname);
115 /// A helper function.
116 string const GetEnvPath(string const & name);
118 /// Substitutes active latex characters with underscores in filename
119 string const MakeLatexName(string const & file);
121 /// Put the name in quotes suitable for the current shell
122 string const QuoteName(string const & file);
124 /// Add a filename to a path. Any path from filename is stripped first.
125 string const AddName(string const & path, string const & fname);
127 /// Append sub-directory(ies) to path in an intelligent way
128 string const AddPath(string const & path, string const & path2);
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.
135 ChangeExtension(string const & oldname, string const & extension);
137 /// Return the extension of the file (not including the .)
138 string const GetExtension(string const & name);
140 /// Return the type of the file as an extension from contents
141 string const getExtFromContents(string const & name);
143 /// check for zipped file
144 bool zippedFile(string const & name);
147 string const unzipFile(string const & zipped_file);
149 /// Returns true is path is absolute
150 bool AbsolutePath(string const & path);
152 /// Create absolute path. If impossible, don't do anything
153 string const ExpandPath(string const & path);
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.
159 string const MakeAbsPath(string const & RelPath = string(),
160 string const & BasePath = string());
162 /** Creates a nice compact path for displaying. The parameter
163 threshold, if given, specifies the maximal length of the path.
166 MakeDisplayPath(string const & path, unsigned int threshold = 1000);
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!!!
176 MakeRelPath(string const & abspath, string const & basepath);
178 /// Strip filename from path name
179 string const OnlyPath(string const & fname);
181 /** Normalize a path. Constracts path/../path
182 * Also converts paths like /foo//bar ==> /foo/bar
184 string const NormalizePath(string const & path);
186 /// Strips path from filename
187 string const OnlyFilename(string const & fname);
189 /// Get the contents of a file as a huge string
190 string const GetFileContents(string const & fname);
192 /** Check and Replace Environmentvariables ${NAME} in Path.
193 Replaces all occurences of these, if they are found in the
195 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
197 string const ReplaceEnvironmentPath(string const & path);
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);
204 /// Uses kpsewhich to find tex files
205 string const findtexfile(string const & fil, string const & format);
207 /// remove the autosave-file and give a Message if it can't be done
208 void removeAutosaveFile(string const & filename);
210 /// read the BoundingBox entry from a ps/eps/pdf-file
211 string const readBB_from_PSFile(string const & file);
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.
220 string const copyFileToDir(string const & path, string const & file);
222 typedef std::pair<int, string> cmd_ret;
224 cmd_ret const RunCommand(string const & cmd);
226 } // namespace support