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