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.
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 &);
49 virtual ~FileName() {}
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 /// 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 /// \return list files in a directory having optional extension ext..
103 std::vector<FileName> dirList(
104 std::string const & ext = std::string());
106 /// Get the contents of a file as a huge std::string
107 std::string fileContents() const;
109 * Get a FileName from \p name in the encoding used by the file system.
110 * Only use this for filenames you got directly from the file system,
111 * e.g. from reading a directory.
112 * \p name must have an absolute path.
115 /** Guess the file format name (as in Format::name()) from contents.
116 Normally you don't want to use this directly, but rather
117 Formats::getFormatFromFile().
119 std::string guessFormatFromContents() const;
121 /// check for zipped file
122 bool isZippedFile() const;
124 static FileName fromFilesystemEncoding(std::string const & name);
125 /// (securely) create a temporary file in the given dir with the given mask
126 /// \p mask must be in filesystem encoding
127 static FileName tempName(FileName const & dir = FileName(),
128 std::string const & mask = std::string());
130 /// filename without path
131 std::string onlyFileName() const;
132 /// path without file name
133 FileName onlyPath() const;
134 /// used for display in the Gui
135 docstring displayName(int threshold = 1000) const;
138 friend class DocFileName;
145 bool operator==(FileName const &, FileName const &);
146 bool operator!=(FileName const &, FileName const &);
147 bool operator<(FileName const &, FileName const &);
148 bool operator>(FileName const &, FileName const &);
149 std::ostream & operator<<(std::ostream &, FileName const &);
153 * Class for storing file names that appear in documents (e. g. child
154 * documents, included figures etc).
155 * The file name must not denote a file in our temporary directory, but a
156 * file that the user chose.
158 class DocFileName : public FileName {
161 /** \param abs_filename the file in question. Must have an absolute path.
162 * \param save_abs_path how is the file to be output to file?
164 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
165 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
167 /** \param filename the file in question. May have either a relative
168 * or an absolute path.
169 * \param buffer_path if \c filename has a relative path, generate
170 * the absolute path using this.
172 void set(std::string const & filename, std::string const & buffer_path);
176 bool saveAbsPath() const { return save_abs_path_; }
177 /// \param buffer_path if empty, uses `pwd`
178 std::string const relFilename(std::string const & buffer_path = std::string()) const;
179 /// \param buf_path if empty, uses `pwd`
180 std::string const outputFilename(std::string const & buf_path = std::string()) const;
182 /** @returns a mangled representation of the absolute file name
183 * suitable for use in the temp dir when, for example, converting
184 * an image file to another format.
186 * @param dir the directory that will contain this file with
187 * its mangled name. This information is used by the mangling
188 * algorithm when determining the maximum allowable length of
191 * An example of a mangled name:
192 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
194 * It is guaranteed that
195 * - two different filenames have different mangled names
196 * - two FileName instances with the same filename have identical
199 * Only the mangled file name is returned. It is not prepended
203 mangledFilename(std::string const & dir = std::string()) const;
205 /// \return true if the file is compressed.
206 bool isZipped() const;
207 /// \return the absolute file name without its .gz, .z, .Z extension
208 std::string const unzippedFilename() const;
212 /// Cache for isZipped() because zippedFile() is expensive
213 mutable bool zipped_;
214 /// Is zipped_ valid?
215 mutable bool zipped_valid_;
219 bool operator==(DocFileName const &, DocFileName const &);
220 bool operator!=(DocFileName const &, DocFileName const &);
223 } // namespace support