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);
59 /// Is this filename empty?
61 /// Is the filename absolute?
62 bool isAbsolute() const;
64 /// get the absolute file name in UTF-8 encoding
65 std::string absFilename() const;
67 * Get the file name in the encoding used by the file system.
68 * Only use this for accessing the file, e.g. with an fstream.
70 std::string toFilesystemEncoding() const;
72 /// returns true if the file exists
74 /// \return true if this object points to a symbolic link.
75 bool isSymLink() const;
76 /// \return true if the file is empty.
77 bool isFileEmpty() const;
78 /// returns time of last write access
79 std::time_t lastModified() const;
80 /// generates a checksum of a file
81 virtual unsigned long checksum() const;
82 /// return true when file is readable but not writabel
83 bool isReadOnly() const;
84 /// return true when it names a directory
85 bool isDirectory() const;
86 /// return true when directory is readable
87 bool isReadableDirectory() const;
88 /// return true when it is a file and readable
89 virtual bool isReadableFile() const;
90 /// return true when file/directory is writable
91 bool isWritable() const;
92 /// return true when file/directory is writable (write test file)
93 bool isDirWritable() const;
94 /// \return list other files in the directory having optional extension 'ext'.
95 FileNameList dirList(std::string const & ext) const;
98 /// \return true when file/directory is writable (write test file)
99 /// \warning This methods has different semantics when system level
100 /// copy command, it will overwrite the \c target file if it exists,
101 bool copyTo(FileName const & target) const;
103 /// remove pointed file.
104 /// \return true on success.
105 bool removeFile() const;
107 /// rename pointed file.
108 /// \return false if the operation fails or if the \param target file
110 /// \return true on success.
111 bool renameTo(FileName const & target) const;
113 /// move pointed file to \param target.
114 /// \return true on success.
115 bool moveTo(FileName const & target) const;
117 /// change mode of pointed file.
118 /// This methods does nothing and return true on platforms that does not
120 /// \return true on success.
121 bool changePermission(unsigned long int mode) const;
123 /// remove pointed directory and all contents.
124 /// \return true on success.
125 bool destroyDirectory() const;
126 /// Creates pointed directory.
127 /// \return true on success.
128 bool createDirectory(int permissions) const;
129 /// Creates pointed path.
130 /// \return true on success.
131 bool createPath() const;
133 /// Get the contents of a file as a huge docstring.
134 /// \param encoding defines the encoding of the file contents.
135 /// Only four encodings are supported:
136 /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
137 /// current system locale.
138 docstring fileContents(std::string const & encoding) const;
140 /// Change extension.
142 * If oldname does not have an extension, it is appended.
143 * If the extension is empty, any extension is removed from the name.
145 void changeExtension(std::string const & extension);
147 /** Guess the file format name (as in Format::name()) from contents.
148 Normally you don't want to use this directly, but rather
149 Formats::getFormatFromFile().
151 std::string guessFormatFromContents() const;
153 /// check for zipped file
154 bool isZippedFile() const;
156 static FileName fromFilesystemEncoding(std::string const & name);
157 /// (securely) create a temporary file with the given mask.
158 /// \p mask must be in filesystem encoding, if it contains a
159 /// relative path, the template file will be created in the global
160 /// temporary directory as given by 'package().temp_dir()'.
161 static FileName tempName(std::string const & mask = empty_string());
162 static FileName tempName(FileName const & temp_dir,
163 std::string const & mask);
165 /// get the current working directory
166 static FileName getcwd();
168 static FileName tempPath();
170 /// filename without path
171 std::string onlyFileName() const;
172 /// filename without path and without extension
173 std::string onlyFileNameWithoutExt() const;
174 /// path without file name
175 FileName onlyPath() const;
176 /// used for display in the Gui
177 docstring displayName(int threshold = 1000) const;
179 /// change to a directory, return success
182 /// \param buffer_path if empty, uses `pwd`
183 docstring const relPath(std::string const & path) const;
185 docstring const absoluteFilePath() const;
194 bool operator==(FileName const &, FileName const &);
195 bool operator!=(FileName const &, FileName const &);
196 bool operator<(FileName const &, FileName const &);
197 bool operator>(FileName const &, FileName const &);
198 std::ostream & operator<<(std::ostream &, FileName const &);
202 * Class for storing file names that appear in documents (e. g. child
203 * documents, included figures etc).
204 * The file name must not denote a file in our temporary directory, but a
205 * file that the user chose.
207 class DocFileName : public FileName {
210 /** \param abs_filename the file in question. Must have an absolute path.
211 * \param save_abs_path how is the file to be output to file?
213 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
214 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
216 /** \param filename the file in question. May have either a relative
217 * or an absolute path.
218 * \param buffer_path if \c filename has a relative path, generate
219 * the absolute path using this.
221 virtual void set(std::string const & filename, std::string const & buffer_path);
225 bool saveAbsPath() const { return save_abs_path_; }
226 /// \param buffer_path if empty, uses `pwd`
227 std::string relFilename(std::string const & buffer_path = empty_string()) const;
228 /// \param buf_path if empty, uses `pwd`
229 std::string outputFilename(std::string const & buf_path = empty_string()) const;
231 /** @returns a mangled representation of the absolute file name
232 * suitable for use in the temp dir when, for example, converting
233 * an image file to another format.
235 * @param dir the directory that will contain this file with
236 * its mangled name. This information is used by the mangling
237 * algorithm when determining the maximum allowable length of
240 * An example of a mangled name:
241 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
243 * It is guaranteed that
244 * - two different filenames have different mangled names
245 * - two FileName instances with the same filename have identical
248 * Only the mangled file name is returned. It is not prepended
252 mangledFilename(std::string const & dir = empty_string()) const;
254 /// \return true if the file is compressed.
255 bool isZipped() const;
256 /// \return the absolute file name without its .gz, .z, .Z extension
257 std::string unzippedFilename() const;
261 /// Cache for isZipped() because zippedFile() is expensive
262 mutable bool zipped_;
263 /// Is zipped_ valid?
264 mutable bool zipped_valid_;
268 bool operator==(DocFileName const &, DocFileName const &);
269 bool operator!=(DocFileName const &, DocFileName const &);
271 } // namespace support