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
7 #ifndef __LYX_FILETOOL_H__
8 #define __LYX_FILETOOL_H__
22 Use this instead of FILE *, it gives a much better structure.
23 It should prehaps do a bit more error checking than it does now.
24 Currently it is a verbatim copy from p309 of Bjarne Stroupstrups
25 The C++ Programming Language. + some additions.
37 FilePtr(LString 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(LString 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(LString const &name, file_mode mode) {
82 // do appropiate #ifdef here so support EMX
84 case read: strcpy(modestr, "r"); break;
85 case write: strcpy(modestr, "w"); break;
87 case read: strcpy(modestr,"rt"); break; // Can read both DOS & UNIX text files.
88 case write: strcpy(modestr,"w"); break; // Write UNIX text files.
91 case update: strcpy(modestr, "r+"); break;
92 case truncate: strcpy(modestr, "w+"); break;
94 // Should probably be rewritten to use open(2)
95 if((p = fopen(name.c_str(), modestr))) {
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 LString CreateBufferTmpDir (LString const & pathfor = LString());
123 /// Creates directory. Returns true on succes.
124 bool createDirectory(LString const & name, int permissions);
127 LString CreateLyXTmpDir (LString const & deflt);
130 int DestroyBufferTmpDir (LString const & tmpdir);
133 int DestroyLyXTmpDir (LString 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 LString FileOpenSearch (LString const & path, LString const & name,
143 LString const & ext = LString());
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 LString FileSearch(LString const & path, LString const & name,
151 LString const & ext = LString());
153 /** Is directory read only?
157 -1: error- couldn't find out, or unsure
159 int IsDirWriteable (LString const & path);
161 /** Is a file readable ?
162 Returns true if the file `path' is readable.
164 bool IsFileReadable (LString const & path);
166 /** Is file read only?
170 -1: error (doesn't exist, no access, anything else)
172 int IsFileWriteable (LString const & path);
175 bool IsLyXFilename(LString const & filename);
178 bool IsSGMLFilename(LString 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 LString LibFileSearch(LString const & dir, LString const & name,
190 LString const & ext = LString());
192 /** Same as LibFileSearch(), but tries first to find an
193 internationalized version of the file by prepending $LANG_ to the
196 LString i18nLibFileSearch(LString const & dir, LString const & name,
197 LString const & ext = LString());
199 /// Substitutes spaces with underscores in filename (and path)
200 LString SpaceLess(LString const & file);
202 /** Returns an unique name to be used as a temporary file. If given,
203 'mask' should the prefix to the temporary file, the rest of the
204 temporary filename will be made from the pid and three letters.
206 LString TmpFileName(LString const & dir = LString(),
207 LString const & mask = "lyx_tmp");
209 /// Is a filename/path absolute?
210 bool AbsolutePath(LString const &path);
212 /// Add a filename to a path. Any path from filename is stripped first.
213 LString AddName(LString const &Path, LString const &Filename);
215 /// Append sub-directory(ies) to path in an intelligent way
216 LString AddPath(LString const & path, LString const & path2);
218 /** Change extension of oldname to extension.
219 If no_path is true, the path is stripped from the filename.
220 If oldname does not have an extension, it is appended.
221 If the extension is empty, any extension is removed from the name.
223 LString ChangeExtension(LString const & oldname, LString const & extension,
226 /// Create absolute path. If impossible, don't do anything
227 LString ExpandPath(LString const &path);
229 /// gets current working directory
232 /// A helper function.
233 inline LString getEnvPath(char const *name)
236 pathlist = getenv(name);
238 pathlist.subst(':', ';');
240 pathlist.subst('\\', '/');
242 return pathlist.strip(';');
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 LString MakeAbsPath(LString const &RelPath = LString(),
250 LString const &BasePath = LString());
252 /** Creates a nice compact path for displaying. The parameter
253 threshold, if given, specifies the maximal length of the path.
255 LString MakeDisplayPath(LString const & path, 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 LString MakeRelPath(LString const & abspath, LString const & basepath);
266 /// Strip filename from path name
267 LString OnlyPath(LString const &Filename);
269 /// Normalize a path. Constracts path/../path
270 LString NormalizePath(LString const &path);
272 /// Strips path from filename
273 LString OnlyFilename(LString const &Filename);
275 /// Cleanup a path if necessary. Currently only useful with OS/2
276 LString CleanupPath(LString 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 LString ReplaceEnvironmentPath(LString 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(LString const & file, LString & Link);