]> git.lyx.org Git - lyx.git/blob - src/support/filename.C
src/support/filename.C: add assertion to ensure that no Windows-style
[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 #include <algorithm>
23
24
25 using std::map;
26 using std::string;
27
28
29 namespace lyx {
30 namespace support {
31
32
33 FileName::FileName()
34 {}
35
36
37 FileName::~FileName()
38 {}
39
40
41 FileName::FileName(string const & abs_filename)
42         : name_(abs_filename)
43 {
44         BOOST_ASSERT(absolutePath(name_));
45         BOOST_ASSERT(!contains(name_, '\\'));
46 }
47
48
49 void FileName::set(string const & name)
50 {
51         name_ = name;
52         BOOST_ASSERT(absolutePath(name_));
53         BOOST_ASSERT(!contains(name_, '\\'));
54 }
55
56
57 void FileName::erase()
58 {
59         name_.erase();
60 }
61
62
63 string const FileName::toFilesystemEncoding() const
64 {
65         // FIXME UNICODE: correct encoding not implemented yet
66         return name_;
67 }
68
69
70 bool operator==(FileName const & lhs, FileName const & rhs)
71 {
72         return lhs.absFilename() == rhs.absFilename();
73 }
74
75
76 bool operator!=(FileName const & lhs, FileName const & rhs)
77 {
78         return lhs.absFilename() != rhs.absFilename();
79 }
80
81
82 bool operator<(FileName const & lhs, FileName const & rhs)
83 {
84         return lhs.absFilename() < rhs.absFilename();
85 }
86
87
88 bool operator>(FileName const & lhs, FileName const & rhs)
89 {
90         return lhs.absFilename() > rhs.absFilename();
91 }
92
93
94 std::ostream & operator<<(std::ostream & os, FileName const & filename)
95 {
96         return os << filename.absFilename();
97 }
98
99
100 DocFileName::DocFileName()
101         : save_abs_path_(true)
102 {}
103
104
105 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
106         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
107 {}
108
109
110 void DocFileName::set(string const & name, string const & buffer_path)
111 {
112         save_abs_path_ = absolutePath(name);
113         name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
114         zipped_valid_ = false;
115 }
116
117
118 void DocFileName::erase()
119 {
120         name_.erase();
121         zipped_valid_ = false;
122 }
123
124
125 string const DocFileName::relFilename(string const & path) const
126 {
127         return makeRelPath(name_, path);
128 }
129
130
131 string const DocFileName::outputFilename(string const & path) const
132 {
133         return save_abs_path_ ? name_ : makeRelPath(name_, path);
134 }
135
136
137 string const DocFileName::mangledFilename(std::string const & dir) const
138 {
139         // We need to make sure that every DocFileName instance for a given
140         // filename returns the same mangled name.
141         typedef map<string, string> MangledMap;
142         static MangledMap mangledNames;
143         MangledMap::const_iterator const it = mangledNames.find(name_);
144         if (it != mangledNames.end())
145                 return (*it).second;
146
147         // Now the real work
148         string mname = os::internal_path(name_);
149         // Remove the extension.
150         mname = changeExtension(name_, string());
151         // Replace '/' in the file name with '_'
152         mname = subst(mname, "/", "_");
153         // Replace '.' in the file name with '_'
154         mname = subst(mname, ".", "_");
155         // Replace ' ' in the file name with '_'
156         mname = subst(mname, " ", "_");
157         // Replace ':' in the file name with '_'
158         mname = subst(mname, ":", "_");
159         // Add the extension back on
160         mname = changeExtension(mname, getExtension(name_));
161
162         // Prepend a counter to the filename. This is necessary to make
163         // the mangled name unique.
164         static int counter = 0;
165         std::ostringstream s;
166         s << counter++ << mname;
167         mname = s.str();
168
169         // MiKTeX's YAP (version 2.4.1803) crashes if the file name
170         // is longer than about 160 characters. MiKTeX's pdflatex
171         // is even pickier. A maximum length of 100 has been proven to work.
172         // If dir.size() > max length, all bets are off for YAP. We truncate
173         // the filename nevertheless, keeping a minimum of 10 chars.
174
175         string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
176
177         // If the mangled file name is too long, hack it to fit.
178         // We know we're guaranteed to have a unique file name because
179         // of the counter.
180         if (mname.size() > max_length) {
181                 int const half = (int(max_length) / 2) - 2;
182                 if (half > 0) {
183                         mname = mname.substr(0, half) + "___" +
184                                 mname.substr(mname.size() - half);
185                 }
186         }
187
188         mangledNames[name_] = mname;
189         return mname;
190 }
191
192
193 bool DocFileName::isZipped() const
194 {
195         if (!zipped_valid_) {
196                 zipped_ = zippedFile(*this);
197                 zipped_valid_ = true;
198         }
199         return zipped_;
200 }
201
202
203 string const DocFileName::unzippedFilename() const
204 {
205         return unzippedFileName(name_);
206 }
207
208
209 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
210 {
211         return lhs.absFilename() == rhs.absFilename() &&
212                 lhs.saveAbsPath() == rhs.saveAbsPath();
213 }
214
215
216 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
217 {
218         return !(lhs == rhs);
219 }
220
221 } // namespace support
222 } // namespace lyx