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