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