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-1999, 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 << "FilePtr: Wrong parameter given to fopen." << endl;
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 active latex characters with underscores in filename
212 string MakeLatexName(string const & file);
214 /// Put the name in quotes suitable for the current shell
215 string QuoteName(string const & file);
217 /** Returns an unique name to be used as a temporary file. If given,
218 'mask' should the prefix to the temporary file, the rest of the
219 temporary filename will be made from the pid and three letters.
221 string TmpFileName(string const & dir = string(),
222 string const & mask = "lyx_tmp");
224 /// Is a filename/path absolute?
225 bool AbsolutePath(string const & path);
227 /// Add a filename to a path. Any path from filename is stripped first.
228 string AddName(string const & path, string const & fname);
230 /// Append sub-directory(ies) to path in an intelligent way
231 string AddPath(string const & path, string const & path2);
233 /** Change extension of oldname to extension.
234 If no_path is true, the path is stripped from the filename.
235 If oldname does not have an extension, it is appended.
236 If the extension is empty, any extension is removed from the name.
238 string ChangeExtension(string const & oldname, string const & extension,
241 /// Create absolute path. If impossible, don't do anything
242 string ExpandPath(string const & path);
244 /// gets current working directory
248 /** Convert relative path into absolute path based on a basepath.
249 If relpath is absolute, just use that.
250 If basepath doesn't exist use CWD.
252 string MakeAbsPath(string const & RelPath = string(),
253 string const & BasePath = string());
255 /** Creates a nice compact path for displaying. The parameter
256 threshold, if given, specifies the maximal length of the path.
258 string MakeDisplayPath(string const & path, unsigned int threshold= 1000);
260 /** Makes relative path out of absolute path.
261 If it is deeper than basepath,
262 it's easy. If basepath and abspath share something (they are all deeper
263 than some directory), it'll be rendered using ..'s. If they are completely
264 different, then the absolute path will be used as relative path
265 WARNING: the absolute path and base path must really be absolute paths!!!
267 string MakeRelPath(string const & abspath, string const & basepath);
269 /// Strip filename from path name
270 string OnlyPath(string const & fname);
272 /// Normalize a path. Constracts path/../path
273 string NormalizePath(string const & path);
275 /// Strips path from filename
276 string OnlyFilename(string const & fname);
278 /// Cleanup a path if necessary. Currently only useful with OS/2
279 string CleanupPath(string const & path) ;
281 /** Check and Replace Environmentvariables ${NAME} in Path.
282 Replaces all occurences of these, if they are found in the
284 Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
286 string ReplaceEnvironmentPath(string const & path);
288 /* Set Link to the path File Points to as a symbolic link.
289 Return True if succesfull, False other wise */
290 bool LyXReadLink(string const & file, string & Link);
292 /* Uses kpsewhich to find tex files */
293 string findtexfile(string const & fil, string const & format);