]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
dfda706f2397461abf684f739652d3ec5bc165ee
[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 <string>
18 #include <ctime>
19
20
21 namespace lyx {
22 namespace support {
23
24
25 /**
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.
29  */
30 class FileName {
31 public:
32         /// Constructor for empty filenames
33         FileName();
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.
39          */
40         explicit FileName(std::string const & abs_filename);
41
42         /// copy constructor.
43         FileName(FileName const &);
44
45         ///
46         FileName & operator=(FileName const &);
47
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.
52          */
53         virtual void set(std::string const & filename);
54         virtual void erase();
55         /// Is this filename empty?
56         bool empty() const;
57         /// get the absolute file name in UTF-8 encoding
58         std::string absFilename() const;
59         /**
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.
62          */
63         std::string toFilesystemEncoding() const;
64
65         /// returns true if the file exists
66         bool exists() const;
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;
85         
86         /// return true when file/directory is writable (write test file)
87         bool copyTo(FileName const & target) const;
88
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;
93
94         /// Get the contents of a file as a huge std::string
95         std::string fileContents() const;
96         /**
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.
101          */
102
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().
106          */
107         std::string guessFormatFromContents() const;
108
109         /// check for zipped file
110         bool isZippedFile() const;
111
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());
117
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;
124
125 private:
126         friend class DocFileName;
127         ///
128         struct Private;
129         Private * const d;
130 };
131
132
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 &);
138
139
140 /**
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.
145  */
146 class DocFileName : public FileName {
147 public:
148         DocFileName();
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?
151          */
152         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
153         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
154
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.
159          */
160         void set(std::string const & filename, std::string const & buffer_path);
161
162         void erase();
163
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;
169         
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.
173          *
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
177          *  the mangled name.
178          *
179          *  An example of a mangled name:
180          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
181          *
182          *  It is guaranteed that
183          *  - two different filenames have different mangled names
184          *  - two FileName instances with the same filename have identical
185          *    mangled names.
186          *
187          *  Only the mangled file name is returned. It is not prepended
188          *  with @c dir.
189          */
190         std::string const
191         mangledFilename(std::string const & dir = std::string()) const;
192
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;
197
198 private:
199         bool save_abs_path_;
200         /// Cache for isZipped() because zippedFile() is expensive
201         mutable bool zipped_;
202         /// Is zipped_ valid?
203         mutable bool zipped_valid_;
204 };
205
206
207 bool operator==(DocFileName const &, DocFileName const &);
208 bool operator!=(DocFileName const &, DocFileName const &);
209
210
211 } // namespace support
212 } // namespace lyx
213
214 #endif