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