]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
* Some more clever elide mode for the LyX buffer tabs. In trunk
[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 file/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         /// filename without path
164         std::string onlyFileName() const;
165         /// filename without path and without extension
166         std::string onlyFileNameWithoutExt() const;
167         /// path without file name
168         FileName onlyPath() const;
169         /// used for display in the Gui
170         docstring displayName(int threshold = 1000) const;
171
172         /// change to a directory, return success
173         bool chdir() const;
174         
175         /// \param buffer_path if empty, uses `pwd`
176         docstring const relPath(std::string const & path) const;
177         
178         docstring const absoluteFilePath() const;
179
180 private:
181         ///
182         struct Private;
183         Private * const d;
184 };
185
186
187 bool operator==(FileName const &, FileName const &);
188 bool operator!=(FileName const &, FileName const &);
189 bool operator<(FileName const &, FileName const &);
190 bool operator>(FileName const &, FileName const &);
191 std::ostream & operator<<(std::ostream &, FileName const &);
192
193
194 /**
195  * Class for storing file names that appear in documents (e. g. child
196  * documents, included figures etc).
197  * The file name must not denote a file in our temporary directory, but a
198  * file that the user chose.
199  */
200 class DocFileName : public FileName {
201 public:
202         DocFileName();
203         /** \param abs_filename the file in question. Must have an absolute path.
204          *  \param save_abs_path how is the file to be output to file?
205          */
206         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
207         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
208
209         /** \param filename the file in question. May have either a relative
210          *  or an absolute path.
211          *  \param buffer_path if \c filename has a relative path, generate
212          *  the absolute path using this.
213          */
214         virtual void set(std::string const & filename, std::string const & buffer_path);
215
216         void erase();
217
218         bool saveAbsPath() const { return save_abs_path_; }
219         /// \param buffer_path if empty, uses `pwd`
220         std::string relFilename(std::string const & buffer_path = empty_string()) const;
221         /// \param buf_path if empty, uses `pwd`
222         std::string outputFilename(std::string const & buf_path = empty_string()) const;
223         
224         /** @returns a mangled representation of the absolute file name
225          *  suitable for use in the temp dir when, for example, converting
226          *  an image file to another format.
227          *
228          *  @param dir the directory that will contain this file with
229          *  its mangled name. This information is used by the mangling
230          *  algorithm when determining the maximum allowable length of
231          *  the mangled name.
232          *
233          *  An example of a mangled name:
234          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
235          *
236          *  It is guaranteed that
237          *  - two different filenames have different mangled names
238          *  - two FileName instances with the same filename have identical
239          *    mangled names.
240          *
241          *  Only the mangled file name is returned. It is not prepended
242          *  with @c dir.
243          */
244         std::string
245         mangledFilename(std::string const & dir = empty_string()) const;
246
247         /// \return true if the file is compressed.
248         bool isZipped() const;
249         /// \return the absolute file name without its .gz, .z, .Z extension
250         std::string unzippedFilename() const;
251
252 private:
253         bool save_abs_path_;
254         /// Cache for isZipped() because zippedFile() is expensive
255         mutable bool zipped_;
256         /// Is zipped_ valid?
257         mutable bool zipped_valid_;
258 };
259
260
261 bool operator==(DocFileName const &, DocFileName const &);
262 bool operator!=(DocFileName const &, DocFileName const &);
263
264 } // namespace support
265 } // namespace lyx
266
267 #endif