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