]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
Check return value of regex_match instead of looking at first match
[lyx.git] / src / support / FileName.h
1 // -*- C++ -*-
2 /**
3  * \file FileName.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef FILENAME_H
13 #define FILENAME_H
14
15 #include "support/os.h"
16 #include "support/strfwd.h"
17
18 #include <ctime>
19 #include <set>
20
21
22 namespace lyx {
23 namespace support {
24
25 /// Defined in "FileNameList.h".
26 class FileNameList;
27
28 /**
29  * Class for storing file names.
30  * The file name may be empty. If it is not empty it is an absolute path.
31  * The file may or may not exist.
32  */
33 class FileName {
34 public:
35         /// Constructor for empty filenames
36         FileName();
37         /** Constructor for nonempty filenames.
38          * explicit because we don't want implicit conversion of relative
39          * paths in function arguments (e.g. of unlink).
40          * \param abs_filename the file in question. Must have an absolute path.
41          * Encoding is always UTF-8.
42          */
43         explicit FileName(std::string const & abs_filename);
44
45         /// copy constructor.
46         FileName(FileName const &);
47
48         /// constructor with base name and suffix.
49         FileName(FileName const & fn, std::string const & suffix);
50
51         ///
52         FileName & operator=(FileName const &);
53
54         virtual ~FileName();
55         /** Set a new filename.
56          * \param filename the file in question. Must have an absolute path.
57          * Encoding is always UTF-8.
58          */
59         void set(std::string const & filename);
60         void set(FileName const & fn, std::string const & suffix);
61         virtual void erase();
62         /// Is this filename empty?
63         bool empty() const;
64         /// Is the filename absolute?
65         static bool isAbsolute(std::string const & name);
66
67         /// get the absolute file name in UTF-8 encoding
68         std::string absFileName() const;
69
70         /** returns an absolute pathname (whose resolution does not involve
71           * '.', '..', or symbolic links) in UTF-8 encoding
72           */
73         std::string realPath() const;
74
75         /**
76          * Get the file name in the encoding used by the file system.
77          * Only use this for passing file names to external commands.
78          * Warning: On Windows this is not unicode safe and should not
79          * be used for accessing files with an fstream, for example.
80          */
81         std::string toFilesystemEncoding() const;
82
83         /**
84          * Get the file name in a unicode safe encoding used by the file system.
85          * Only use this for accessing the file with standard I/O functions
86          * non explicitly unicode aware, e.g. with an fstream. This can also
87          * be used for passing file names to external commands, but only if
88          * you are sure that the stem of the name will not be used for
89          * producing derivative files. For example, don't use this for passing
90          * file names to LaTeX, as the stem of the .dvi file will not correspond
91          * to the stem of the .tex file anymore.
92          * Use os::CREATE if the file is to be accessed for writing.
93          */
94         std::string toSafeFilesystemEncoding(os::file_access how = os::EXISTING) const;
95
96         /// returns true if the file exists
97         bool exists() const;
98         /// refreshes the file info
99         void refresh() const;
100         /// \return true if this object points to a symbolic link.
101         bool isSymLink() const;
102         /// \return true if the file is empty.
103         bool isFileEmpty() const;
104         /// returns time of last write access
105         std::time_t lastModified() const;
106         /// generates a checksum of a file
107         virtual unsigned long checksum() const;
108         /// return true when file is readable but not writable
109         bool isReadOnly() const;
110         /// return true when it names a directory
111         bool isDirectory() const;
112         /// return true when directory is readable
113         bool isReadableDirectory() const;
114         /// return true when it is a file and readable
115         virtual bool isReadableFile() const;
116         /// return true when file/directory is writable
117         bool isWritable() const;
118         /// return true when file/directory is writable (write test file)
119         bool isDirWritable() const;
120         /// \return list other files in the directory having optional extension 'ext'.
121         FileNameList dirList(std::string const & ext) const;
122         
123         /// copy a file
124         /// \return true when file/directory is writable (write test file)
125         /// \warning This methods has different semantics when system level
126         /// copy command, it will overwrite the \c target file if it exists,
127         /// If \p keepsymlink is true, the copy will be written to the symlink
128         /// target. Otherwise, the symlink will be destroyed.
129         bool copyTo(FileName const & target, bool keepsymlink = false) const;
130
131         /// remove pointed file.
132         /// \return true on success.
133         bool removeFile() const;
134
135         /// rename pointed file.
136         /// \return false if the operation fails or if the \param target file
137         /// already exists.
138         /// \return true on success.
139         bool renameTo(FileName const & target) const;
140
141         /// move pointed file to \param target.
142         /// If \p target exists it will be overwritten (if it is a symlink,
143         /// the symlink will be destroyed).
144         /// \return true on success.
145         bool moveTo(FileName const & target) const;
146         bool link(FileName const & name) const;
147
148         /// change mode of pointed file.
149         /// This methods does nothing and return true on platforms that does not
150         /// support this.
151         /// \return true on success.
152         bool changePermission(unsigned long int mode) const;
153
154         /// remove pointed directory and all contents.
155         /// \return true on success.
156         bool destroyDirectory() const;
157         /// Creates pointed directory.
158         /// \return true on success.
159         bool createDirectory(int permissions) const;
160         /// Creates pointed path.
161         /// \return true on success.
162         bool createPath() const;
163
164         /// Get the contents of a file as a huge docstring.
165         /// \param encoding defines the encoding of the file contents.
166         /// Only four encodings are supported:
167         /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
168         /// current system locale.
169         docstring fileContents(std::string const & encoding) const;
170
171         /// Change extension.
172         /**
173         * If oldname does not have an extension, it is appended.
174         * If the extension is empty, any extension is removed from the name.
175         */
176         void changeExtension(std::string const & extension);
177
178         static FileName fromFilesystemEncoding(std::string const & name);
179
180         /// get the current working directory
181         static FileName getcwd();
182
183         static FileName tempPath();
184
185         /// filename without path
186         std::string onlyFileName() const;
187         /// filename without path and without extension
188         std::string onlyFileNameWithoutExt() const;
189         /// only extension after the last dot.
190         std::string extension() const;
191         /** checks if the file has the given extension
192                 on Windows and Mac it compares case insensitive */
193         bool hasExtension(const std::string & ext);
194         /// path without file name
195         FileName onlyPath() const;
196         /// path of parent directory
197         /// returns empty path for root directory
198         FileName parentPath() const;
199         /// used for display in the Gui
200         docstring displayName(int threshold = 1000) const;
201
202         /// change to a directory, return success
203         bool chdir() const;
204         
205         /// \param buffer_path if empty, uses `pwd`
206         docstring const relPath(std::string const & path) const;
207         
208         docstring const absoluteFilePath() const;
209
210 private:
211         friend bool equivalent(FileName const &, FileName const &);
212         /// Set for tracking of already visited file names.
213         /// Uses operator==() (which may be case insensitive), and not
214         /// equvalent(), so that symlinks are not resolved.
215         typedef std::set<FileName> FileNameSet;
216         /// Helper for public copyTo() to find circular symlink chains
217         bool copyTo(FileName const &, bool, FileNameSet &) const;
218         ///
219         struct Private;
220         Private * const d;
221 };
222
223
224 /// \return true if lhs and rhs represent the same file. E.g.,
225 /// they might be hardlinks of one another.
226 bool equivalent(FileName const & lhs, FileName const & rhs);
227 /// \return true if the absolute path names are the same.
228 bool operator==(FileName const &, FileName const &);
229 ///
230 bool operator!=(FileName const &, FileName const &);
231 /// Lexically compares the absolute path names.
232 bool operator<(FileName const &, FileName const &);
233 /// Lexically compares the absolute path names.
234 bool operator>(FileName const &, FileName const &);
235 /// Writes the absolute path name to the stream.
236 std::ostream & operator<<(std::ostream &, FileName const &);
237
238
239 /**
240  * Class for storing file names that appear in documents (e. g. child
241  * documents, included figures etc).
242  * The file name must not denote a file in our temporary directory, but a
243  * file that the user chose.
244  */
245 class DocFileName : public FileName {
246 public:
247         DocFileName();
248         /** \param abs_filename the file in question. Must have an absolute path.
249          *  \param save_abs_path how is the filename to be output?
250          */
251         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
252         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
253
254         /** \param filename the file in question. May have either a relative
255          *  or an absolute path.
256          *  \param buffer_path if \c filename has a relative path, generate
257          *  the absolute path using this.
258          */
259         void set(std::string const & filename, std::string const & buffer_path);
260         ///
261         void erase();
262         ///
263         bool saveAbsPath() const { return save_abs_path_; }
264         /// \param buffer_path if empty, uses `pwd`
265         std::string relFileName(std::string const & buffer_path = empty_string()) const;
266         /// \param buf_path if empty, uses `pwd`
267         std::string outputFileName(std::string const & buf_path = empty_string()) const;
268         
269         /** @returns a mangled representation of the absolute file name
270          *  suitable for use in the temp dir when, for example, converting
271          *  an image file to another format.
272          *
273          *  @param dir the directory that will contain this file with
274          *  its mangled name. This information is used by the mangling
275          *  algorithm when determining the maximum allowable length of
276          *  the mangled name.
277          *
278          *  An example of a mangled name:
279          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
280          *
281          *  It is guaranteed that
282          *  - two different filenames have different mangled names
283          *  - two FileName instances with the same filename have identical
284          *    mangled names.
285          *
286          *  Only the mangled file name is returned. It is not prepended
287          *  with @c dir.
288          */
289         std::string
290         mangledFileName(std::string const & dir = empty_string()) const;
291
292         /// \return the absolute file name without its .gz, .z, .Z extension
293         std::string unzippedFileName() const;
294
295 private:
296         /// Records whether we should save (or export) the filename as a relative
297         /// or absolute path.
298         bool save_abs_path_;
299 };
300
301
302 /// \return true if these have the same absolute path name AND 
303 /// if save_abs_path_ has the same value in both cases.
304 bool operator==(DocFileName const &, DocFileName const &);
305 ///
306 bool operator!=(DocFileName const &, DocFileName const &);
307
308 } // namespace support
309 } // namespace lyx
310
311 #endif