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.
26 * Class for storing file names.
27 * The file name may be empty. If it is not empty it is an absolute path.
28 * The file may or may not exist.
32 /// Constructor for empty filenames
34 /** Constructor for nonempty filenames.
35 * explicit because we don't want implicit conversion of relative
36 * paths in function arguments (e.g. of unlink).
37 * \param abs_filename the file in question. Must have an absolute path.
38 * Encoding is always UTF-8.
40 explicit FileName(std::string const & abs_filename);
41 virtual ~FileName() {}
42 /** Set a new filename.
43 * \param filename the file in question. Must have an absolute path.
44 * Encoding is always UTF-8.
46 virtual void set(std::string const & filename);
48 /// Is this filename empty?
49 bool empty() const { return name_.empty(); }
50 /// get the absolute file name in UTF-8 encoding
51 std::string const absFilename() const { return name_; }
53 * Get the file name in the encoding used by the file system.
54 * Only use this for accessing the file, e.g. with an fstream.
56 std::string toFilesystemEncoding() const;
58 /// returns true if the file exists
60 /// returns time of last write access
61 std::time_t lastModified() const;
62 /// return true when file is readable but not writabel
63 bool isReadOnly() const;
64 /// return true when it names a directory
65 bool isDirectory() const;
66 /// return true when file/directory is readable
67 bool isReadable() const;
68 /// return true when it is a file and readable
69 bool isFileReadable() const;
70 /// return true when file/directory is writable
71 bool isWritable() const;
72 /// return true when file/directory is writable (write test file)
73 bool isDirWritable() const;
75 /// remove directory and all contents, returns true on success
76 bool destroyDirectory() const;
77 /// Creates directory. Returns true on success
78 bool createDirectory(int permissions) const;
80 /// Get the contents of a file as a huge std::string
81 std::string fileContents() const;
83 * Get a FileName from \p name in the encoding used by the file system.
84 * Only use this for filenames you got directly from the file system,
85 * e.g. from reading a directory.
86 * \p name must have an absolute path.
89 /** Guess the file format name (as in Format::name()) from contents.
90 Normally you don't want to use this directly, but rather
91 Formats::getFormatFromFile().
93 std::string guessFormatFromContents() const;
95 /// check for zipped file
96 bool isZippedFile() const;
98 static FileName fromFilesystemEncoding(std::string const & name);
99 /// (securely) create a temporary file in the given dir with the given mask
100 /// \p mask must be in filesystem encoding
101 static FileName tempName(FileName const & dir = FileName(),
102 std::string const & mask = std::string());
104 /// filename without path
105 std::string onlyFileName() const;
106 /// path without file name
107 std::string onlyPath() const;
108 /// used for display in the Gui
109 docstring displayName(int threshold = 1000) const;
113 /// The absolute file name.
114 /// The encoding is currently unspecified, anything else than ASCII
115 /// may or may not work.
120 bool operator==(FileName const &, FileName const &);
121 bool operator!=(FileName const &, FileName const &);
122 bool operator<(FileName const &, FileName const &);
123 bool operator>(FileName const &, FileName const &);
124 std::ostream & operator<<(std::ostream &, FileName const &);
128 * Class for storing file names that appear in documents (e. g. child
129 * documents, included figures etc).
130 * The file name must not denote a file in our temporary directory, but a
131 * file that the user chose.
133 class DocFileName : public FileName {
136 /** \param abs_filename the file in question. Must have an absolute path.
137 * \param save_abs_path how is the file to be output to file?
139 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
140 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
142 /** \param filename the file in question. May have either a relative
143 * or an absolute path.
144 * \param buffer_path if \c filename has a relative path, generate
145 * the absolute path using this.
147 void set(std::string const & filename, std::string const & buffer_path);
151 bool saveAbsPath() const { return save_abs_path_; }
152 /// \param buffer_path if empty, uses `pwd`
153 std::string const relFilename(std::string const & buffer_path = std::string()) const;
154 /// \param buf_path if empty, uses `pwd`
155 std::string const outputFilename(std::string const & buf_path = std::string()) const;
157 /** @returns a mangled representation of the absolute file name
158 * suitable for use in the temp dir when, for example, converting
159 * an image file to another format.
161 * @param dir the directory that will contain this file with
162 * its mangled name. This information is used by the mangling
163 * algorithm when determining the maximum allowable length of
166 * An example of a mangled name:
167 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
169 * It is guaranteed that
170 * - two different filenames have different mangled names
171 * - two FileName instances with the same filename have identical
174 * Only the mangled file name is returned. It is not prepended
178 mangledFilename(std::string const & dir = std::string()) const;
180 /// \return true if the file is compressed.
181 bool isZipped() const;
182 /// \return the absolute file name without its .gz, .z, .Z extension
183 std::string const unzippedFilename() const;
187 /// Cache for isZipped() because zippedFile() is expensive
188 mutable bool zipped_;
189 /// Is zipped_ valid?
190 mutable bool zipped_valid_;
194 bool operator==(DocFileName const &, DocFileName const &);
195 bool operator!=(DocFileName const &, DocFileName const &);
198 } // namespace support