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