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