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 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.
79 std::string toFilesystemEncoding() const;
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.
91 std::string toSafeFilesystemEncoding() const;
93 /// returns true if the file exists
95 /// refreshes the file info
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;
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;
126 /// remove pointed file.
127 /// \return true on success.
128 bool removeFile() const;
130 /// rename pointed file.
131 /// \return false if the operation fails or if the \param target file
133 /// \return true on success.
134 bool renameTo(FileName const & target) const;
136 /// move pointed file to \param target.
137 /// \return true on success.
138 bool moveTo(FileName const & target) const;
140 /// change mode of pointed file.
141 /// This methods does nothing and return true on platforms that does not
143 /// \return true on success.
144 bool changePermission(unsigned long int mode) const;
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;
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;
163 /// Change extension.
165 * If oldname does not have an extension, it is appended.
166 * If the extension is empty, any extension is removed from the name.
168 void changeExtension(std::string const & extension);
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().
174 std::string guessFormatFromContents() const;
176 /// check for zipped file
177 bool isZippedFile() const;
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);
188 /// get the current working directory
189 static FileName getcwd();
191 static FileName tempPath();
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;
207 /// change to a directory, return success
210 /// \param buffer_path if empty, uses `pwd`
211 docstring const relPath(std::string const & path) const;
213 docstring const absoluteFilePath() const;
216 friend bool equivalent(FileName const &, FileName const &);
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 &);
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 &);
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.
244 class DocFileName : public FileName {
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?
250 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
251 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
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.
258 virtual void set(std::string const & filename, std::string const & buffer_path);
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;
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.
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
277 * An example of a mangled name:
278 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
280 * It is guaranteed that
281 * - two different filenames have different mangled names
282 * - two FileName instances with the same filename have identical
285 * Only the mangled file name is returned. It is not prepended
289 mangledFilename(std::string const & dir = empty_string()) const;
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;
297 /// Records whether we should save (or export) the filename as a relative
298 /// or absolute path.
300 /// Cache for isZipped() because zippedFile() is expensive
301 mutable bool zipped_;
302 /// Is zipped_ valid?
303 mutable bool zipped_valid_;
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 &);
311 bool operator!=(DocFileName const &, DocFileName const &);
313 } // namespace support