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