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 /// \warning This methods has different semantics when system level
94 /// copy command, it will overwrite the \c target file if it exists,
95 bool copyTo(FileName const & target) const;
97 /// remove pointed file.
98 /// \return true on success.
99 bool removeFile() const;
101 /// rename pointed file.
102 /// \return false if the operation fails or if the \param target file
104 /// \return true on success.
105 bool renameTo(FileName const & target) const;
107 /// move pointed file to \param target.
108 /// \return true on success.
109 bool moveTo(FileName const & target) const;
111 /// change mode of pointed file.
112 /// This methods does nothing and return true on platforms that does not
114 /// \return true on success.
115 bool changePermission(unsigned long int mode) const;
117 /// remove pointed directory and all contents.
118 /// \return true on success.
119 bool destroyDirectory() const;
120 /// Creates pointed directory.
121 /// \return true on success.
122 bool createDirectory(int permissions) const;
123 /// Creates pointed path.
124 /// \return true on success.
125 bool createPath() const;
127 /// Get the contents of a file as a huge docstring.
128 /// \param encoding defines the encoding of the file contents.
129 /// Only four encodings are supported:
130 /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
131 /// current system locale.
132 docstring fileContents(std::string const & encoding) const;
134 /// Change extension.
136 * If oldname does not have an extension, it is appended.
137 * If the extension is empty, any extension is removed from the name.
139 void changeExtension(std::string const & extension);
141 /** Guess the file format name (as in Format::name()) from contents.
142 Normally you don't want to use this directly, but rather
143 Formats::getFormatFromFile().
145 std::string guessFormatFromContents() const;
147 /// check for zipped file
148 bool isZippedFile() const;
150 static FileName fromFilesystemEncoding(std::string const & name);
151 /// (securely) create a temporary file with the given mask.
152 /// \p mask must be in filesystem encoding, if it contains a
153 /// relative path, the template file will be created in the global
154 /// temporary directory as given by 'package().temp_dir()'.
155 static FileName tempName(std::string const & mask = empty_string());
157 /// get the current working directory
158 static FileName getcwd();
160 /// filename without path
161 std::string onlyFileName() const;
162 /// path without file name
163 FileName onlyPath() const;
164 /// used for display in the Gui
165 docstring displayName(int threshold = 1000) const;
167 /// change to a directory, return success
170 /// \param buffer_path if empty, uses `pwd`
171 docstring const relPath(std::string const & path) const;
173 docstring const absoluteFilePath() const;
182 bool operator==(FileName const &, FileName const &);
183 bool operator!=(FileName const &, FileName const &);
184 bool operator<(FileName const &, FileName const &);
185 bool operator>(FileName const &, FileName const &);
186 std::ostream & operator<<(std::ostream &, FileName const &);
190 * Class for storing file names that appear in documents (e. g. child
191 * documents, included figures etc).
192 * The file name must not denote a file in our temporary directory, but a
193 * file that the user chose.
195 class DocFileName : public FileName {
198 /** \param abs_filename the file in question. Must have an absolute path.
199 * \param save_abs_path how is the file to be output to file?
201 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
202 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
204 /** \param filename the file in question. May have either a relative
205 * or an absolute path.
206 * \param buffer_path if \c filename has a relative path, generate
207 * the absolute path using this.
209 void set(std::string const & filename, std::string const & buffer_path);
213 bool saveAbsPath() const { return save_abs_path_; }
214 /// \param buffer_path if empty, uses `pwd`
215 std::string relFilename(std::string const & buffer_path = empty_string()) const;
216 /// \param buf_path if empty, uses `pwd`
217 std::string outputFilename(std::string const & buf_path = empty_string()) const;
219 /** @returns a mangled representation of the absolute file name
220 * suitable for use in the temp dir when, for example, converting
221 * an image file to another format.
223 * @param dir the directory that will contain this file with
224 * its mangled name. This information is used by the mangling
225 * algorithm when determining the maximum allowable length of
228 * An example of a mangled name:
229 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
231 * It is guaranteed that
232 * - two different filenames have different mangled names
233 * - two FileName instances with the same filename have identical
236 * Only the mangled file name is returned. It is not prepended
240 mangledFilename(std::string const & dir = empty_string()) const;
242 /// \return true if the file is compressed.
243 bool isZipped() const;
244 /// \return the absolute file name without its .gz, .z, .Z extension
245 std::string unzippedFilename() const;
249 /// Cache for isZipped() because zippedFile() is expensive
250 mutable bool zipped_;
251 /// Is zipped_ valid?
252 mutable bool zipped_valid_;
256 bool operator==(DocFileName const &, DocFileName const &);
257 bool operator!=(DocFileName const &, DocFileName const &);
259 } // namespace support