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"
25 * Class for storing file names.
26 * The file name may be empty. If it is not empty it is an absolute path.
27 * The file may or may not exist.
31 /// Constructor for empty filenames
33 /** Constructor for nonempty filenames.
34 * explicit because we don't want implicit conversion of relative
35 * paths in function arguments (e.g. of unlink).
36 * \param abs_filename the file in question. Must have an absolute path.
37 * Encoding is always UTF-8.
39 explicit FileName(std::string const & abs_filename);
42 FileName(FileName const &);
45 FileName & operator=(FileName const &);
48 /** Set a new filename.
49 * \param filename the file in question. Must have an absolute path.
50 * Encoding is always UTF-8.
52 virtual void set(std::string const & filename);
54 /// Is this filename empty?
56 /// get the absolute file name in UTF-8 encoding
57 std::string absFilename() const;
59 * Get the file name in the encoding used by the file system.
60 * Only use this for accessing the file, e.g. with an fstream.
62 std::string toFilesystemEncoding() const;
64 /// returns true if the file exists
66 /// \return true if this object points to a symbolic link.
67 bool isSymLink() const;
68 /// \return true if the file is empty.
69 bool isFileEmpty() const;
70 /// returns time of last write access
71 std::time_t lastModified() const;
72 /// generates a checksum of a file
73 unsigned long checksum() const;
74 /// return true when file is readable but not writabel
75 bool isReadOnly() const;
76 /// return true when it names a directory
77 bool isDirectory() const;
78 /// return true when file/directory is readable
79 bool isReadableDirectory() const;
80 /// return true when it is a file and readable
81 bool isReadableFile() const;
82 /// return true when file/directory is writable
83 bool isWritable() const;
84 /// return true when file/directory is writable (write test file)
85 bool isDirWritable() const;
88 /// \return true when file/directory is writable (write test file)
89 /// \param overwrite: set to true if we should erase the \c target
90 /// file if it exists,
91 bool copyTo(FileName const & target, bool overwrite = false) const;
93 /// remove pointed file.
94 /// \retrun true on success.
95 bool removeFile() const;
97 /// remove directory and all contents, returns true on success
98 bool destroyDirectory() const;
99 /// Creates directory. Returns true on success
100 bool createDirectory(int permissions) const;
102 /// Get the contents of a file as a huge std::string
103 std::string fileContents() const;
105 * Get a FileName from \p name in the encoding used by the file system.
106 * Only use this for filenames you got directly from the file system,
107 * e.g. from reading a directory.
108 * \p name must have an absolute path.
111 /// Change extension.
113 * If oldname does not have an extension, it is appended.
114 * If the extension is empty, any extension is removed from the name.
116 void changeExtension(std::string const & extension);
118 /** Guess the file format name (as in Format::name()) from contents.
119 Normally you don't want to use this directly, but rather
120 Formats::getFormatFromFile().
122 std::string guessFormatFromContents() const;
124 /// check for zipped file
125 bool isZippedFile() const;
127 static FileName fromFilesystemEncoding(std::string const & name);
128 /// (securely) create a temporary file in the given dir with the given mask
129 /// \p mask must be in filesystem encoding
130 static FileName tempName(FileName const & dir = FileName(),
131 std::string const & mask = empty_string());
133 /// filename without path
134 std::string onlyFileName() const;
135 /// path without file name
136 FileName onlyPath() const;
137 /// used for display in the Gui
138 docstring displayName(int threshold = 1000) const;
140 /// change to a directory, return success
143 /// \return list other files in the directory having optional extension 'ext'.
144 std::vector<FileName> dirList(std::string const & ext = empty_string());
146 /// \param buffer_path if empty, uses `pwd`
147 docstring const relPath(std::string const & path) const;
156 bool operator==(FileName const &, FileName const &);
157 bool operator!=(FileName const &, FileName const &);
158 bool operator<(FileName const &, FileName const &);
159 bool operator>(FileName const &, FileName const &);
160 std::ostream & operator<<(std::ostream &, FileName const &);
164 * Class for storing file names that appear in documents (e. g. child
165 * documents, included figures etc).
166 * The file name must not denote a file in our temporary directory, but a
167 * file that the user chose.
169 class DocFileName : public FileName {
172 /** \param abs_filename the file in question. Must have an absolute path.
173 * \param save_abs_path how is the file to be output to file?
175 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
176 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
178 /** \param filename the file in question. May have either a relative
179 * or an absolute path.
180 * \param buffer_path if \c filename has a relative path, generate
181 * the absolute path using this.
183 void set(std::string const & filename, std::string const & buffer_path);
187 bool saveAbsPath() const { return save_abs_path_; }
188 /// \param buffer_path if empty, uses `pwd`
189 std::string relFilename(std::string const & buffer_path = empty_string()) const;
190 /// \param buf_path if empty, uses `pwd`
191 std::string outputFilename(std::string const & buf_path = empty_string()) const;
193 /** @returns a mangled representation of the absolute file name
194 * suitable for use in the temp dir when, for example, converting
195 * an image file to another format.
197 * @param dir the directory that will contain this file with
198 * its mangled name. This information is used by the mangling
199 * algorithm when determining the maximum allowable length of
202 * An example of a mangled name:
203 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
205 * It is guaranteed that
206 * - two different filenames have different mangled names
207 * - two FileName instances with the same filename have identical
210 * Only the mangled file name is returned. It is not prepended
214 mangledFilename(std::string const & dir = empty_string()) const;
216 /// \return true if the file is compressed.
217 bool isZipped() const;
218 /// \return the absolute file name without its .gz, .z, .Z extension
219 std::string unzippedFilename() const;
223 /// Cache for isZipped() because zippedFile() is expensive
224 mutable bool zipped_;
225 /// Is zipped_ valid?
226 mutable bool zipped_valid_;
230 bool operator==(DocFileName const &, DocFileName const &);
231 bool operator!=(DocFileName const &, DocFileName const &);
234 } // namespace support