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 &);
47 FileName & operator=(FileName const &);
50 /** Set a new filename.
51 * \param filename the file in question. Must have an absolute path.
52 * Encoding is always UTF-8.
54 virtual void set(std::string const & filename);
56 /// Is this filename empty?
58 /// get the absolute file name in UTF-8 encoding
59 std::string absFilename() const;
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.
64 std::string toFilesystemEncoding() const;
66 /// returns true if the file exists
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 /// \return list other files in the directory having optional extension 'ext'.
89 FileNameList dirList(std::string const & ext) const;
92 /// \return true when file/directory is writable (write test file)
93 /// \param overwrite: set to true if we should erase the \c target
94 /// file if it exists,
95 bool copyTo(FileName const & target, bool overwrite = false) const;
97 /// remove pointed file.
98 /// \retrun true on success.
99 bool removeFile() const;
101 /// remove directory and all contents, returns true on success
102 bool destroyDirectory() const;
103 /// Creates directory. Returns true on success
104 bool createDirectory(int permissions) const;
106 /// Get the contents of a file as a huge docstring.
107 /// \param encoding defines the encoding of the file contents.
108 /// Only four encodings are supported:
109 /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
110 /// current system locale.
111 docstring fileContents(std::string const & encoding) const;
113 /// Change extension.
115 * If oldname does not have an extension, it is appended.
116 * If the extension is empty, any extension is removed from the name.
118 void changeExtension(std::string const & extension);
120 /** Guess the file format name (as in Format::name()) from contents.
121 Normally you don't want to use this directly, but rather
122 Formats::getFormatFromFile().
124 std::string guessFormatFromContents() const;
126 /// check for zipped file
127 bool isZippedFile() const;
129 static FileName fromFilesystemEncoding(std::string const & name);
130 /// (securely) create a temporary file in the given dir with the given mask
131 /// \p mask must be in filesystem encoding
132 static FileName tempName(FileName const & dir = FileName(),
133 std::string const & mask = empty_string());
135 /// filename without path
136 std::string onlyFileName() const;
137 /// path without file name
138 FileName onlyPath() const;
139 /// used for display in the Gui
140 docstring displayName(int threshold = 1000) const;
142 /// change to a directory, return success
145 /// \param buffer_path if empty, uses `pwd`
146 docstring const relPath(std::string const & path) const;
148 docstring const absoluteFilePath() const;
157 bool operator==(FileName const &, FileName const &);
158 bool operator!=(FileName const &, FileName const &);
159 bool operator<(FileName const &, FileName const &);
160 bool operator>(FileName const &, FileName const &);
161 std::ostream & operator<<(std::ostream &, FileName const &);
165 * Class for storing file names that appear in documents (e. g. child
166 * documents, included figures etc).
167 * The file name must not denote a file in our temporary directory, but a
168 * file that the user chose.
170 class DocFileName : public FileName {
173 /** \param abs_filename the file in question. Must have an absolute path.
174 * \param save_abs_path how is the file to be output to file?
176 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
177 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
179 /** \param filename the file in question. May have either a relative
180 * or an absolute path.
181 * \param buffer_path if \c filename has a relative path, generate
182 * the absolute path using this.
184 void set(std::string const & filename, std::string const & buffer_path);
188 bool saveAbsPath() const { return save_abs_path_; }
189 /// \param buffer_path if empty, uses `pwd`
190 std::string relFilename(std::string const & buffer_path = empty_string()) const;
191 /// \param buf_path if empty, uses `pwd`
192 std::string outputFilename(std::string const & buf_path = empty_string()) const;
194 /** @returns a mangled representation of the absolute file name
195 * suitable for use in the temp dir when, for example, converting
196 * an image file to another format.
198 * @param dir the directory that will contain this file with
199 * its mangled name. This information is used by the mangling
200 * algorithm when determining the maximum allowable length of
203 * An example of a mangled name:
204 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
206 * It is guaranteed that
207 * - two different filenames have different mangled names
208 * - two FileName instances with the same filename have identical
211 * Only the mangled file name is returned. It is not prepended
215 mangledFilename(std::string const & dir = empty_string()) const;
217 /// \return true if the file is compressed.
218 bool isZipped() const;
219 /// \return the absolute file name without its .gz, .z, .Z extension
220 std::string unzippedFilename() const;
224 /// Cache for isZipped() because zippedFile() is expensive
225 mutable bool zipped_;
226 /// Is zipped_ valid?
227 mutable bool zipped_valid_;
231 bool operator==(DocFileName const &, DocFileName const &);
232 bool operator!=(DocFileName const &, DocFileName const &);
234 } // namespace support