]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
* gcc does not like missing characters in keywords
[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 <string>
16 #include <ctime>
17
18
19 namespace lyx {
20 namespace support {
21
22
23 /**
24  * Class for storing file names.
25  * The file name may be empty. If it is not empty it is an absolute path.
26  * The file may or may not exist.
27  */
28 class FileName {
29 public:
30         /// Constructor for empty filenames
31         FileName() {}
32         /** Constructor for nonempty filenames.
33          * explicit because we don't want implicit conversion of relative
34          * paths in function arguments (e.g. of unlink).
35          * \param abs_filename the file in question. Must have an absolute path.
36          * Encoding is always UTF-8.
37          */
38         explicit FileName(std::string const & abs_filename);
39         virtual ~FileName() {}
40         /** Set a new filename.
41          * \param filename the file in question. Must have an absolute path.
42          * Encoding is always UTF-8.
43          */
44         virtual void set(std::string const & filename);
45         virtual void erase();
46         /// Is this filename empty?
47         bool empty() const { return name_.empty(); }
48         /// get the absolute file name in UTF-8 encoding
49         std::string const absFilename() const { return name_; }
50         /**
51          * Get the file name in the encoding used by the file system.
52          * Only use this for accessing the file, e.g. with an fstream.
53          */
54         std::string toFilesystemEncoding() const;
55
56         /// returns true if the file exists
57         bool exists() const;
58         /// returns time of last write access
59         std::time_t lastModified() const;
60         /// return true when file is readable but not writabel
61         bool isReadOnly() const;
62         /// return true when it names a directory
63         bool isDirectory() const;
64         /// return true when file/directory is readable
65         bool isReadable() const;
66         /// return true when it is a file and readable
67         bool isFileReadable() const;
68         /// return true when file/directory is writable
69         bool isWritable() const;
70         /// return true when file/directory is writable (write test file)
71         bool isDirWritable() const;
72         
73         /// remove directory and all contents, returns true on success
74         bool destroyDirectory() const;
75         /// Creates directory. Returns true on success
76         bool createDirectory(int permissions) const;
77
78         /// Get the contents of a file as a huge std::string
79         std::string fileContents() const;
80         /**
81          * Get a FileName from \p name in the encoding used by the file system.
82          * Only use this for filenames you got directly from the file system,
83          * e.g. from reading a directory.
84          * \p name must have an absolute path.
85          */
86
87         /** Guess the file format name (as in Format::name()) from contents.
88          Normally you don't want to use this directly, but rather
89          Formats::getFormatFromFile().
90          */
91         std::string guessFormatFromContents() const;
92
93         /// check for zipped file
94         bool isZippedFile() const;
95
96         static FileName fromFilesystemEncoding(std::string const & name);
97         /// (securely) create a temporary file in the given dir with the given mask
98         /// \p mask must be in filesystem encoding
99         static FileName tempName(FileName const & dir = FileName(),
100                                                 std::string const & mask = std::string());
101 protected:
102         /// The absolute file name.
103         /// The encoding is currently unspecified, anything else than ASCII
104         /// may or may not work.
105         std::string name_;
106 };
107
108
109 bool operator==(FileName const &, FileName const &);
110 bool operator!=(FileName const &, FileName const &);
111 bool operator<(FileName const &, FileName const &);
112 bool operator>(FileName const &, FileName const &);
113 std::ostream & operator<<(std::ostream &, FileName const &);
114
115
116 /**
117  * Class for storing file names that appear in documents (e. g. child
118  * documents, included figures etc).
119  * The file name must not denote a file in our temporary directory, but a
120  * file that the user chose.
121  */
122 class DocFileName : public FileName {
123 public:
124         DocFileName();
125         /** \param abs_filename the file in question. Must have an absolute path.
126          *  \param save_abs_path how is the file to be output to file?
127          */
128         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
129         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
130
131         /** \param filename the file in question. May have either a relative
132          *  or an absolute path.
133          *  \param buffer_path if \c filename has a relative path, generate
134          *  the absolute path using this.
135          */
136         void set(std::string const & filename, std::string const & buffer_path);
137
138         void erase();
139
140         bool saveAbsPath() const { return save_abs_path_; }
141         /// \param buffer_path if empty, uses `pwd`
142         std::string const relFilename(std::string const & buffer_path = std::string()) const;
143         /// \param buf_path if empty, uses `pwd`
144         std::string const outputFilename(std::string const & buf_path = std::string()) const;
145         
146         /** @returns a mangled representation of the absolute file name
147          *  suitable for use in the temp dir when, for example, converting
148          *  an image file to another format.
149          *
150          *  @param dir the directory that will contain this file with
151          *  its mangled name. This information is used by the mangling
152          *  algorithm when determining the maximum allowable length of
153          *  the mangled name.
154          *
155          *  An example of a mangled name:
156          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
157          *
158          *  It is guaranteed that
159          *  - two different filenames have different mangled names
160          *  - two FileName instances with the same filename have identical
161          *    mangled names.
162          *
163          *  Only the mangled file name is returned. It is not prepended
164          *  with @c dir.
165          */
166         std::string const
167         mangledFilename(std::string const & dir = std::string()) const;
168
169         /// \return true if the file is compressed.
170         bool isZipped() const;
171         /// \return the absolute file name without its .gz, .z, .Z extension
172         std::string const unzippedFilename() const;
173
174 private:
175         bool save_abs_path_;
176         /// Cache for isZipped() because zippedFile() is expensive
177         mutable bool zipped_;
178         /// Is zipped_ valid?
179         mutable bool zipped_valid_;
180 };
181
182
183 bool operator==(DocFileName const &, DocFileName const &);
184 bool operator!=(DocFileName const &, DocFileName const &);
185
186
187 } // namespace support
188 } // namespace lyx
189
190 #endif