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