4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
15 #include "support/strfwd.h"
23 /// Defined in "FileNameList.h".
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.
33 /// Constructor for empty filenames
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.
41 explicit FileName(std::string const & abs_filename);
44 FileName(FileName const &);
46 /// constructor with base name and suffix.
47 FileName(FileName const & fn, std::string const & suffix);
50 FileName & operator=(FileName const &);
53 /** Set a new filename.
54 * \param filename the file in question. Must have an absolute path.
55 * Encoding is always UTF-8.
57 virtual void set(std::string const & filename);
58 virtual void set(FileName const & fn, std::string const & suffix);
60 /// Is this filename empty?
62 /// Is the filename absolute?
63 static bool isAbsolute(std::string const & name);
65 /// get the absolute file name in UTF-8 encoding
66 std::string absFilename() const;
68 /** returns an absolute pathname (whose resolution does not involve
69 * '.', '..', or symbolic links) in UTF-8 encoding
71 std::string realPath() const;
74 * Get the file name in the encoding used by the file system.
75 * Only use this for accessing the file, e.g. with an fstream.
77 std::string toFilesystemEncoding() const;
79 /// returns true if the file exists
81 /// refreshes the file info
83 /// \return true if this object points to a symbolic link.
84 bool isSymLink() const;
85 /// \return true if the file is empty.
86 bool isFileEmpty() const;
87 /// returns time of last write access
88 std::time_t lastModified() const;
89 /// generates a checksum of a file
90 virtual unsigned long checksum() const;
91 /// return true when file is readable but not writable
92 bool isReadOnly() const;
93 /// return true when it names a directory
94 bool isDirectory() const;
95 /// return true when directory is readable
96 bool isReadableDirectory() const;
97 /// return true when it is a file and readable
98 virtual bool isReadableFile() const;
99 /// return true when file/directory is writable
100 bool isWritable() const;
101 /// return true when file/directory is writable (write test file)
102 bool isDirWritable() const;
103 /// \return list other files in the directory having optional extension 'ext'.
104 FileNameList dirList(std::string const & ext) const;
107 /// \return true when file/directory is writable (write test file)
108 /// \warning This methods has different semantics when system level
109 /// copy command, it will overwrite the \c target file if it exists,
110 bool copyTo(FileName const & target) const;
112 /// remove pointed file.
113 /// \return true on success.
114 bool removeFile() const;
116 /// rename pointed file.
117 /// \return false if the operation fails or if the \param target file
119 /// \return true on success.
120 bool renameTo(FileName const & target) const;
122 /// move pointed file to \param target.
123 /// \return true on success.
124 bool moveTo(FileName const & target) const;
126 /// change mode of pointed file.
127 /// This methods does nothing and return true on platforms that does not
129 /// \return true on success.
130 bool changePermission(unsigned long int mode) const;
132 /// remove pointed directory and all contents.
133 /// \return true on success.
134 bool destroyDirectory() const;
135 /// Creates pointed directory.
136 /// \return true on success.
137 bool createDirectory(int permissions) const;
138 /// Creates pointed path.
139 /// \return true on success.
140 bool createPath() const;
142 /// Get the contents of a file as a huge docstring.
143 /// \param encoding defines the encoding of the file contents.
144 /// Only four encodings are supported:
145 /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
146 /// current system locale.
147 docstring fileContents(std::string const & encoding) const;
149 /// Change extension.
151 * If oldname does not have an extension, it is appended.
152 * If the extension is empty, any extension is removed from the name.
154 void changeExtension(std::string const & extension);
156 /** Guess the file format name (as in Format::name()) from contents.
157 Normally you don't want to use this directly, but rather
158 Formats::getFormatFromFile().
160 std::string guessFormatFromContents() const;
162 /// check for zipped file
163 bool isZippedFile() const;
165 static FileName fromFilesystemEncoding(std::string const & name);
166 /// (securely) create a temporary file with the given mask.
167 /// \p mask must be in filesystem encoding, if it contains a
168 /// relative path, the template file will be created in the global
169 /// temporary directory as given by 'package().temp_dir()'.
170 static FileName tempName(std::string const & mask = empty_string());
171 static FileName tempName(FileName const & temp_dir,
172 std::string const & mask);
174 /// get the current working directory
175 static FileName getcwd();
177 static FileName tempPath();
179 /// filename without path
180 std::string onlyFileName() const;
181 /// filename without path and without extension
182 std::string onlyFileNameWithoutExt() const;
183 /// only extension after the last dot.
184 std::string extension() const;
185 /** checks if the file has the given extension
186 on Windows and Mac it compares case insensitive */
187 bool hasExtension(const std::string & ext);
188 /// path without file name
189 FileName onlyPath() const;
190 /// used for display in the Gui
191 docstring displayName(int threshold = 1000) const;
193 /// change to a directory, return success
196 /// \param buffer_path if empty, uses `pwd`
197 docstring const relPath(std::string const & path) const;
199 docstring const absoluteFilePath() const;
202 friend bool equivalent(FileName const &, FileName const &);
209 /// \return true if lhs and rhs represent the same file. E.g.,
210 /// they might be hardlinks of one another.
211 bool equivalent(FileName const & lhs, FileName const & rhs);
212 /// \return true if the absolute path names are the same.
213 bool operator==(FileName const &, FileName const &);
215 bool operator!=(FileName const &, FileName const &);
216 /// Lexically compares the absolute path names.
217 bool operator<(FileName const &, FileName const &);
218 /// Lexically compares the absolute path names.
219 bool operator>(FileName const &, FileName const &);
220 /// Writes the absolute path name to the stream.
221 std::ostream & operator<<(std::ostream &, FileName const &);
225 * Class for storing file names that appear in documents (e. g. child
226 * documents, included figures etc).
227 * The file name must not denote a file in our temporary directory, but a
228 * file that the user chose.
230 class DocFileName : public FileName {
233 /** \param abs_filename the file in question. Must have an absolute path.
234 * \param save_abs_path how is the filename to be output?
236 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
237 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
239 /** \param filename the file in question. May have either a relative
240 * or an absolute path.
241 * \param buffer_path if \c filename has a relative path, generate
242 * the absolute path using this.
244 virtual void set(std::string const & filename, std::string const & buffer_path);
248 bool saveAbsPath() const { return save_abs_path_; }
249 /// \param buffer_path if empty, uses `pwd`
250 std::string relFilename(std::string const & buffer_path = empty_string()) const;
251 /// \param buf_path if empty, uses `pwd`
252 std::string outputFilename(std::string const & buf_path = empty_string()) const;
254 /** @returns a mangled representation of the absolute file name
255 * suitable for use in the temp dir when, for example, converting
256 * an image file to another format.
258 * @param dir the directory that will contain this file with
259 * its mangled name. This information is used by the mangling
260 * algorithm when determining the maximum allowable length of
263 * An example of a mangled name:
264 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
266 * It is guaranteed that
267 * - two different filenames have different mangled names
268 * - two FileName instances with the same filename have identical
271 * Only the mangled file name is returned. It is not prepended
275 mangledFilename(std::string const & dir = empty_string()) const;
277 /// \return true if the file is compressed.
278 bool isZipped() const;
279 /// \return the absolute file name without its .gz, .z, .Z extension
280 std::string unzippedFilename() const;
283 /// Records whether we should save (or export) the filename as a relative
284 /// or absolute path.
286 /// Cache for isZipped() because zippedFile() is expensive
287 mutable bool zipped_;
288 /// Is zipped_ valid?
289 mutable bool zipped_valid_;
293 /// \return true if these have the same absolute path name AND
294 /// if save_abs_path_ has the same value in both cases.
295 bool operator==(DocFileName const &, DocFileName const &);
297 bool operator!=(DocFileName const &, DocFileName const &);
299 } // namespace support