]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
0151c7fa819a3652efc368c99a145bde86b5b499
[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 <string>
19 #include <vector>
20
21
22 namespace lyx {
23 namespace support {
24
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         
89         /// copy a file
90         /// \return true when file/directory is writable (write test file)
91         /// \param overwrite: set to true if we should erase the \c target 
92         /// file if it exists,
93         bool copyTo(FileName const & target, bool overwrite = false) const;
94
95         /// remove pointed file.
96         /// \retrun true on success.
97         bool removeFile() const;
98
99         /// remove directory and all contents, returns true on success
100         bool destroyDirectory() const;
101         /// Creates directory. Returns true on success
102         bool createDirectory(int permissions) const;
103
104         /// \return list files in a directory having optional extension ext..
105         std::vector<FileName> dirList(
106                 std::string const & ext = std::string());
107
108         /// Get the contents of a file as a huge std::string
109         std::string fileContents() const;
110         /**
111          * Get a FileName from \p name in the encoding used by the file system.
112          * Only use this for filenames you got directly from the file system,
113          * e.g. from reading a directory.
114          * \p name must have an absolute path.
115          */
116
117         /// Change extension.
118         /**
119         * If oldname does not have an extension, it is appended.
120         * If the extension is empty, any extension is removed from the name.
121         */
122          void changeExtension(std::string const & extension);
123
124         /** Guess the file format name (as in Format::name()) from contents.
125          Normally you don't want to use this directly, but rather
126          Formats::getFormatFromFile().
127          */
128         std::string guessFormatFromContents() const;
129
130         /// check for zipped file
131         bool isZippedFile() const;
132
133         static FileName fromFilesystemEncoding(std::string const & name);
134         /// (securely) create a temporary file in the given dir with the given mask
135         /// \p mask must be in filesystem encoding
136         static FileName tempName(FileName const & dir = FileName(),
137                                                 std::string const & mask = std::string());
138
139         /// filename without path
140         std::string onlyFileName() const;
141         /// path without file name
142         FileName onlyPath() const;
143         /// used for display in the Gui
144         docstring displayName(int threshold = 1000) const;
145
146 private:
147         friend class DocFileName;
148         ///
149         struct Private;
150         Private * const d;
151 };
152
153
154 bool operator==(FileName const &, FileName const &);
155 bool operator!=(FileName const &, FileName const &);
156 bool operator<(FileName const &, FileName const &);
157 bool operator>(FileName const &, FileName const &);
158 std::ostream & operator<<(std::ostream &, FileName const &);
159
160
161 /**
162  * Class for storing file names that appear in documents (e. g. child
163  * documents, included figures etc).
164  * The file name must not denote a file in our temporary directory, but a
165  * file that the user chose.
166  */
167 class DocFileName : public FileName {
168 public:
169         DocFileName();
170         /** \param abs_filename the file in question. Must have an absolute path.
171          *  \param save_abs_path how is the file to be output to file?
172          */
173         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
174         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
175
176         /** \param filename the file in question. May have either a relative
177          *  or an absolute path.
178          *  \param buffer_path if \c filename has a relative path, generate
179          *  the absolute path using this.
180          */
181         void set(std::string const & filename, std::string const & buffer_path);
182
183         void erase();
184
185         bool saveAbsPath() const { return save_abs_path_; }
186         /// \param buffer_path if empty, uses `pwd`
187         std::string const relFilename(std::string const & buffer_path = std::string()) const;
188         /// \param buf_path if empty, uses `pwd`
189         std::string const outputFilename(std::string const & buf_path = std::string()) const;
190         
191         /** @returns a mangled representation of the absolute file name
192          *  suitable for use in the temp dir when, for example, converting
193          *  an image file to another format.
194          *
195          *  @param dir the directory that will contain this file with
196          *  its mangled name. This information is used by the mangling
197          *  algorithm when determining the maximum allowable length of
198          *  the mangled name.
199          *
200          *  An example of a mangled name:
201          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
202          *
203          *  It is guaranteed that
204          *  - two different filenames have different mangled names
205          *  - two FileName instances with the same filename have identical
206          *    mangled names.
207          *
208          *  Only the mangled file name is returned. It is not prepended
209          *  with @c dir.
210          */
211         std::string const
212         mangledFilename(std::string const & dir = std::string()) const;
213
214         /// \return true if the file is compressed.
215         bool isZipped() const;
216         /// \return the absolute file name without its .gz, .z, .Z extension
217         std::string const unzippedFilename() const;
218
219 private:
220         bool save_abs_path_;
221         /// Cache for isZipped() because zippedFile() is expensive
222         mutable bool zipped_;
223         /// Is zipped_ valid?
224         mutable bool zipped_valid_;
225 };
226
227
228 bool operator==(DocFileName const &, DocFileName const &);
229 bool operator!=(DocFileName const &, DocFileName const &);
230
231
232 } // namespace support
233 } // namespace lyx
234
235 #endif