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