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