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.
23 * Class for storing file names.
24 * The file name may be empty. If it is not empty it is an absolute path.
25 * The file may or may not exist.
29 /// Constructor for empty filenames
31 /** Constructor for nonempty filenames.
32 * explicit because we don't want implicit conversion of relative
33 * paths in function arguments (e.g. of unlink).
34 * \param abs_filename the file in question. Must have an absolute path.
35 * Encoding is always UTF-8.
37 explicit FileName(std::string const & abs_filename);
39 /** Set a new filename.
40 * \param filename the file in question. Must have an absolute path.
41 * Encoding is always UTF-8.
43 virtual void set(std::string const & filename);
45 /// Is this filename empty?
46 bool empty() const { return name_.empty(); }
47 /// get the absolute file name in UTF-8 encoding
48 std::string const absFilename() const { return name_; }
50 * Get the file name in the encoding used by the file system.
51 * Only use this for accessing the file, e.g. with an fstream.
53 std::string const toFilesystemEncoding() const;
55 * Get a FileName from \p name in the encoding used by the file system.
56 * Only use this for filenames you got directly from the file system,
57 * e.g. from reading a directory.
58 * \p name must have an absolute path.
60 static FileName const fromFilesystemEncoding(std::string const & name);
62 /// The absolute file name.
63 /// The encoding is currently unspecified, anything else than ASCII
64 /// may or may not work.
69 bool operator==(FileName const &, FileName const &);
70 bool operator!=(FileName const &, FileName const &);
71 bool operator<(FileName const &, FileName const &);
72 bool operator>(FileName const &, FileName const &);
73 std::ostream & operator<<(std::ostream &, FileName const &);
77 * Class for storing file names that appear in documents (e. g. child
78 * documents, included figures etc).
79 * The file name must not denote a file in our temporary directory, but a
80 * file that the user chose.
82 class DocFileName : public FileName {
85 /** \param abs_filename the file in question. Must have an absolute path.
86 * \param save_abs_path how is the file to be output to file?
88 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
89 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
91 /** \param filename the file in question. May have either a relative
92 * or an absolute path.
93 * \param buffer_path if \c filename has a relative path, generate
94 * the absolute path using this.
96 void set(std::string const & filename, std::string const & buffer_path);
100 bool saveAbsPath() const { return save_abs_path_; }
101 /// \param buffer_path if empty, uses `pwd`
102 std::string const relFilename(std::string const & buffer_path = std::string()) const;
103 /// \param buf_path if empty, uses `pwd`
104 std::string const outputFilename(std::string const & buf_path = std::string()) const;
106 /** @returns a mangled representation of the absolute file name
107 * suitable for use in the temp dir when, for example, converting
108 * an image file to another format.
110 * @param dir the directory that will contain this file with
111 * its mangled name. This information is used by the mangling
112 * algorithm when determining the maximum allowable length of
115 * An example of a mangled name:
116 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
118 * It is guaranteed that
119 * - two different filenames have different mangled names
120 * - two FileName instances with the same filename have identical
123 * Only the mangled file name is returned. It is not prepended
127 mangledFilename(std::string const & dir = std::string()) const;
129 /// \return true if the file is compressed.
130 bool isZipped() const;
131 /// \return the absolute file name without its .gz, .z, .Z extension
132 std::string const unzippedFilename() const;
136 /// Cache for isZipped() because zippedFile() is expensive
137 mutable bool zipped_;
138 /// Is zipped_ valid?
139 mutable bool zipped_valid_;
143 bool operator==(DocFileName const &, DocFileName const &);
144 bool operator!=(DocFileName const &, DocFileName const &);
147 } // namespace support