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);
43 FileName(FileName const &);
46 FileName & operator=(FileName const &);
48 virtual ~FileName() {}
49 /** Set a new filename.
50 * \param filename the file in question. Must have an absolute path.
51 * Encoding is always UTF-8.
53 virtual void set(std::string const & filename);
55 /// Is this filename empty?
57 /// get the absolute file name in UTF-8 encoding
58 std::string absFilename() const;
60 * Get the file name in the encoding used by the file system.
61 * Only use this for accessing the file, e.g. with an fstream.
63 std::string toFilesystemEncoding() const;
65 /// returns true if the file exists
67 /// \return true if this object points to a symbolic link.
68 bool isSymLink() const;
69 /// \return true if the file is empty.
70 bool isFileEmpty() const;
71 /// returns time of last write access
72 std::time_t lastModified() 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;
86 /// return true when file/directory is writable (write test file)
87 bool copyTo(FileName const & target) const;
89 /// remove directory and all contents, returns true on success
90 bool destroyDirectory() const;
91 /// Creates directory. Returns true on success
92 bool createDirectory(int permissions) const;
94 /// Get the contents of a file as a huge std::string
95 std::string fileContents() const;
97 * Get a FileName from \p name in the encoding used by the file system.
98 * Only use this for filenames you got directly from the file system,
99 * e.g. from reading a directory.
100 * \p name must have an absolute path.
103 /** Guess the file format name (as in Format::name()) from contents.
104 Normally you don't want to use this directly, but rather
105 Formats::getFormatFromFile().
107 std::string guessFormatFromContents() const;
109 /// check for zipped file
110 bool isZippedFile() const;
112 static FileName fromFilesystemEncoding(std::string const & name);
113 /// (securely) create a temporary file in the given dir with the given mask
114 /// \p mask must be in filesystem encoding
115 static FileName tempName(FileName const & dir = FileName(),
116 std::string const & mask = std::string());
118 /// filename without path
119 std::string onlyFileName() const;
120 /// path without file name
121 FileName onlyPath() const;
122 /// used for display in the Gui
123 docstring displayName(int threshold = 1000) const;
126 friend class DocFileName;
133 bool operator==(FileName const &, FileName const &);
134 bool operator!=(FileName const &, FileName const &);
135 bool operator<(FileName const &, FileName const &);
136 bool operator>(FileName const &, FileName const &);
137 std::ostream & operator<<(std::ostream &, FileName const &);
141 * Class for storing file names that appear in documents (e. g. child
142 * documents, included figures etc).
143 * The file name must not denote a file in our temporary directory, but a
144 * file that the user chose.
146 class DocFileName : public FileName {
149 /** \param abs_filename the file in question. Must have an absolute path.
150 * \param save_abs_path how is the file to be output to file?
152 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
153 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
155 /** \param filename the file in question. May have either a relative
156 * or an absolute path.
157 * \param buffer_path if \c filename has a relative path, generate
158 * the absolute path using this.
160 void set(std::string const & filename, std::string const & buffer_path);
164 bool saveAbsPath() const { return save_abs_path_; }
165 /// \param buffer_path if empty, uses `pwd`
166 std::string const relFilename(std::string const & buffer_path = std::string()) const;
167 /// \param buf_path if empty, uses `pwd`
168 std::string const outputFilename(std::string const & buf_path = std::string()) const;
170 /** @returns a mangled representation of the absolute file name
171 * suitable for use in the temp dir when, for example, converting
172 * an image file to another format.
174 * @param dir the directory that will contain this file with
175 * its mangled name. This information is used by the mangling
176 * algorithm when determining the maximum allowable length of
179 * An example of a mangled name:
180 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
182 * It is guaranteed that
183 * - two different filenames have different mangled names
184 * - two FileName instances with the same filename have identical
187 * Only the mangled file name is returned. It is not prepended
191 mangledFilename(std::string const & dir = std::string()) const;
193 /// \return true if the file is compressed.
194 bool isZipped() const;
195 /// \return the absolute file name without its .gz, .z, .Z extension
196 std::string const unzippedFilename() const;
200 /// Cache for isZipped() because zippedFile() is expensive
201 mutable bool zipped_;
202 /// Is zipped_ valid?
203 mutable bool zipped_valid_;
207 bool operator==(DocFileName const &, DocFileName const &);
208 bool operator!=(DocFileName const &, DocFileName const &);
211 } // namespace support