]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
d447f5abf35c066123a282e8269da6dffad261b6
[lyx.git] / src / support / FileName.h
1 // -*- C++ -*-
2 /**
3  * \file FileName.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef FILENAME_H
13 #define FILENAME_H
14
15 #include "strfwd.h"
16
17 #include <ctime>
18 #include <string>
19 #include <vector>
20
21
22 namespace lyx {
23 namespace support {
24
25
26 /**
27  * Class for storing file names.
28  * The file name may be empty. If it is not empty it is an absolute path.
29  * The file may or may not exist.
30  */
31 class FileName {
32 public:
33         /// Constructor for empty filenames
34         FileName();
35         /** Constructor for nonempty filenames.
36          * explicit because we don't want implicit conversion of relative
37          * paths in function arguments (e.g. of unlink).
38          * \param abs_filename the file in question. Must have an absolute path.
39          * Encoding is always UTF-8.
40          */
41         explicit FileName(std::string const & abs_filename);
42
43         /// copy constructor.
44         FileName(FileName const &);
45
46         ///
47         FileName & operator=(FileName const &);
48
49         virtual ~FileName() {}
50         /** Set a new filename.
51          * \param filename the file in question. Must have an absolute path.
52          * Encoding is always UTF-8.
53          */
54         virtual void set(std::string const & filename);
55         virtual void erase();
56         /// Is this filename empty?
57         bool empty() const;
58         /// get the absolute file name in UTF-8 encoding
59         std::string absFilename() const;
60         /**
61          * Get the file name in the encoding used by the file system.
62          * Only use this for accessing the file, e.g. with an fstream.
63          */
64         std::string toFilesystemEncoding() const;
65
66         /// returns true if the file exists
67         bool exists() const;
68         /// \return true if this object points to a symbolic link.
69         bool isSymLink() const;
70         /// \return true if the file is empty.
71         bool isFileEmpty() const;
72         /// returns time of last write access
73         std::time_t lastModified() const;
74         /// return true when file is readable but not writabel
75         bool isReadOnly() const;
76         /// return true when it names a directory
77         bool isDirectory() const;
78         /// return true when file/directory is readable
79         bool isReadableDirectory() const;
80         /// return true when it is a file and readable
81         bool isReadableFile() const;
82         /// return true when file/directory is writable
83         bool isWritable() const;
84         /// return true when file/directory is writable (write test file)
85         bool isDirWritable() const;
86         
87         /// return true when file/directory is writable (write test file)
88         bool copyTo(FileName const & target) const;
89
90         /// remove directory and all contents, returns true on success
91         bool destroyDirectory() const;
92         /// Creates directory. Returns true on success
93         bool createDirectory(int permissions) const;
94
95         /// \return list files in a directory having optional extension ext..
96         std::vector<FileName> dirList(
97                 std::string const & ext = std::string());
98
99         /// Get the contents of a file as a huge std::string
100         std::string fileContents() const;
101         /**
102          * Get a FileName from \p name in the encoding used by the file system.
103          * Only use this for filenames you got directly from the file system,
104          * e.g. from reading a directory.
105          * \p name must have an absolute path.
106          */
107
108         /** Guess the file format name (as in Format::name()) from contents.
109          Normally you don't want to use this directly, but rather
110          Formats::getFormatFromFile().
111          */
112         std::string guessFormatFromContents() const;
113
114         /// check for zipped file
115         bool isZippedFile() const;
116
117         static FileName fromFilesystemEncoding(std::string const & name);
118         /// (securely) create a temporary file in the given dir with the given mask
119         /// \p mask must be in filesystem encoding
120         static FileName tempName(FileName const & dir = FileName(),
121                                                 std::string const & mask = std::string());
122
123         /// filename without path
124         std::string onlyFileName() const;
125         /// path without file name
126         FileName onlyPath() const;
127         /// used for display in the Gui
128         docstring displayName(int threshold = 1000) const;
129
130 private:
131         friend class DocFileName;
132         ///
133         struct Private;
134         Private * const d;
135 };
136
137
138 bool operator==(FileName const &, FileName const &);
139 bool operator!=(FileName const &, FileName const &);
140 bool operator<(FileName const &, FileName const &);
141 bool operator>(FileName const &, FileName const &);
142 std::ostream & operator<<(std::ostream &, FileName const &);
143
144
145 /**
146  * Class for storing file names that appear in documents (e. g. child
147  * documents, included figures etc).
148  * The file name must not denote a file in our temporary directory, but a
149  * file that the user chose.
150  */
151 class DocFileName : public FileName {
152 public:
153         DocFileName();
154         /** \param abs_filename the file in question. Must have an absolute path.
155          *  \param save_abs_path how is the file to be output to file?
156          */
157         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
158         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
159
160         /** \param filename the file in question. May have either a relative
161          *  or an absolute path.
162          *  \param buffer_path if \c filename has a relative path, generate
163          *  the absolute path using this.
164          */
165         void set(std::string const & filename, std::string const & buffer_path);
166
167         void erase();
168
169         bool saveAbsPath() const { return save_abs_path_; }
170         /// \param buffer_path if empty, uses `pwd`
171         std::string const relFilename(std::string const & buffer_path = std::string()) const;
172         /// \param buf_path if empty, uses `pwd`
173         std::string const outputFilename(std::string const & buf_path = std::string()) const;
174         
175         /** @returns a mangled representation of the absolute file name
176          *  suitable for use in the temp dir when, for example, converting
177          *  an image file to another format.
178          *
179          *  @param dir the directory that will contain this file with
180          *  its mangled name. This information is used by the mangling
181          *  algorithm when determining the maximum allowable length of
182          *  the mangled name.
183          *
184          *  An example of a mangled name:
185          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
186          *
187          *  It is guaranteed that
188          *  - two different filenames have different mangled names
189          *  - two FileName instances with the same filename have identical
190          *    mangled names.
191          *
192          *  Only the mangled file name is returned. It is not prepended
193          *  with @c dir.
194          */
195         std::string const
196         mangledFilename(std::string const & dir = std::string()) const;
197
198         /// \return true if the file is compressed.
199         bool isZipped() const;
200         /// \return the absolute file name without its .gz, .z, .Z extension
201         std::string const unzippedFilename() const;
202
203 private:
204         bool save_abs_path_;
205         /// Cache for isZipped() because zippedFile() is expensive
206         mutable bool zipped_;
207         /// Is zipped_ valid?
208         mutable bool zipped_valid_;
209 };
210
211
212 bool operator==(DocFileName const &, DocFileName const &);
213 bool operator!=(DocFileName const &, DocFileName const &);
214
215
216 } // namespace support
217 } // namespace lyx
218
219 #endif