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 /// \return true if this object points to a symbolic link.
61 bool isSymLink() const;
62 /// \return true if the file is empty.
63 bool isFileEmpty() const;
64 /// returns time of last write access
65 std::time_t lastModified() const;
66 /// return true when file is readable but not writabel
67 bool isReadOnly() const;
68 /// return true when it names a directory
69 bool isDirectory() const;
70 /// return true when file/directory is readable
71 bool isReadableDirectory() const;
72 /// return true when it is a file and readable
73 bool isReadableFile() const;
74 /// return true when file/directory is writable
75 bool isWritable() const;
76 /// return true when file/directory is writable (write test file)
77 bool isDirWritable() const;
79 /// return true when file/directory is writable (write test file)
80 bool copyTo(FileName const & target, bool noclobber) const;
82 /// remove directory and all contents, returns true on success
83 bool destroyDirectory() const;
84 /// Creates directory. Returns true on success
85 bool createDirectory(int permissions) const;
87 /// Get the contents of a file as a huge std::string
88 std::string fileContents() const;
90 * Get a FileName from \p name in the encoding used by the file system.
91 * Only use this for filenames you got directly from the file system,
92 * e.g. from reading a directory.
93 * \p name must have an absolute path.
96 /** Guess the file format name (as in Format::name()) from contents.
97 Normally you don't want to use this directly, but rather
98 Formats::getFormatFromFile().
100 std::string guessFormatFromContents() const;
102 /// check for zipped file
103 bool isZippedFile() const;
105 static FileName fromFilesystemEncoding(std::string const & name);
106 /// (securely) create a temporary file in the given dir with the given mask
107 /// \p mask must be in filesystem encoding
108 static FileName tempName(FileName const & dir = FileName(),
109 std::string const & mask = std::string());
111 /// filename without path
112 std::string onlyFileName() const;
113 /// path without file name
114 FileName onlyPath() const;
115 /// used for display in the Gui
116 docstring displayName(int threshold = 1000) const;
120 /// The absolute file name in UTF-8 encoding.
125 bool operator==(FileName const &, FileName const &);
126 bool operator!=(FileName const &, FileName const &);
127 bool operator<(FileName const &, FileName const &);
128 bool operator>(FileName const &, FileName const &);
129 std::ostream & operator<<(std::ostream &, FileName const &);
133 * Class for storing file names that appear in documents (e. g. child
134 * documents, included figures etc).
135 * The file name must not denote a file in our temporary directory, but a
136 * file that the user chose.
138 class DocFileName : public FileName {
141 /** \param abs_filename the file in question. Must have an absolute path.
142 * \param save_abs_path how is the file to be output to file?
144 DocFileName(std::string const & abs_filename, bool save_abs_path = true);
145 DocFileName(FileName const & abs_filename, bool save_abs_path = true);
147 /** \param filename the file in question. May have either a relative
148 * or an absolute path.
149 * \param buffer_path if \c filename has a relative path, generate
150 * the absolute path using this.
152 void set(std::string const & filename, std::string const & buffer_path);
156 bool saveAbsPath() const { return save_abs_path_; }
157 /// \param buffer_path if empty, uses `pwd`
158 std::string const relFilename(std::string const & buffer_path = std::string()) const;
159 /// \param buf_path if empty, uses `pwd`
160 std::string const outputFilename(std::string const & buf_path = std::string()) const;
162 /** @returns a mangled representation of the absolute file name
163 * suitable for use in the temp dir when, for example, converting
164 * an image file to another format.
166 * @param dir the directory that will contain this file with
167 * its mangled name. This information is used by the mangling
168 * algorithm when determining the maximum allowable length of
171 * An example of a mangled name:
172 * C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
174 * It is guaranteed that
175 * - two different filenames have different mangled names
176 * - two FileName instances with the same filename have identical
179 * Only the mangled file name is returned. It is not prepended
183 mangledFilename(std::string const & dir = std::string()) const;
185 /// \return true if the file is compressed.
186 bool isZipped() const;
187 /// \return the absolute file name without its .gz, .z, .Z extension
188 std::string const unzippedFilename() const;
192 /// Cache for isZipped() because zippedFile() is expensive
193 mutable bool zipped_;
194 /// Is zipped_ valid?
195 mutable bool zipped_valid_;
199 bool operator==(DocFileName const &, DocFileName const &);
200 bool operator!=(DocFileName const &, DocFileName const &);
203 } // namespace support