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