]> git.lyx.org Git - lyx.git/blob - src/support/filename.h
Forward port the name-mangling code from 1.3.x.
[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 class FileName {
23 public:
24         FileName();
25         /** \param abs_filename the file in question. Must have an absolute path.
26          *  \param save_abs_path how is the file to be output to file?
27          */
28         FileName(std::string const & abs_filename, bool save_abs_path = true);
29
30         /** \param filename the file in question. May have either a relative
31          *  or an absolute path.
32          *  \param buffer_path if \c filename has a relative path, generate
33          *  the absolute path using this.
34          */
35         void set(std::string const & filename, std::string const & buffer_path);
36
37         void erase();
38         bool empty() const { return name_.empty(); }
39
40         bool saveAbsPath() const { return save_abs_path_; }
41         std::string const absFilename() const { return name_; }
42         /// \param buffer_path if empty, uses `pwd`
43         std::string const relFilename(std::string const & buffer_path = std::string()) const;
44         /// \param buf_path if empty, uses `pwd`
45         std::string const outputFilename(std::string const & buf_path = std::string()) const;
46
47         /** @returns a mangled representation of the absolute file name
48          *  suitable for use in the temp dir when, for example, converting
49          *  an image file to another format.
50          *
51          *  @param dir the directory that will contain this file with
52          *  its mangled name. This information is used by the mangling
53          *  algorithm when determining the maximum allowable length of
54          *  the mangled name.
55          *
56          *  An example of a mangled name:
57          *  C:/foo bar/baz.eps -> 0C__foo_bar_baz.eps
58          *
59          *  It is guaranteed that
60          *  - two different filenames have different mangled names
61          *  - two FileName instances with the same filename have identical
62          *    mangled names.
63          *
64          *  Only the mangled file name is returned. It is not prepended
65          *  with @c dir.
66          */
67         std::string const
68         mangledFilename(std::string const & dir = std::string()) const;
69
70         /// \return true if the file is compressed.
71         bool isZipped() const;
72         /// \return the absolute file name without its .gz, .z, .Z extension
73         std::string const unzippedFilename() const;
74
75 private:
76         std::string name_;
77         bool save_abs_path_;
78 };
79
80
81 bool operator==(FileName const &, FileName const &);
82 bool operator!=(FileName const &, FileName const &);
83
84
85 } // namespace support
86 } // namespace lyx
87
88 #endif