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