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