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