2 /* lyx-filetool.h : tools functions for file/path handling
3 this file is part of LyX, the High Level Word Processor
4 copyright (C) 1995-1997, Matthias Ettrich and the LyX Team
20 #include "support/lstrings.h"
23 Use this instead of FILE *, it gives a much better structure.
24 It should prehaps do a bit more error checking than it does now.
25 Currently it is a verbatim copy from p309 of Bjarne Stroupstrups
26 The C++ Programming Language. + some additions.
38 FilePtr(string const & name, file_mode mode) {
43 FilePtr(FILE * pp) { init(); p = pp; }
45 ~FilePtr() { close(); }
47 /** Use this if you want to rebind the FilePtr to another file.
49 FilePtr & reopen(string const & name, file_mode mode) {
50 // close the file it it is already open
58 Use this with some carefullness. After it has been used
59 the FilePtr is unusable. Only use it if it is important
60 that the file is closed before the FilePtr goes out
64 int result = fclose(p);
70 /// automatic converson to FILE* if that is needed.
71 operator FILE*() { return p; }
73 FilePtr & operator=(FILE * f) { p=f; return *this;}
75 FILE * operator()() { return p; }
78 void do_open(string const & name, file_mode mode) {
82 // do appropiate #ifdef here so support EMX
84 case read: modestr = "r"; break;
85 case write: modestr = "w"; break;
87 case read: modestr = "rt"; break; // Can read both DOS & UNIX text files.
88 case write: modestr = "w"; break; // Write UNIX text files.
91 case update: modestr = "r+"; break;
92 case truncate: modestr = "w+"; break;
94 // Should probably be rewritten to use open(2)
95 if((p = fopen(name.c_str(), modestr.c_str()))) {
96 // file succesfully opened.
97 if (fcntl(fileno(p),F_SETFD,FD_CLOEXEC) == -1) {
101 // we have an error let's check what it is.
104 // Internal LyX error.
105 lyxerr.print("FilePtr: Wrong parameter given to fopen.");
114 void init() { p = 0; }
121 string CreateBufferTmpDir (string const & pathfor = string());
123 /// Creates directory. Returns true on succes.
124 bool createDirectory(string const & name, int permissions);
127 string CreateLyXTmpDir (string const & deflt);
130 int DestroyBufferTmpDir (string const & tmpdir);
133 int DestroyLyXTmpDir (string const & tmpdir);
135 /** Find file by searching several directories.
136 Uses a string of paths separated by ";"s to find a file to open.
137 Can't cope with pathnames with a ';' in them. Returns full path to file.
138 If path entry begins with $$LyX/, use system_lyxdir.
139 If path entry begins with $$User/, use user_lyxdir.
140 Example: "$$User/doc;$$LyX/doc".
142 string FileOpenSearch (string const & path, string const & name,
143 string const & ext = string());
145 /** Returns the real name of file name in directory path, with optional
147 The file is searched in the given path (unless it is an absolute
148 file name), first directly, and then with extension .ext (if given).
150 string FileSearch(string const & path, string const & name,
151 string const & ext = string());
153 /** Is directory read only?
157 -1: error- couldn't find out, or unsure
159 int IsDirWriteable (string const & path);
161 /** Is a file readable ?
162 Returns true if the file `path' is readable.
164 bool IsFileReadable (string const & path);
166 /** Is file read only?
170 -1: error (doesn't exist, no access, anything else)
172 int IsFileWriteable (string const & path);
175 bool IsLyXFilename(string const & filename);
178 bool IsSGMLFilename(string const & filename);
180 /** Returns the path of a library data file.
181 Search the file name.ext in the subdirectory dir of
184 \item build_lyxdir (if not empty)
187 The third parameter `ext' is optional.
189 string LibFileSearch(string const & dir, string const & name,
190 string const & ext = string());
192 /** Same as LibFileSearch(), but tries first to find an
193 internationalized version of the file by prepending $LANG_ to the
196 string i18nLibFileSearch(string const & dir, string const & name,
197 string const & ext = string());
200 string GetEnv(string const & envname);
202 /// A helper function.
203 string GetEnvPath(string const & name);
206 bool PutEnv(string const & envstr);
209 bool PutEnvPath(string const & envstr);
211 /// Substitutes spaces with underscores in filename (and path)
212 string SpaceLess(string const & file);
214 /** Returns an unique name to be used as a temporary file. If given,
215 'mask' should the prefix to the temporary file, the rest of the
216 temporary filename will be made from the pid and three letters.
218 string TmpFileName(string const & dir = string(),
219 string const & mask = "lyx_tmp");
221 /// Is a filename/path absolute?
222 bool AbsolutePath(string const & path);
224 /// Add a filename to a path. Any path from filename is stripped first.
225 string AddName(string const & path, string const & fname);
227 /// Append sub-directory(ies) to path in an intelligent way
228 string AddPath(string const & path, string const & path2);
230 /** Change extension of oldname to extension.
231 If no_path is true, the path is stripped from the filename.
232 If oldname does not have an extension, it is appended.
233 If the extension is empty, any extension is removed from the name.
235 string ChangeExtension(string const & oldname, string const & extension,
238 /// Create absolute path. If impossible, don't do anything
239 string ExpandPath(string const & path);
241 /// gets current working directory
245 /** Convert relative path into absolute path based on a basepath.
246 If relpath is absolute, just use that.
247 If basepath doesn't exist use CWD.
249 string MakeAbsPath(string const & RelPath = string(),
250 string const & BasePath = string());
252 /** Creates a nice compact path for displaying. The parameter
253 threshold, if given, specifies the maximal length of the path.
255 string MakeDisplayPath(string const & path, unsigned int threshold=1000);
257 /** Makes relative path out of absolute path.
258 If it is deeper than basepath,
259 it's easy. If basepath and abspath share something (they are all deeper
260 than some directory), it'll be rendered using ..'s. If they are completely
261 different, then the absolute path will be used as relative path
262 WARNING: the absolute path and base path must really be absolute paths!!!
264 string MakeRelPath(string const & abspath, string const & basepath);
266 /// Strip filename from path name
267 string OnlyPath(string const & fname);
269 /// Normalize a path. Constracts path/../path
270 string NormalizePath(string const & path);
272 /// Strips path from filename
273 string OnlyFilename(string const & fname);
275 /// Cleanup a path if necessary. Currently only useful with OS/2
276 string CleanupPath(string const & path) ;
278 /** Check and Replace Environmentvariables ${NAME} in Path.
279 Replaces all occurences of these, if they are found in the
281 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
283 string ReplaceEnvironmentPath(string const & path);
285 /* Set Link to the path File Points to as a symbolic link.
286 Return True if succesfull, False other wise */
287 bool LyXReadLink(string const & file, string & Link);