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