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