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