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"
24 * Class for storing file names.
25 * The file name may be empty. If it is not empty it is an absolute path.
26 * The file may or may not exist.
30 /// Constructor for empty filenames
32 /** Constructor for nonempty filenames.
33 * explicit because we don't want implicit conversion of relative
34 * paths in function arguments (e.g. of unlink).
35 * \param abs_filename the file in question. Must have an absolute path.
36 * Encoding is always UTF-8.
38 explicit FileName(std::string const & abs_filename);
41 FileName(FileName const &);
44 FileName & operator=(FileName const &);
47 /** Set a new filename.
48 * \param filename the file in question. Must have an absolute path.
49 * Encoding is always UTF-8.
51 virtual void set(std::string const & filename);
53 /// Is this filename empty?
55 /// get the absolute file name in UTF-8 encoding
56 std::string absFilename() const;
58 * Get the file name in the encoding used by the file system.
59 * Only use this for accessing the file, e.g. with an fstream.
61 std::string toFilesystemEncoding() const;
63 /// returns true if the file exists
65 /// \return true if this object points to a symbolic link.
66 bool isSymLink() const;
67 /// \return true if the file is empty.
68 bool isFileEmpty() const;
69 /// returns time of last write access
70 std::time_t lastModified() const;
71 /// generates a checksum of a file
72 unsigned long checksum() const;
73 /// return true when file is readable but not writabel
74 bool isReadOnly() const;
75 /// return true when it names a directory
76 bool isDirectory() const;
77 /// return true when file/directory is readable
78 bool isReadableDirectory() const;
79 /// return true when it is a file and readable
80 bool isReadableFile() const;
81 /// return true when file/directory is writable
82 bool isWritable() const;
83 /// return true when file/directory is writable (write test file)
84 bool isDirWritable() const;
87 /// \return true when file/directory is writable (write test file)
88 /// \param overwrite: set to true if we should erase the \c target
89 /// file if it exists,
90 bool copyTo(FileName const & target, bool overwrite = false) const;
92 /// remove pointed file.
93 /// \retrun true on success.
94 bool removeFile() const;
96 /// remove directory and all contents, returns true on success
97 bool destroyDirectory() const;
98 /// Creates directory. Returns true on success
99 bool createDirectory(int permissions) const;
101 /// Get the contents of a file as a huge std::string
102 std::string fileContents() const;
104 * Get a FileName from \p name in the encoding used by the file system.
105 * Only use this for filenames you got directly from the file system,
106 * e.g. from reading a directory.
107 * \p name must have an absolute path.
110 /// Change extension.
112 * If oldname does not have an extension, it is appended.
113 * If the extension is empty, any extension is removed from the name.
115 void changeExtension(std::string const & extension);
117 /** Guess the file format name (as in Format::name()) from contents.
118 Normally you don't want to use this directly, but rather
119 Formats::getFormatFromFile().
121 std::string guessFormatFromContents() const;
123 /// check for zipped file
124 bool isZippedFile() const;
126 static FileName fromFilesystemEncoding(std::string const & name);
127 /// (securely) create a temporary file in the given dir with the given mask
128 /// \p mask must be in filesystem encoding
129 static FileName tempName(FileName const & dir = FileName(),
130 std::string const & mask = empty_string());
132 /// filename without path
133 std::string onlyFileName() const;
134 /// path without file name
135 FileName onlyPath() const;
136 /// used for display in the Gui
137 docstring displayName(int threshold = 1000) const;
139 /// change to a directory, return success
142 /// \param buffer_path if empty, uses `pwd`
143 docstring const relPath(std::string const & path) const;
145 docstring const absoluteFilePath() const;
154 bool operator==(FileName const &, FileName const &);
155 bool operator!=(FileName const &, FileName const &);
156 bool operator<(FileName const &, FileName const &);
157 bool operator>(FileName const &, FileName const &);
158 std::ostream & operator<<(std::ostream &, FileName const &);
162 * Class for storing file names that appear in documents (e. g. child
163 * documents, included figures etc).
164 * The file name must not denote a file in our temporary directory, but a
165 * file that the user chose.
167 class DocFileName : public FileName {
170 /** \param abs_filename the file in question. Must have an absolute path.
171 * \param save_abs_path how is the file to be output to file?
173 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
174 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
176 /** \param filename the file in question. May have either a relative
177 * or an absolute path.
178 * \param buffer_path if \c filename has a relative path, generate
179 * the absolute path using this.
181 void set(std::string const & filename, std::string const & buffer_path);
185 bool saveAbsPath() const { return save_abs_path_; }
186 /// \param buffer_path if empty, uses `pwd`
187 std::string relFilename(std::string const & buffer_path = empty_string()) const;
188 /// \param buf_path if empty, uses `pwd`
189 std::string outputFilename(std::string const & buf_path = empty_string()) const;
191 /** @returns a mangled representation of the absolute file name
192 * suitable for use in the temp dir when, for example, converting
193 * an image file to another format.
195 * @param dir the directory that will contain this file with
196 * its mangled name. This information is used by the mangling
197 * algorithm when determining the maximum allowable length of
200 * An example of a mangled name:
201 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
203 * It is guaranteed that
204 * - two different filenames have different mangled names
205 * - two FileName instances with the same filename have identical
208 * Only the mangled file name is returned. It is not prepended
212 mangledFilename(std::string const & dir = empty_string()) const;
214 /// \return true if the file is compressed.
215 bool isZipped() const;
216 /// \return the absolute file name without its .gz, .z, .Z extension
217 std::string unzippedFilename() const;
221 /// Cache for isZipped() because zippedFile() is expensive
222 mutable bool zipped_;
223 /// Is zipped_ valid?
224 mutable bool zipped_valid_;
228 bool operator==(DocFileName const &, DocFileName const &);
229 bool operator!=(DocFileName const &, DocFileName const &);
232 } // namespace support