]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
lyx-devel.diff
[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 ///
87 bool IsSGMLFilename(string const & filename);
88
89 /** Returns the path of a library data file.
90   Search the file name.ext in the subdirectory dir of
91   \begin{enumerate}
92     \item user_lyxdir
93     \item build_lyxdir (if not empty)
94     \item system_lyxdir
95   \end{enumerate}
96     The third parameter `ext' is optional.
97 */
98 string const LibFileSearch(string const & dir, string const & name, 
99                      string const & ext = string());
100
101 /** Same as LibFileSearch(), but tries first to find an
102   internationalized version of the file by prepending $LANG_ to the
103   name 
104   */
105 string const
106 i18nLibFileSearch(string const & dir, string const & name, 
107                   string const & ext = string());
108
109 ///
110 string const GetEnv(string const & envname);
111
112 /// A helper function.
113 string const GetEnvPath(string const & name);
114
115 ///
116 bool PutEnv(string const & envstr);
117
118 ///
119 bool PutEnvPath(string const & envstr);
120
121 /// Substitutes active latex characters with underscores in filename
122 string const MakeLatexName(string const & file);
123
124 /// Put the name in quotes suitable for the current shell
125 string const QuoteName(string const & file);
126
127 /// Is a filename/path absolute?
128 bool AbsolutePath(string const & path);
129
130 /// Add a filename to a path. Any path from filename is stripped first.
131 string const AddName(string const & path, string const & fname);
132
133 /// Append sub-directory(ies) to path in an intelligent way
134 string const AddPath(string const & path, string const & path2);
135
136 /** Change extension of oldname to extension.
137  If oldname does not have an extension, it is appended.
138  If the extension is empty, any extension is removed from the name.
139  */
140 string const
141 ChangeExtension(string const & oldname, string const & extension);
142
143 /// Return the extension of the file (not including the .)
144 string const GetExtension(string const & name);
145
146 /// Create absolute path. If impossible, don't do anything
147 string const ExpandPath(string const & path);
148
149 /** Convert relative path into absolute path based on a basepath.
150   If relpath is absolute, just use that.
151   If basepath doesn't exist use CWD.
152   */
153 string const MakeAbsPath(string const & RelPath = string(), 
154                          string const & BasePath = string());
155
156 /** Creates a nice compact path for displaying. The parameter
157   threshold, if given, specifies the maximal length of the path.
158   */
159 string const
160 MakeDisplayPath(string const & path,
161                 unsigned int threshold = 1000);
162
163 /** Makes relative path out of absolute path.
164   If it is deeper than basepath,
165   it's easy. If basepath and abspath share something (they are all deeper
166   than some directory), it'll be rendered using ..'s. If they are completely
167   different, then the absolute path will be used as relative path
168   WARNING: the absolute path and base path must really be absolute paths!!!
169   */
170 string const
171 MakeRelPath(string const & abspath, string const & basepath);
172
173 /// Strip filename from path name
174 string const OnlyPath(string const & fname);
175
176 /// Normalize a path. Constracts path/../path
177 string const NormalizePath(string const & path);
178
179 /// Strips path from filename
180 string const OnlyFilename(string const & fname);
181
182 /// Get the contents of a file as a huge string
183 string const GetFileContents(string const & fname);
184
185 /// Cleanup a path if necessary. Currently only useful with OS/2
186 string const CleanupPath(string const & path);
187
188 /** Check and Replace Environmentvariables ${NAME} in Path.
189     Replaces all occurences of these, if they are found in the
190     environment.
191     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
192 */
193 string const ReplaceEnvironmentPath(string const & path);
194
195 /* Set Link to the path file points to as a symbolic link.
196    Returns true if successful */
197 bool LyXReadLink(string const & file, string & Link);
198
199 /// Uses kpsewhich to find tex files
200 string const findtexfile(string const & fil, string const & format);
201
202 /// remove the autosave-file and give a Message if it can't be done
203 void removeAutosaveFile(string const & filename);
204
205 #endif