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