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.
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);
39 virtual ~FileName() {}
40 /** Set a new filename.
41 * \param filename the file in question. Must have an absolute path.
42 * Encoding is always UTF-8.
44 virtual void set(std::string const & filename);
46 /// Is this filename empty?
47 bool empty() const { return name_.empty(); }
48 /// get the absolute file name in UTF-8 encoding
49 std::string const absFilename() const { return name_; }
51 * Get the file name in the encoding used by the file system.
52 * Only use this for accessing the file, e.g. with an fstream.
54 std::string const toFilesystemEncoding() const;
56 /// returns true if the file exists
58 /// returns time of last write access
59 std::time_t lastModified() const;
60 /// return true when file is readable but not writabel
61 bool isReadOnly() const;
62 /// return true when it names a directory
63 bool isDirectory() const;
64 /// return true when file is readable
65 bool isReadable() const;
68 * Get a FileName from \p name in the encoding used by the file system.
69 * Only use this for filenames you got directly from the file system,
70 * e.g. from reading a directory.
71 * \p name must have an absolute path.
73 static FileName const fromFilesystemEncoding(std::string const & name);
75 /// The absolute file name.
76 /// The encoding is currently unspecified, anything else than ASCII
77 /// may or may not work.
82 bool operator==(FileName const &, FileName const &);
83 bool operator!=(FileName const &, FileName const &);
84 bool operator<(FileName const &, FileName const &);
85 bool operator>(FileName const &, FileName const &);
86 std::ostream & operator<<(std::ostream &, FileName const &);
90 * Class for storing file names that appear in documents (e. g. child
91 * documents, included figures etc).
92 * The file name must not denote a file in our temporary directory, but a
93 * file that the user chose.
95 class DocFileName : public FileName {
98 /** \param abs_filename the file in question. Must have an absolute path.
99 * \param save_abs_path how is the file to be output to file?
101 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
102 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
104 /** \param filename the file in question. May have either a relative
105 * or an absolute path.
106 * \param buffer_path if \c filename has a relative path, generate
107 * the absolute path using this.
109 void set(std::string const & filename, std::string const & buffer_path);
113 bool saveAbsPath() const { return save_abs_path_; }
114 /// \param buffer_path if empty, uses `pwd`
115 std::string const relFilename(std::string const & buffer_path = std::string()) const;
116 /// \param buf_path if empty, uses `pwd`
117 std::string const outputFilename(std::string const & buf_path = std::string()) const;
119 /** @returns a mangled representation of the absolute file name
120 * suitable for use in the temp dir when, for example, converting
121 * an image file to another format.
123 * @param dir the directory that will contain this file with
124 * its mangled name. This information is used by the mangling
125 * algorithm when determining the maximum allowable length of
128 * An example of a mangled name:
129 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
131 * It is guaranteed that
132 * - two different filenames have different mangled names
133 * - two FileName instances with the same filename have identical
136 * Only the mangled file name is returned. It is not prepended
140 mangledFilename(std::string const & dir = std::string()) const;
142 /// \return true if the file is compressed.
143 bool isZipped() const;
144 /// \return the absolute file name without its .gz, .z, .Z extension
145 std::string const unzippedFilename() const;
149 /// Cache for isZipped() because zippedFile() is expensive
150 mutable bool zipped_;
151 /// Is zipped_ valid?
152 mutable bool zipped_valid_;
156 bool operator==(DocFileName const &, DocFileName const &);
157 bool operator!=(DocFileName const &, DocFileName const &);
160 } // namespace support