]> git.lyx.org Git - lyx.git/blob - src/support/filename.C
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / filename.C
1 /**
2  * \file filename.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "filename.h"
14
15 #include "filetools.h"
16 #include "lstrings.h"
17 #include "os.h"
18
19 #include <boost/assert.hpp>
20
21 #include <map>
22 #include <sstream>
23
24
25 using std::map;
26 using std::string;
27
28
29 namespace lyx {
30 namespace support {
31
32
33 FileName::FileName()
34         : save_abs_path_(true)
35 {}
36
37
38 FileName::FileName(string const & abs_filename, bool save_abs)
39         : name_(abs_filename), save_abs_path_(save_abs)
40 {
41         BOOST_ASSERT(AbsolutePath(name_));
42 }
43
44
45 void FileName::set(string const & name, string const & buffer_path)
46 {
47         save_abs_path_ = AbsolutePath(name);
48         name_ = save_abs_path_ ? name : MakeAbsPath(name, buffer_path);
49 }
50
51
52 void FileName::erase()
53 {
54         name_.erase();
55 }
56
57
58 string const FileName::relFilename(string const & path) const
59 {
60         return MakeRelPath(name_, path);
61 }
62
63
64 string const FileName::outputFilename(string const & path) const
65 {
66         return save_abs_path_ ? name_ : MakeRelPath(name_, path);
67 }
68
69
70 string const FileName::mangledFilename() const
71 {
72         // We need to make sure that every FileName instance for a given
73         // filename returns the same mangled name.
74         typedef map<string, string> MangledMap;
75         static MangledMap mangledNames;
76         MangledMap::const_iterator const it = mangledNames.find(name_);
77         if (it != mangledNames.end())
78                 return (*it).second;
79
80         // Now the real work
81         string mname = os::slashify_path(name_);
82         // Remove the extension.
83         mname = ChangeExtension(name_, string());
84         // Replace '/' in the file name with '_'
85         mname = subst(mname, "/", "_");
86         // Replace '.' in the file name with '_'
87         mname = subst(mname, ".", "_");
88         // Add the extension back on
89         mname = ChangeExtension(mname, GetExtension(name_));
90         // Prepend a counter to the filename. This is necessary to make
91         // the mangled name unique.
92         static int counter = 0;
93         std::ostringstream s;
94         s << counter++;
95         mname = s.str() + mname;
96         mangledNames[name_] = mname;
97         return mname;
98 }
99
100
101 bool FileName::isZipped() const
102 {
103         return zippedFile(name_);
104 }
105
106
107 string const FileName::unzippedFilename() const
108 {
109         return unzippedFileName(name_);
110 }
111
112
113 bool operator==(FileName const & lhs, FileName const & rhs)
114 {
115         return lhs.absFilename() == rhs.absFilename() &&
116                 lhs.saveAbsPath() == rhs.saveAbsPath();
117 }
118
119
120 bool operator!=(FileName const & lhs, FileName const & rhs)
121 {
122         return !(lhs == rhs);
123 }
124
125 } // namespace support
126 } // namespace lyx