]> git.lyx.org Git - lyx.git/blob - src/support/filename.h
* allow resizing of detached panel
[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 protected:
29         /// Constructor for empty filenames (only needed for DocFileName)
30         FileName();
31 public:
32         /** Constructor for nonempty filenames.
33          * \param abs_filename the file in question. Must have an absolute path.
34          */
35         FileName(std::string const & abs_filename);
36         /// Is this filename empty?
37         bool empty() const { return name_.empty(); }
38         /// get the absolute file name
39         std::string const absFilename() const { return name_; }
40 protected:
41         /// The absolute file name.
42         /// The encoding is currently unspecified, anything else than ASCII
43         /// may or may not work.
44         std::string name_;
45 };
46
47
48 bool operator==(FileName const &, FileName const &);
49 bool operator!=(FileName const &, FileName const &);
50
51
52 /**
53  * Class for storing file names that appear in documents (e. g. child
54  * documents, included figures etc).
55  * The file name must not denote a file in our temporary directory, but a
56  * file that the user chose.
57  */
58 class DocFileName : public FileName {
59 public:
60         DocFileName();
61         /** \param abs_filename the file in question. Must have an absolute path.
62          *  \param save_abs_path how is the file to be output to file?
63          */
64         DocFileName(std::string const & abs_filename, bool save_abs_path = true);
65
66         /** \param filename the file in question. May have either a relative
67          *  or an absolute path.
68          *  \param buffer_path if \c filename has a relative path, generate
69          *  the absolute path using this.
70          */
71         void set(std::string const & filename, std::string const & buffer_path);
72
73         void erase();
74
75         bool saveAbsPath() const { return save_abs_path_; }
76         /// \param buffer_path if empty, uses `pwd`
77         std::string const relFilename(std::string const & buffer_path = std::string()) const;
78         /// \param buf_path if empty, uses `pwd`
79         std::string const outputFilename(std::string const & buf_path = std::string()) const;
80
81         /** @returns a mangled representation of the absolute file name
82          *  suitable for use in the temp dir when, for example, converting
83          *  an image file to another format.
84          *
85          *  @param dir the directory that will contain this file with
86          *  its mangled name. This information is used by the mangling
87          *  algorithm when determining the maximum allowable length of
88          *  the mangled name.
89          *
90          *  An example of a mangled name:
91          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
92          *
93          *  It is guaranteed that
94          *  - two different filenames have different mangled names
95          *  - two FileName instances with the same filename have identical
96          *    mangled names.
97          *
98          *  Only the mangled file name is returned. It is not prepended
99          *  with @c dir.
100          */
101         std::string const
102         mangledFilename(std::string const & dir = std::string()) const;
103
104         /// \return true if the file is compressed.
105         bool isZipped() const;
106         /// \return the absolute file name without its .gz, .z, .Z extension
107         std::string const unzippedFilename() const;
108
109 private:
110         bool save_abs_path_;
111         /// Cache for isZipped() because zippedFile() is expensive
112         mutable bool zipped_;
113         /// Is zipped_ valid?
114         mutable bool zipped_valid_;
115 };
116
117
118 bool operator==(DocFileName const &, DocFileName const &);
119 bool operator!=(DocFileName const &, DocFileName const &);
120
121
122 } // namespace support
123 } // namespace lyx
124
125 #endif