]> git.lyx.org Git - lyx.git/blob - src/support/FileName.h
isome more FileName shuffling
[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         /**
74          * Get a FileName from \p name in the encoding used by the file system.
75          * Only use this for filenames you got directly from the file system,
76          * e.g. from reading a directory.
77          * \p name must have an absolute path.
78          */
79         static FileName fromFilesystemEncoding(std::string const & name);
80         /// (securely) create a temporary file in the given dir with the given mask
81         /// \p mask must be in filesystem encoding
82         static FileName tempName(FileName const & dir = FileName(),
83                                                 std::string const & mask = std::string());
84 protected:
85         /// The absolute file name.
86         /// The encoding is currently unspecified, anything else than ASCII
87         /// may or may not work.
88         std::string name_;
89 };
90
91
92 bool operator==(FileName const &, FileName const &);
93 bool operator!=(FileName const &, FileName const &);
94 bool operator<(FileName const &, FileName const &);
95 bool operator>(FileName const &, FileName const &);
96 std::ostream & operator<<(std::ostream &, FileName const &);
97
98
99 /**
100  * Class for storing file names that appear in documents (e. g. child
101  * documents, included figures etc).
102  * The file name must not denote a file in our temporary directory, but a
103  * file that the user chose.
104  */
105 class DocFileName : public FileName {
106 public:
107         DocFileName();
108         /** \param abs_filename the file in question. Must have an absolute path.
109          *  \param save_abs_path how is the file to be output to file?
110          */
111         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
112         DocFileName(FileName const & abs_filename, bool save_abs_path = true);
113
114         /** \param filename the file in question. May have either a relative
115          *  or an absolute path.
116          *  \param buffer_path if \c filename has a relative path, generate
117          *  the absolute path using this.
118          */
119         void set(std::string const & filename, std::string const & buffer_path);
120
121         void erase();
122
123         bool saveAbsPath() const { return save_abs_path_; }
124         /// \param buffer_path if empty, uses `pwd`
125         std::string const relFilename(std::string const & buffer_path = std::string()) const;
126         /// \param buf_path if empty, uses `pwd`
127         std::string const outputFilename(std::string const & buf_path = std::string()) const;
128         
129         /** @returns a mangled representation of the absolute file name
130          *  suitable for use in the temp dir when, for example, converting
131          *  an image file to another format.
132          *
133          *  @param dir the directory that will contain this file with
134          *  its mangled name. This information is used by the mangling
135          *  algorithm when determining the maximum allowable length of
136          *  the mangled name.
137          *
138          *  An example of a mangled name:
139          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
140          *
141          *  It is guaranteed that
142          *  - two different filenames have different mangled names
143          *  - two FileName instances with the same filename have identical
144          *    mangled names.
145          *
146          *  Only the mangled file name is returned. It is not prepended
147          *  with @c dir.
148          */
149         std::string const
150         mangledFilename(std::string const & dir = std::string()) const;
151
152         /// \return true if the file is compressed.
153         bool isZipped() const;
154         /// \return the absolute file name without its .gz, .z, .Z extension
155         std::string const unzippedFilename() const;
156
157 private:
158         bool save_abs_path_;
159         /// Cache for isZipped() because zippedFile() is expensive
160         mutable bool zipped_;
161         /// Is zipped_ valid?
162         mutable bool zipped_valid_;
163 };
164
165
166 bool operator==(DocFileName const &, DocFileName const &);
167 bool operator!=(DocFileName const &, DocFileName const &);
168
169
170 } // namespace support
171 } // namespace lyx
172
173 #endif